表达式求值c++
时间: 2023-08-03 22:13:57 浏览: 38
在C++中,可以使用栈来实现表达式求值。具体的步骤如下:
1. 定义一个栈,用来存储数字和运算符。
2. 遍历表达式的每一个字符,如果是数字则直接压入栈中,如果是运算符则判断与栈顶元素的优先级,如果当前运算符的优先级大于等于栈顶元素的优先级,则将当前运算符压入栈中;否则,将栈顶元素弹出并进行运算,直到当前运算符的优先级大于等于栈顶元素的优先级。
3. 遍历完成后,如果栈中还有元素,则依次弹出并进行运算,直到栈为空。
4. 最终栈顶元素即为表达式的结果。
下面是一个简单的代码实现:
```c++
#include <iostream>
#include <stack>
using namespace std;
int calculate(int a, int b, char op) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
default: return 0;
}
}
int evaluate(string expr) {
stack<int> numStack;
stack<char> opStack;
int num = 0;
bool hasNum = false;
for (int i = 0; i < expr.length(); i++) {
char c = expr[i];
if (isdigit(c)) {
num = num * 10 + c - '0';
hasNum = true;
} else {
if (hasNum) {
numStack.push(num);
num = 0;
hasNum = false;
}
if (c == '+' || c == '-') {
while (!opStack.empty() && opStack.top() != '(') {
int b = numStack.top(); numStack.pop();
int a = numStack.top(); numStack.pop();
numStack.push(calculate(a, b, opStack.top()));
opStack.pop();
}
opStack.push(c);
} else if (c == '*' || c == '/') {
while (!opStack.empty() && (opStack.top() == '*' || opStack.top() == '/')) {
int b = numStack.top(); numStack.pop();
int a = numStack.top(); numStack.pop();
numStack.push(calculate(a, b, opStack.top()));
opStack.pop();
}
opStack.push(c);
} else if (c == '(') {
opStack.push(c);
} else if (c == ')') {
while (!opStack.empty() && opStack.top() != '(') {
int b = numStack.top(); numStack.pop();
int a = numStack.top(); numStack.pop();
numStack.push(calculate(a, b, opStack.top()));
opStack.pop();
}
opStack.pop();
}
}
}
if (hasNum) {
numStack.push(num);
}
while (!opStack.empty()) {
int b = numStack.top(); numStack.pop();
int a = numStack.top(); numStack.pop();
numStack.push(calculate(a, b, opStack.top()));
opStack.pop();
}
return numStack.top();
}
int main() {
string expr;
cout << "请输入表达式:";
cin >> expr;
int result = evaluate(expr);
cout << "结果为:" << result << endl;
return 0;
}
```
注意:这个代码只支持整数运算,不支持浮点数运算。如果要支持浮点数运算,可以将`int`改为`double`。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)