其他分享
首页 > 其他分享> > 表达式计算(双栈实现)

表达式计算(双栈实现)

作者:互联网

问题:计算下列表达式
  1+2*3-4-(2-(-1))*2+4/2
计算中包含+,-,*,/,(,),数字,负号(-)


1 #include <iostream> 2 #include <stack> 3 4 using namespace std; 5 6 bool isOp(string &val, int pos) { 7 if (val[pos] == '-') { 8 if (pos == 0 || isOp(val, pos-1)) { 9 return false; 10 } 11 } 12 char c = val[pos]; 13 return (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')'); 14 } 15 16 int getPriority(char c) { 17 if (c == '(' || c == ')') 18 return 0; 19 else if (c == '+' || c == '-') 20 return 1; 21 else if (c == '*' || c == '/') 22 return 2; 23 return 0; 24 } 25 26 int getResult(int a, int b, char op) { 27 if (op == '+') 28 return a + b; 29 else if (op == '-') 30 return a - b; 31 else if (op == '*') 32 return a * b; 33 else if (op == '/') 34 return a / b; 35 return 0; 36 } 37 38 void calculate(stack<int> &data, stack<char> &op) { 39 char ch = op.top(); 40 int behind = data.top(); 41 data.pop(); 42 int before = data.top(); 43 data.pop(); 44 data.push(getResult(before, behind, ch)); 45 op.pop(); 46 } 47 48 int main() { 49 string val; 50 cin >> val; 51 int result = 0; 52 stack<int> data; 53 stack<char> op; 54 string tmp = ""; 55 for (int i = 0; i < val.length(); ++i) { 56 if (isOp(val, i) ) { 57 if (!tmp.empty()) { 58 data.push(atoi(tmp.c_str())); 59 tmp.clear(); 60 } 61 if (op.empty() || val[i] == '(') { 62 op.push(val[i]); 63 } else { 64 bool bKuoHao = false; 65 while (!op.empty() && getPriority(op.top()) >= getPriority(val[i])) { 66 if (val[i] == ')') { 67 while(!op.empty() && op.top() != '(') { 68 calculate(data, op); 69 } 70 op.pop(); 71 bKuoHao = true; 72 break; 73 } else { 74 calculate(data, op); 75 } 76 } 77 if (!bKuoHao) 78 op.push(val[i]); 79 } 80 } else { 81 tmp += val[i]; 82 if (i == val.length() - 1) { 83 data.push(atoi(tmp.c_str())); 84 tmp.clear(); 85 } 86 } 87 } 88 while(!op.empty()) { 89 calculate(data, op); 90 } 91 cout << data.top() << endl; 92 93 return 0; 94 }

 

上面的不支持浮点数的计算,所以这里再调整一下数据类型

 1 #include <iostream>
 2 #include <stack>
 3 
 4 using namespace std;
 5 
 6 bool isOp(string &val, int pos) {
 7     if (val[pos] == '-') {
 8         if (pos == 0 || isOp(val, pos-1)) {
 9             return false;
10         }
11     }
12     char c = val[pos];
13     return (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')');
14 }
15 
16 int getPriority(char c) {
17     if (c == '(' || c == ')')
18         return 0;
19     else if (c == '+' || c == '-') 
20         return 1;   
21     else if (c == '*' || c == '/')
22         return 2;
23     return 0;
24 }
25 
26 double getResult(double a, double b, char op) {
27     if (op == '+')
28         return a + b;
29     else if (op == '-')
30         return a - b;
31     else if (op == '*')
32         return a * b;
33     else if (op == '/')
34         return a / b;
35     return 0;
36 }
37 
38 void calculate(stack<double> &data, stack<char> &op) {
39     char ch = op.top();
40     double behind = data.top();
41     data.pop();
42     double before = data.top();
43     data.pop();
44     data.push(getResult(before, behind, ch));
45     op.pop();
46 }
47 
48 int main() {
49     string val;
50     cin >> val;
51     int result = 0;
52     stack<double> data;
53     stack<char> op;
54     string tmp = "";
55     for (int i = 0; i < val.length(); ++i) {
56         if (isOp(val, i) ) {
57             if (!tmp.empty()) {
58                 data.push(atof(tmp.c_str()));
59                 tmp.clear();
60             }
61             if (op.empty() || val[i] == '(') {
62                 op.push(val[i]);
63             } else {
64                 bool bKuoHao = false;
65                 while (!op.empty() && getPriority(op.top()) >= getPriority(val[i])) {
66                     if (val[i] == ')') {
67                         while(!op.empty() && op.top() != '(') {
68                             calculate(data, op);
69                         }
70                         op.pop();
71                         bKuoHao = true;
72                         break;
73                     } else {
74                         calculate(data, op);
75                     }
76                 }
77                 if (!bKuoHao)
78                     op.push(val[i]);
79             }
80         } else {
81             tmp += val[i];
82             if (i == val.length() - 1) {
83                 data.push(atof(tmp.c_str()));
84                 tmp.clear();
85             }
86         }
87     }
88     while(!op.empty()) {
89         calculate(data, op);
90     }
91     cout << data.top() << endl;
92 
93     return 0;
94 }

 

 

 

  

标签:return,val,int,双栈,else,计算,data,表达式,op
来源: https://www.cnblogs.com/zllwxm123/p/16408583.html