Below is a function post fixEval that is supposed to read a queue of char tokens and evaluates them as postfix notation
Posted: Sat May 14, 2022 8:18 pm
Below is a function post fixEval that is supposed to read a queue of char tokens and evaluates them as postfix notation (assuming there are only has * and + operations). If the postfix expression isn't formatted correctly it should print a warning to the user and return -1. Otherwise you should return value produced by the expression. The program should not leak any memory. Example: input: 5 3 2*1+ * output: 35 Since 5* ((3 * 2) + 1) = 5* (6+1) = 5 * 7 = 35 1 int *postfixEval( Queue *pq ){ Stack ps = createStack(); 2 3 4 while( isEmpty (pq) ) { char sym = dequeue ( ps); 5 6 7 if( sym != '+ || sym != '*') push( ps, next ); 8 9 10 11 12 13 else{ char y = pop( ps); char x = pop( ps ); if ( sym == "+" ) push( ps, x+y); else push( ps, x*y); } 14 15 16 17 18 } 19 20 int ret = top( ps); 21 22 23 if( !empty(ps)){ scanf( "Postfix expression formatted incorrectly\n"); return -1; } 24 25 26 27 freeQueue( pq); return ret; 28 29 }