[] OJ infix expression evaluation

Postfix expression Review: https://blog.csdn.net/ssjhust123/article/details/8001651

Title Contents:

By integration "infix turn suffix" and "suffix evaluation" two arithmetic functions with (non-simple sequence call),

Direct evaluation of the infix expression, a new algorithm or scan from left to right infix expression,

But the use of two stacks, a temporary operator, a temporary storage operand to be evaluated.

(Support + - * / ^ five kinds of operations)

 

Input formats:

Total 1 row for a character string, i.e., a infix expressions,

Wherein each inter numerals or symbols separated by a space.

 

Output formats:

Of 1 line, is an integer, i.e., the result of evaluating.

 

Sample input:

( 2 + 3 ) * 6 + 4 / 2

 

Sample output:

32

note:

1. Connect the digital characters

2.pow (int, int) on dev through, but the correct format pow (int, double)

  1 #include <iostream>
  2 #include <stack>
  3 #include <queue>
  4 #include <cmath>
  5 using namespace std;
  6 void Pop1(stack<char> &x, queue<char> &y){
  7     while (!x.empty() && (x.top() == '+' || x.top() == '-' || x.top() == '*' || x.top() == '/' || x.top() == '^')){
  8         y.push(x.top());
  9         x.pop();
 10     }
 11 }
 12 void Pop2(stack<char> &x, queue<char> &y){
 13     while (!x.empty() && (x.top() == '*' || x.top() == '/' || x.top() == '^')){
 14         y.push(x.top());
 15         x.pop();
 16     }
 17 }
 18 void Pop3(stack<char> &x, queue<four> &y){
 19     while (x.top() != '('){
 20         y.push(x.top());
 21         x.pop();
 22     }
 23     x.pop();
 24 }
 25 void Pop4(stack<char> &x, queue<char> &y){
 26     while (!x.empty() && x.top() == '^'){
 27         y.push(x.top());
 28         x.pop();
 29     }
 30 }
 31 int main(){
 32     char infix[100];
 33     char a;
 34     int b, c;
 35     int sign = 0; 
 36     stack<char> my_symbol;
 37     queue<char> my_suffix;
 38     stack<int> my_cal;
 39     cin.getline(infix, 100);
 40     for (int i = 0; i < 100; ++i){
 41         a = infix[i];
 42         if (a == ' '){
 43             sign = 0;
 44             continue;
 45         }
 46         if (a == '\0')
 47             break;
 48         if ('0' <= a && a <= '9'){
 49             my_suffix.push(a);
 50             if (sign != 0)
 51                 my_suffix.push('@');
 52             sign++;
 53             continue;
 54         }
 55         switch(a){
 56             case '(': my_symbol.push(a); break;
 57             case '+':
 58             case '-': Pop1(my_symbol, my_suffix); my_symbol.push(a); break; 
 59             case '*':
 60             case '/': Pop2(my_symbol, my_suffix); my_symbol.push(a); break;
 61             case ')': Pop3(my_symbol, my_suffix); break; 
 62             case '^': Pop4(my_symbol, my_suffix); my_symbol.push(a); break;
 63         }
 64         sign = 0;
 65     }
 66     while (!my_symbol.empty()){
 67         my_suffix.push(my_symbol.top());
 68         my_symbol.pop();
 69     }
 70     while (!my_suffix.empty()){
 71         a = my_suffix.front();
 72         my_suffix.pop();
 73         if ('0' <= a && a <= '9'){
 74             my_cal.push(a - '0');
 75             continue;
 76         }
 77         b = my_cal.top();
 78         my_cal.pop();
 79         c = my_cal.top();
 80         my_cal.pop();
 81         switch(a){
 82             case '+':
 83                 my_cal.push(c + b);
 84                 break;
 85             case '-':
 86                 my_cal.push(c - b);
 87                 break;
 88             case '*':
 89                 my_cal.push(c * b);
 90                 break;
 91             case '/':
 92                 my_cal.push(c / b);
 93                 break;
 94             case '@':
 95                 my_cal.push(c * 10 + b);
 96                 break;
 97             case '^' :
 98                 my_cal.push(pow(c, (double)b));
 99                 break;
100         }
101     }
102     cout << my_cal.top() << endl;
103     return 0;
104 }

 

Guess you like

Origin www.cnblogs.com/victorique-de-blois/p/11655914.html