/************** Inverse Polish-suffix notation preprocessing-> infix to postfix algorithm: while (the expression is not empty) if (when an operand is encountered) directly outputs else if (when an operator op is encountered) OP is (directly in the stack s op is) s. push the output until (OP is an arithmetic operation, if (S is empty | S. top is (| op with a higher priority than S. top) OP into the stack else S. push output if (! S. empty) s. push output computing algorithm: If (encountering operands) into Stack s else (encountering operator) s. push two elements for calculation, result: The stack is *******************/# include <iostream >#include <stack> using namespace STD; typedef struct tagcalc {bool is_op; Union {char op; int value ;};} calc; int main () {stack <calc> S; calc; int ch; while (1) {While (CH = getchar () & Ch! = '\ N') {If (null = strchr ("+-*/", CH) // operand {If (strchr ("()", CH) = NULL) printf ("% C", CH); If ('= CH) {Calc. is_op = 1; Calc. OP = CH; S. push (calc);} else if (')' = CH) {While (! S. empty () {calc = S. top (); S. pop (); If ('= Calc. OP) break; printf ("% C", Calc. OP) ;}} else {If (S. empty () | S. top (). OP = '(' | (null! = Strchr ("*/", CH) & null! = Strchr ("+-", S. top (). OP) {Calc. is_op = true; Calc. OP = CH; S. push (calc);} else {calc = S. top (); S. pop (); printf ("% C", Calc. OP) ;}}while (! S. empty () {calc = S. top (); S. pop (); printf ("% C", Calc. OP) ;}printf ("\ n ");}}
Inverse Polish formula and expression solving