有人能帮忙看一下有没有错吗(支持小括号、+-*/) 或者告诉我什么算式计算结果错误也行。
应为代码长,不要放在洛谷在线IDE里面跑(卡评测机),编译大约需要10秒左右(悲
#include<bits/stdc++.h>
using namespace std;
map<char, int> mp;
int res(string s){
stack <int> a;
int len = s.length();
//cout << s << endl;
//return 0;
cout << "开始计算后缀表达式" << endl;
for(int i = 0;i < len;i++){
if(s[i] == '.'){
continue;
}else if(s[i] == '+'){
int y = a.top();
a.pop();
int x = a.top();
a.pop();
a.push(x+y);
}else if(s[i] == '-'){
int y = a.top();
a.pop();
int x = a.top();
a.pop();
a.push(x-y);
}else if(s[i] == '*'){
int y = a.top();
a.pop();
int x = a.top();
a.pop();
a.push(x*y);
}else if(s[i] == '/'){
int y = a.top();
a.pop();
int x = a.top();
a.pop();
a.push(x/y);
}else{
string num = "";
while(s[i] != '.'){
num += s[i];
i++;
}
cout << "数字处理 字符型数字:" <<num << ",数字型数字:" ;
int numlen = num.length();
int j = numlen-1;
int sum = 0;
for(int i = 0;i < numlen;i++,j--){
sum += (num[i]-'0')*pow(10,j);
}
cout << sum << "。\n";
a.push(sum);
}
}
return a.top();
}
string make(string s){
string res = "",x = "";
stack <char> a;
int len = s.length();
cout << "开始制作表达式" << endl;
for(int i = 0;i < len;i++){
cout << s[i] << endl;
if(s[i] <= '9' && s[i] >= '0'){
x += s[i];
}else{
res += '.'+x + '.';
x = "";
if(s[i] == '+' || s[i] == '-'){
if(s[i] == '+'){
if(a.empty()){
a.push(s[i]);
}else if(a.top() == '('){
a.push(s[i]);
}else{
while(1){
if(a.empty()){
break;
}else if(mp[a.top()] > 1){
res += a.top();
a.pop();
}else{
break;
}
}
}
}
if(s[i] == '-'){
if(a.empty()){
a.push(s[i]);
}else if(a.top() == '('){
a.push(s[i]);
}else{
while(1){
if(a.empty()){
break;
}else if(mp[a.top()] > 1){
res += a.top();
a.pop();
}else{
break;
}
}
}
}
}else if(s[i] == '*' || s[i] == '/'){
if(s[i] == '*'){
if(a.empty()){
a.push(s[i]);
}else if(a.top() == '(' || a.top() == '+' || a.top() == '-'){
a.push(s[i]);
}else{
res += s[i];
}
}
if(s[i] == '/'){
if(a.empty()){
a.push(s[i]);
}else if(a.top() == '(' || a.top() == '+' || a.top() == '-'){
a.push(s[i]);
}else{
res += s[i];
}
}
}else if(s[i] == '('){
a.push(s[i]);
}else{
while(a.top() != '('){
res += a.top();
a.pop();
}
a.pop();
}
}
}
res += '.'+x+'.';
while(!a.empty()){
res += a.top();
a.pop();
}
return res;
}
int main(){
cout << "开始" << endl;
mp['+'] = 1;
mp['-'] = 1;
mp['*'] = 2;
mp['/'] = 2;
mp['('] = 0;
string a,b;
cout << "赋值完成" << endl;
cin >> a;
b = make(a);
cout << "后缀表达式制作完成,表达式为" << b << endl;
cout << res(b) << endl;
cout << "程序结束" << endl;
return 0;
}