陈柏诚 • 2年前
求大佬帮助
#include<bits/stdc++.h>
using namespace std;
stack<double> num;
stack<char> op;
map<char,int> h {{'+',1},{'-',1},{'*',2},{'/',2}};
void fucker()
{
int a = num.top();
num.pop();
int b = num.top();
num.pop();
char p = op.top();
op.pop();
int r = 0;
if (p=='+') r = b + a;
if (p=='-') r = b - a;
if (p=='*') r = b * a;
if (p=='/') r = b / a;
num.push(r);
}
int main()
{
string s;
cin>>s;
for(int i = 0; i < s.size(); i++)
{
if(isdigit(s[i]))
{
int x = 0, j = i;
while(j <s.size() && isdigit(s[j]))
{
x = x * 10 + s[j] - '0';
j++;
}
num.push(x);
i = j - 1;
}
else if(s[i] == '(')
{
op.push(s[i]);
}
else if(s[i] == ')')
{
while(op.top() != '(')
fucker();
op.pop();
}
else
{
while(op.size() && h[op.top()] >= h[s[i]])
fucker();
op.push(s[i]);
}
}
while(op.size()) fucker();
cout << fixed << setprecision(2) << num.top() << endl;
return 0;
}
评论: