我还是个新手,用 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);
}