C++算术表达式求值编程
时间: 2023-09-06 14:08:17 浏览: 47
可以通过栈来实现算术表达式求值。具体步骤如下:
1. 创建两个栈,一个存放运算符,一个存放操作数。
2. 从左到右遍历表达式。
3. 如果当前字符为数字,则将其压入操作数栈中。
4. 如果当前字符为运算符,则比较其与栈顶运算符的优先级。
4.1 如果当前运算符优先级高于栈顶运算符,则将其压入运算符栈中。
4.2 否则,将栈顶运算符弹出并压入操作数栈中,直到当前运算符的优先级大于栈顶运算符。
5. 如果当前字符为左括号,则将其压入运算符栈中。
6. 如果当前字符为右括号,则依次弹出运算符栈中的运算符并压入操作数栈中,直到遇到左括号为止。
7. 当表达式遍历完后,如果运算符栈中还有运算符,则依次弹出运算符并压入操作数栈中。
8. 最后操作数栈中的唯一元素即为表达式的值。
下面是一个示例代码:
```c++
#include <iostream>
#include <stack>
using namespace std;
int priority(char op) { // 定义运算符优先级
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
case ')':
return 0;
default:
return -1;
}
}
int evaluate(string expr) {
stack<int> operands; // 存放操作数的栈
stack<char> operators; // 存放运算符的栈
int n = expr.length();
for (int i = 0; i < n; i++) {
char c = expr[i];
if (isdigit(c)) {
int num = 0;
while (i < n && isdigit(expr[i])) {
num = num * 10 + (expr[i] - '0');
i++;
}
i--;
operands.push(num);
} else if (c == '(') {
operators.push(c);
} else if (c == ')') {
while (!operators.empty() && operators.top() != '(') {
char op = operators.top();
operators.pop();
int b = operands.top();
operands.pop();
int a = operands.top();
operands.pop();
if (op == '+') operands.push(a + b);
else if (op == '-') operands.push(a - b);
else if (op == '*') operands.push(a * b);
else if (op == '/') operands.push(a / b);
}
operators.pop(); // 弹出左括号
} else {
while (!operators.empty() && priority(c) <= priority(operators.top())) {
char op = operators.top();
operators.pop();
int b = operands.top();
operands.pop();
int a = operands.top();
operands.pop();
if (op == '+') operands.push(a + b);
else if (op == '-') operands.push(a - b);
else if (op == '*') operands.push(a * b);
else if (op == '/') operands.push(a / b);
}
operators.push(c);
}
}
while (!operators.empty()) {
char op = operators.top();
operators.pop();
int b = operands.top();
operands.pop();
int a = operands.top();
operands.pop();
if (op == '+') operands.push(a + b);
else if (op == '-') operands.push(a - b);
else if (op == '*') operands.push(a * b);
else if (op == '/') operands.push(a / b);
}
return operands.top();
}
int main() {
string expr = "2*(3+4)-5/2";
int result = evaluate(expr);
cout << result << endl; // 输出15
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)