Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
118884 | 梁颢城 | 后序表达式 | C++ | 解答错误 | 0 | 0 MS | 260 KB | 1092 | 2024-01-02 13:17:18 |
#include<iostream> #include<cstring> #include<stack> #include<algorithm> #include<unordered_map> using namespace std; stack<double> num; stack<char> op; unordered_map<char ,float> pr={{'+',1},{'-',1},{'/',2},{'*',2}}; void evel(){ auto x=num.top();num.pop(); auto y=num.top();num.pop(); auto z=op.top();op.pop(); if(z=='+')num.push(x+y); if(z=='-')num.push(x-y); if(z=='*')num.push(x*y); if(z=='/')num.push(x/y); } int main(){ string s; cin>>s; for(int i=0;i<s.size();i++) { if(isdigit(s[i])) { int j=i; int sum=0; while(j<s.size()&&isdigit(s[j]))sum=sum*10+s[j++]-'0'; i=j-1; num.push(sum); } else if(s[i]=='(') op.push(s[i]); else if(s[i]==')'){ while(op.top()!='(') evel(); op.pop(); } else { while(op.size()!=0&&pr[op.top()]>=pr[s[i]])evel(); op.push(s[i]); } } while(op.size()) evel(); printf("%2.lf",num.top()); }