c++输入中缀、后缀、前缀表达式求值的代码
时间: 2023-07-10 17:23:10 浏览: 102
C++中缀表达式求值
5星 · 资源好评率100%
下面是C++中求解中缀表达式、后缀表达式和前缀表达式的代码,其中包括了表达式求值的算法实现:
1. 求解中缀表达式:
```c++
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int priority(char c) { // 定义运算符优先级
if (c == '*' || c == '/') {
return 2;
} else if (c == '+' || c == '-') {
return 1;
} else {
return 0;
}
}
double calculate(double a, double b, char c) { // 定义运算符操作
switch (c) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
return 0;
}
}
double evaluateInfix(string expression) { // 中缀表达式求值
stack<double> nums; // 存放数字
stack<char> ops; // 存放运算符
int len = expression.length();
for (int i = 0; i < len; i++) {
char c = expression[i];
if (isdigit(c)) { // 如果是数字
double num = 0;
while (i < len && isdigit(expression[i])) {
num = num * 10 + (expression[i] - '0');
i++;
}
nums.push(num);
i--;
} else if (c == '(') { // 如果是左括号
ops.push(c);
} else if (c == ')') { // 如果是右括号
while (!ops.empty() && ops.top() != '(') {
double b = nums.top();
nums.pop();
double a = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
nums.push(calculate(a, b, op));
}
ops.pop();
} else { // 如果是运算符
while (!ops.empty() && priority(ops.top()) >= priority(c)) {
double b = nums.top();
nums.pop();
double a = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
nums.push(calculate(a, b, op));
}
ops.push(c);
}
}
while (!ops.empty()) { // 处理剩余的运算符
double b = nums.top();
nums.pop();
double a = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
nums.push(calculate(a, b, op));
}
return nums.top();
}
int main() {
string expression = "1+2*3-4/2";
double result = evaluateInfix(expression);
cout << expression << " = " << result << endl;
return 0;
}
```
2. 求解后缀表达式:
```c++
#include <iostream>
#include <stack>
#include <string>
using namespace std;
double calculate(double a, double b, char c) { // 定义运算符操作
switch (c) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
return 0;
}
}
double evaluatePostfix(string expression) { // 后缀表达式求值
stack<double> nums; // 存放数字
int len = expression.length();
for (int i = 0; i < len; i++) {
char c = expression[i];
if (isdigit(c)) { // 如果是数字
double num = 0;
while (i < len && isdigit(expression[i])) {
num = num * 10 + (expression[i] - '0');
i++;
}
nums.push(num);
i--;
} else { // 如果是运算符
double b = nums.top();
nums.pop();
double a = nums.top();
nums.pop();
nums.push(calculate(a, b, c));
}
}
return nums.top();
}
int main() {
string expression = "123*+4-2/";
double result = evaluatePostfix(expression);
cout << expression << " = " << result << endl;
return 0;
}
```
3. 求解前缀表达式:
```c++
#include <iostream>
#include <stack>
#include <string>
using namespace std;
double calculate(double a, double b, char c) { // 定义运算符操作
switch (c) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
return 0;
}
}
double evaluatePrefix(string expression) { // 前缀表达式求值
stack<double> nums; // 存放数字
int len = expression.length();
for (int i = len - 1; i >= 0; i--) {
char c = expression[i];
if (isdigit(c)) { // 如果是数字
double num = 0;
int j = i;
while (j >= 0 && isdigit(expression[j])) {
num = num + (expression[j] - '0') * pow(10, i - j);
j--;
}
nums.push(num);
i = j + 1;
} else { // 如果是运算符
double a = nums.top();
nums.pop();
double b = nums.top();
nums.pop();
nums.push(calculate(a, b, c));
}
}
return nums.top();
}
int main() {
string expression = "-+*123/84";
double result = evaluatePrefix(expression);
cout << expression << " = " << result << endl;
return 0;
}
```
阅读全文