c - 在 C 中使用堆栈和数组计算后缀表达式

标签 c postfix-notation

我还是个新手,学习 C 编码的速度不是很快。对于一项作业,我必须使用堆栈从数组中计算后缀表达式。虽然我确信我的代码有几个问题,但我觉得基本结构很好。它编译没有错误,但确实有一些警告。它将运行并打印出 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]=='-' || ..... 

关于c - 在 C 中使用堆栈和数组计算后缀表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16372941/

相关文章:

c - 数字三角形的反转[c]

c++ - 如何仅在 5 个位置的数组中初始化第 4 个位置

c++ - 使用 C++ 的堆栈和 char 数组评估后缀表达式

JavaScript 后缀数学

java - Java 中缀到后缀转换的括号错误检查

c++ - 接受错误 : Bad file descriptor

c - 具有结构的字符串的冒泡排序

c - 在c中选择动态创建的内存池

algorithm - 逆波兰表示法中的变长运算符(后缀)

c++ - 字符串转换为数学表达式