ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

表达式计算(双栈实现)

2022-06-24 14:03:18  阅读:120  来源: 互联网

标签:return val int 双栈 else 计算 data 表达式 op


问题:计算下列表达式
  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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有