#includeusing namespace std;#include#include#include#include#includeconst int maxn = 100020;stacknum; //数字栈stackop; //运算符栈void eval() //用于中缀表达式计算{ auto b = num.top(); //考虑运算顺序,先取出来的是第二个运算数 num.pop(); auto a = num.top(); num.pop(); auto c = op.top(); op.pop(); double x = 0; if (c == '+')x = a + b; else if (c == '-')x = a - b; else if (c == '*')x = a * b; else x = a / b; num.push(x);} bool eval_(char c) //用于后缀表达式{ auto b = num.top(); //考虑运算顺序,先取出来的是第二个运算数 num.pop(); auto a = num.top(); num.pop(); int x = 0; if (c == '+')x = a + b; else if (c == '-')x = a - b; else if (c == '*')x = a * b; else { if (b == 0) { cout << "Error: " << a << "/0"; return false; } x = a / b; } num.push(x); return true;}//中缀表达式求值函数void evalmerge(string str){ unordered_mappr{ {'+',1},{'-',1},{'*',2},{'/',2} }; //定义一个哈希表,存储优先级 for (int i = 0; i < str.size(); i++) { auto c = str[i]; if (isdigit(c)||c=='-'&&(i==0||str[i-1]=='(')) { int flag = 1, xiaoshuwei = 1; double x = c - '0', j = i + 1; if (c == '-') { x = 0; } while (j = pr[c])//符号栈没空,并且栈顶的运算符优先级大于当前运算符 { eval(); //操作栈顶的运算符 } op.push(c); //新的运算符压入栈 } } while (op.size()) //把最后剩下的运算符操作完 { eval(); } cout << num.top();}//后缀表达式求值函数,以#结尾,每个字符用空格隔开 负数粘在一起void evallast(string str){ for (int i = 0; i < str.size() - 1; i++) { auto c = str[i]; if (isdigit(c) || (c == '-' && i + 1 < str.size() - 1 && str[i + 1] != ' ')) { int x = c - '0', j = i + 1; if (c == '-') { x = 0; } while (j < str.size() - 1 && isdigit(str[j])) { x = x * 10 + str[j++] - '0'; //这里的j是要自增的 } i = j - 1; //别忘记了更新i if (c == '-') { x = -x; } num.push(x); }//取出数字 else if (c != ' ') { if (num.size() < 2) { cout << "Expression Error: " << num.top(); return; } else { bool flag = eval_(c); if (!flag) { return; } } } } if (num.size() == 1) cout < 1) { cout << "Expression Error: " << num.top(); } } }//中缀转后缀string transe(string str) { string ans; //存储结果表达式 stackshu; stackfu; unordered_mappr{ {'+',1},{'-',1},{'*',2},{'/',2} }; //定义一个哈希表,存储优先级 for (int i = 0; i < str.size(); i++) { auto c = str[i]; if (isdigit(c) || c == '-' && (i == 0 || str[i - 1] == '(')) { string temp; temp += c; int x = c - '0', j = i + 1; while (j = pr[c])//符号栈没空,并且栈顶的运算符优先级大于当前运算符 { auto temp = fu.top(); fu.pop(); ans += temp; ans += " "; } fu.push(c); //新的运算符压入栈 } } while (fu.size()) //把最后剩下的运算符操作完 { auto temp = fu.top(); fu.pop(); ans += temp; ans += " "; } ans += '#'; return ans;}int main(){ string s; cin >> s; evalmerge(s); cout << endl; while (!num.empty()) { num.pop(); } while (!op.empty()) { op.pop(); } string ss = transe(s); cout << ss << endl; return 0;}