Infix expression value, infix expression
Infix expression value(Expr. cpp)[Problem description]Enter an infix expression (consisting of 0-9 Arithmetic Operators, plus + subtraction-multiplication * Division/operators, left and right parentheses. Note that "-" can also be used as a negative sign, and the expression uses "@" as the terminator) to determine whether the expression is legal. If it is invalid, output "NO "; otherwise, convert the expression to the suffix format, and then obtain the value of the suffix expression and output it. Note: The stack operation is required and the value of the expression cannot be output directly. What if it is a real number operation? [Input file]ExprThe first line of the. in input file is a string ending. [Output file]Expr.OutIf the expression is invalid, output "NO" in uppercase. If the expression is valid, output the calculation result. [Input sample] 1 + 2 × 8-9 [output sample] 8'
1 # include & lt; iostream & gt; 2 # include & lt; cmath & gt; 3 # include & lt; cstdio & gt; 4 # include & lt; cstring & gt; 5 using namespace std; 6 int number [101], I = 0, p = 1; 7 char symbol [101], s [256], t [256]; 8 void push () // operator import stack 9 {10 symbol [++ p] = s [I]; 11} 12 void pop () // operator stack top element output stack, and fetch the operand stack element to complete the corresponding operation 13 {14 switch (symbol [p --]) // after the operation is complete, discard the operator, it also indicates that the number of operations to be performed has completed 15 {16 case '+': 17 {18 number [p] + = number [p + 1]; 19 break; 20} 21 case '-': 22 {23 number [p]-= number [p + 1]; 24 break; 25} 26 case '*': 27 {28 number [p] * = number [p + 1]; 29 break; 30} 31 case '/': 32 {33 number [p]/= number [p + 1]; 34 break; 35} 36 case '^ ': 37 {38 number [p] = pow (number [p], number [p + 1]); 39} 40} 41} 42 bool can () // determine the priority of operators and create a flag function, can the computation be performed 43 {44 if (s [I] = '+' | s [I] = '-') & symbol [p]! = '(') // 45 return 1 in parentheses; 46 if (s [I] = '*' | s [I] = '/') & (symbol [p] = '*' | symbol [p] = '/')) 47 // If multiplication and division p happens to be multiplication and division 48 return 1; 49 return 0; 50} 51 bool judge (char s [256]) 52 {53 int top = 0, I = 0; 54 while (s [I]! = '@') 55 {56 if (s [I] = '(') top ++; 57 if (s [I] = ')') 58 {59 if (top> 0) top --; 60 else return 0; 61} 62 I ++; 63} 64 if (top! = 0) return 0; // check whether the stack is empty. If it is not null, there are unmatched parentheses 65 else return 1; 66} 67 int main () 68 {69 gets (s); 70 if (judge (s) = 0) 71 {72 cout <"NO"; 73 return 0; 74} 75 76 s [strlen (s)] = ')'; 77 symbol [p] = '('; 78 while (I <strlen (s) 79 {80 while (s [I] = '(') // The left bracket is processed and pushed into the left bracket, you can match the left bracket with the right bracket. while (symbol [p]! = '(') 81 {82 push (); 83 I ++; 84} 85 int x = 0; 86 while (s [I]> = '0' & s [I] <= '9 ') // number of input operations stack 87 x = x * 10 + s [I ++]-'0'; 88 number [p] = x; 89 do 90 {91 if (s [I] = ') // process 92 {93 while (symbol [p] when there is a right bracket behind the right bracket. = '(') 94 pop (); // when there is still a formula in the brackets that is not completed, calculate 95 number [-- p] = number [p + 1]; // when the operation is complete, the left parenthesis is meaningless, so the pointer p is moved forward to an operator, and copy the result 96} 97 else 98 {// according to the flag function value for the operator into the stack or out of the stack operation 99 while (can () 100 pop (); // run the computation and press the 101 push (); 102} 103 I ++; 104} while (I <strlen (s) & s [I-1] = '); // when the right brace is detected, you can calculate the content in the brace 105} 106 printf ("Result = % d ", number [0]); // because all operations are built in parentheses, the number of operations to be performed decreases with the number of operators) and the final p --, the result must be saved in number [0] 107 return 0; 108}