问题 : 负号与数字的识别
负号 - 有两种含义:
- 作为运算符。
- 作为负数的前缀。
目前的代码无法区分这两种情况,比如在处理 "-1" 或 "-1*(-1-1)" 时,负号会被当作运算符,而不是负数的一部分。
修改思路
我们需要在解析表达式时判断 - 是前缀负号还是运算符:
- 如果 - 出现在数字前,并且前面是起始位置或开括号 (,则它是负号。
- 否则,它是减法运算符。
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <cctype> // for isdigit
using namespace std;
int main() {
string a;
while (cin >> a) { // 注意 while 处理多个 case
vector<int> vec; // 存储中缀表达式
for (int i = 0; i < a.length(); ) {
if (isdigit(a[i]) || (a[i] == '-' && (i == 0 || a[i - 1] == '('))) {
// 处理数字,包括负数
int j = i + 1;
while (j < a.length() && isdigit(a[j])) ++j;
int x = stoi(a.substr(i, j - i));
vec.push_back(x);
i = j;
} else {
// 处理运算符和括号
vec.push_back(a[i]);
i++;
}
}
// cout << "中缀表达式: ";
// for (auto &x : vec) {
// if (x < 0 || isdigit(x)) {
// cout << x << " ";
// } else {
// cout << char(x) << " ";
// }
// }
// cout << endl;
// 中缀转后缀
stack<int> stk;
vector<int> back; // 存储后缀表达式
for (auto &p : vec) {
if (p != '*' && p != '(' && p != ')' && p != '/' && p != '+' && p != '-') {
back.push_back(p);
} else {
switch (p) {
case '(':
stk.push(p);
break;
case ')':
while (!stk.empty() && stk.top() != '(') {
back.push_back(stk.top());
stk.pop();
}
stk.pop(); // 弹出 '('
break;
case '+': case '-':
while (!stk.empty() && (stk.top() == '+' || stk.top() == '-' || stk.top() == '*' || stk.top() == '/')) {
back.push_back(stk.top());
stk.pop();
}
stk.push(p);
break;
case '*': case '/':
while (!stk.empty() && (stk.top() == '*' || stk.top() == '/')) {
back.push_back(stk.top());
stk.pop();
}
stk.push(p);
break;
}
}
}
while (!stk.empty()) {
back.push_back(stk.top());
stk.pop();
}
// cout << "后缀表达式: ";
// for (auto &x : back) {
// if (x < 0 || isdigit(x)) {
// cout << x << " ";
// } else {
// cout << char(x) << " ";
// }
// }
// cout << endl;
// 计算后缀表达式的值
stack<int> calc;
for (auto &x : back) {
if (x != '+' && x != '-' && x != '*' && x != '/') {
calc.push(x);
} else {
int b = calc.top(); calc.pop();
int a = calc.top(); calc.pop();
switch (x) {
case '+': calc.push(a + b); break;
case '-': calc.push(a - b); break;
case '*': calc.push(a * b); break;
case '/': calc.push(a / b); break;
}
}
}
// cout << "计算结果: " << calc.top() << endl;
cout << calc.top() << endl;
}
}



京公网安备 11010502036488号