Evaluate a postfix expression using a stack and array in C -
i still new , not quick on picking coding c. assignment have evaluate postfix expression array using stack. while sure have several problems code feel basic structure good. compiles without errors, have warnings. run , printout printf statements in main not in evaluation far can tell. not looking cheat or complete fix guidance appreciated please respond assuming know little.
cheers
#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 postfix expression\n"); scanf("%s", postfix); evaluatepostfixexpression(postfix); printf("the value of expression is: %i",answer); } int evaluatepostfixexpression( char *expr ) //evaluate postfix expression. { stacknode node; stacknodeptr ptrnode; ptrnode = &node; int x; int y; int z; strcat(ptrnode,'\0');/*append null character ('\0') end of postfix expression. when null character encountered, no further processing necessary.*/ int i=0; for(i; postfix[i]!='\0'; i++){ //while '\0' has not been encountered, read expression left right. if(isdigit(postfix[i])){ //if current character digit,push integer value onto stack push(&ptrnode, postfix[i]); //(the integer value of digit character value in computer’s character printstack(ptrnode); //set minus value of '0' in computer’s character set). } else if(postfix[i]=='+'||postfix[i]=='-'||postfix[i]=='*'||postfix[i]=='/'||postfix[i]=='^'){ //otherwise, if current character operator, pop 2 top elements of stack x=pop(&ptrnode); //variables x , y. calculate y operator x. printstack(ptrnode); y=pop(&ptrnode); printstack(ptrnode); z=calculate(x,y,postfix[i]); push(&ptrnode, z); /*push result of calculation onto stack.*/ printstack(ptrnode); } if (postfix[i]=='\0'){ //when null character encountered in expression, pop top value of answer = pop(&ptrnode);//stack. result of postfix expression. printstack(ptrnode); } } } int calculate( int op1, int op2, char operator ) //evaluate 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 value on stack. { stacknodeptr temp; /* create new node */ temp = malloc(sizeof(value));//need malloc because not remember temp->data = value; temp->nextptr = null; /* new node points null */ if(isempty(*topptr)==0) { temp->nextptr = *topptr; } } int pop( stacknodeptr *topptr ) //pop value off stack. { char data ; /* used store data */ stacknodeptr tmp; /* used handling node*/ /* going deleted */ tmp = *topptr; /* tmp has address of node */ /* going deleted */ data = tmp->data; *topptr = tmp->nextptr; free(tmp); return data; } int isempty( stacknodeptr topptr ) //determine if stack empty. { if(topptr->nextptr==null) return 1; else return 0; } void printstack( stacknodeptr topptr ) //print stack. { while ((topptr->nextptr)!=null){ printf("%c",topptr->data); } if ((topptr->nextptr)==null) printf("null"); printstack(topptr->nextptr); }
one thing see was:
if(postfix[i]=="%i"){ //if current character digit,push integer value onto stack
this incorrect. comparing character @ index address of string literal. should using function isdigit() instead.
also
else if(postfix[i]==('+')|('-')|('*')|('/')|('^')){
should be:
else if(postfix[i]=='+' || postfix[i]=='-' || .....
Comments
Post a Comment