簡體   English   中英

使用C中的堆棧和數組計算后綴表達式

[英]Evaluate a postfix expression using a stack and array in C

我仍然是新的,並且不太快就用C來獲取編碼。對於一項任務,我必須使用堆棧從數組中評估Postfix表達式。 雖然我確信我的代碼有幾個問題但我覺得基本結構很好。 它編譯沒有錯誤,但確實有一些警告。 它將在main中運行並打印出printf語句,但據我所知,它不會在評估中做任何事情。 我不是在尋找作弊或完整修復,但我會贊賞一些指導。請回答假設我知之甚少。

干杯

#include <stdio.h>
#include <stdlib.h>

struct stackNode {
    int data;
    struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;

int evaluatePostfixExpression( char *expr );
int calculate( int op1, int op2, char operator );
void push( StackNodePtr *topPtr, int value );
int pop( StackNodePtr *topPtr );
int isEmpty( StackNodePtr topPtr );
void printStack( StackNodePtr topPtr );

char postfix[50];
int answer;



void main()
{
    printf("Print an postfix expression\n");
    scanf("%s", postfix);
    evaluatePostfixExpression(postfix);
    printf("The value of the expression is: %i",answer);
}

int evaluatePostfixExpression( char *expr )
    //Evaluate the postfix expression.
{

    StackNode node;
    StackNodePtr ptrnode;
    ptrnode = &node;

    int x;
    int y;
    int z;

    strcat(ptrnode,'\0');/*Append the null character ('\0') to the end of the postfix expression.
    When the null character is encountered, no further processing is necessary.*/

    int i=0;
    for(i; postfix[i]!='\0'; i++){      //While '\0' has not been encountered, read the expression from left to right.
        if(isdigit(postfix[i])){           //If the current character is a digit,Push its integer value onto the stack
            push(&ptrnode, postfix[i]); //(the integer value of a digit character is its value in the computer’s character
            printStack(ptrnode);                           //set minus the value of '0' in the computer’s character set).
        }
        else if(postfix[i]=='+'||postfix[i]=='-'||postfix[i]=='*'||postfix[i]=='/'||postfix[i]=='^'){                   //Otherwise, if the current character is an operator, Pop the two top elements of the stack into
            x=pop(&ptrnode);    //variables x and y. Calculate y operator x.
            printStack(ptrnode);
            y=pop(&ptrnode);
            printStack(ptrnode);
            z=calculate(x,y,postfix[i]);
            push(&ptrnode, z);          /*Push the result of the calculation onto the stack.*/
            printStack(ptrnode);
        }
        if (postfix[i]=='\0'){  //When the null character is encountered in the expression, pop the top value of the
            answer = pop(&ptrnode);//stack. This is the result of the postfix expression.
            printStack(ptrnode);
        }
    }
}

int calculate( int op1, int op2, char operator )
    //Evaluate the expression op1 operator op2.
{
    if (operator=='+')
        return op1+op2;

    else if (operator=='-')
        return op1-op2;

    else if (operator=='*')
        return op1*op2;

    else if (operator=='/')
        return op1/op2;

    else if (operator=='^')
        return op1^op2;



    else{
    return printf("calculation error");
    }
}

void push( StackNodePtr *topPtr, int value )
    //Push a value on the stack.
{
    StackNodePtr temp; /* to create a new node */
    temp =  malloc(sizeof(value));//need Malloc because it will not remember it
    temp->data = value;
    temp->nextPtr = NULL; /* the new node points to NULL */


    if(isEmpty(*topPtr)==0) {
    temp->nextPtr = *topPtr;
    }

}

int pop( StackNodePtr *topPtr )
    //Pop a value off the stack.
{
    char Data ; /* to be used to store the data */
    StackNodePtr tmp; /* to be used for handling the node*/
                        /* that is going to be deleted */
    tmp = *topPtr; /* tmp has the address of the node */
                        /* that is going to be deleted */
    Data = tmp->data;
    *topPtr = tmp->nextPtr;

    free(tmp);
    return Data;
}

int isEmpty( StackNodePtr topPtr )
    //Determine if the stack is empty.
{
    if(topPtr->nextPtr==NULL)
        return 1;
    else
        return 0;
}

void printStack( StackNodePtr topPtr )
    //Print the stack.
{
    while ((topPtr->nextPtr)!=NULL){
        printf("%C",topPtr->data);
    }
    if ((topPtr->nextPtr)==NULL)
        printf("NULL");

    printStack(topPtr->nextPtr);
}

我能立刻看到的一件事是:

if(postfix[i]=="%i"){  //If the current character is a digit,Push its integer value onto the stack

這是不正確的。 您正在將索引i處的字符與字符串文字的地址進行比較。 您應該使用像isdigit()這樣的函數。

else if(postfix[i]==('+')|('-')|('*')|('/')|('^')){ 

應該:

else if(postfix[i]=='+' || postfix[i]=='-' || ..... 

通過堆棧評估后固定表達式的常規邏輯可以解決僅1個數字的數字,即0-9。 這是所使用的邏輯的一個非常大的缺點,並且它使得該程序沒有實際用途。 通過簡單地改變輸入到堆棧的方法,我們已經消除了單個數字整數的問題,現在可以使用任何數字的數字進行評估。 獲取代碼: http//wowmoron.wordpress.com/2014/11/16/evaluation-of-postfix-expression-containing-multi-digit-integer/

這是一個解決你的后綴表達式評估問題的鏈接

/* Here is the best example for evaluating postfix expression   */
    /*   8+(7-9)*2    */
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #define Max 20
    #include <ctype.h>
    #define SIZE 50            /* Size of Stack */
    int s[SIZE];

    int TOS=-1,TOP=0;
    int top=-1;       /* Global declarations */

    char infix[Max],post[Max],stack[Max];
    void push(char);
    void infix_post();
    char pop();
    int precedence(char ch);
    void main()
    {
     clrscr();
     printf("\n enter the expression  " );
     fflush(stdin);
     gets(infix);
     strcat(infix,")");
     infix_post();
     printf("\n  Postfix exp. is ::>> %s",post);
     postfix_convert(post);
     getch();
    }
    void infix_post()
    {
      int i=0;
      char temp;
      push('(');
      while(infix[i]!='\0')
      {
           if(isdigit(infix[i]) )
           {
    post[TOP++]=infix[i];
           }
           else
           {
     switch(infix[i])
     {
        case '(':
    push(infix[i]);
    break;
        case ')':
    temp=pop();
    while(temp!='(')
    {
      post[TOP++]=temp;
      temp=pop();
    }
    break;
        case '+':
        case '-':
        case '*':
        case '/':
        case '%':
        case '$':
        case '^':
          while(precedence(infix[i])<=precedence(stack[TOS]))
          {
       temp=pop();
       post[TOP++]=temp;
          }
          push(infix[i]);
          break;

        default:
        break;
     } // switch

           }// else
      i++;
      } // while
      post[TOP]='\0';
    }// main
    void push(char ch)
    {

      if(TOS==Max-1)
      {
        printf("\n overflow");
        getch();
        return;
      }
      else
      {
        stack[++TOS]=ch;
      }
    }
    char pop()
    {
      if(TOS==-1)
      {
        printf("\n underflow");
        return -1;
      }
      else
      return(stack[TOS--]);
    }

    int precedence(char ch)
    {
       if(ch=='+'||ch=='-')
       return 1;
       else if(ch=='%'||ch=='/'||ch=='*')
       return 2;
       else if (ch=='$'||ch=='^')
       return 3;
       else
       return 0;
    }
    push_p(int elem)
    {                       /* Function for PUSH operation */
        s[++top]=elem;
    }

    int pop_p()
    {                      /* Function for POP operation */
        return(s[top--]);
    }

    postfix_convert(char pofx[Max])
    {                         /* Main Program */
        char ch;
        int i=0,op1,op2;
        printf("\n\n Postfix Expression ? %s",pofx);
        while( (ch=pofx[i++]) != '\0')
        {
    if(isdigit(ch))
    push_p(ch-'0'); /* Push the operand */
    else
    {        /* Operator,pop two  operands */
      op2=pop_p();
      op1=pop_p();
       switch(ch)
       {
       case '+':push_p(op1+op2);break;
       case '-':push_p(op1-op2);break;
       case '*':push_p(op1*op2);break;
       case '/':push_p(op1/op2);break;
       }
    }
        }
        printf("\n Given Postfix Expn: %s\n",pofx);
        printf("\n Result after Evaluation: %d\n",s[top]);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM