比赛 |
20191211 |
评测结果 |
WWWWWWWWWW |
题目名称 |
表达式的值 |
最终得分 |
0 |
用户昵称 |
fmq03 |
运行时间 |
0.080 s |
代码语言 |
C++ |
内存使用 |
13.66 MiB |
提交时间 |
2019-12-11 21:33:12 |
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#define momo 10007
using namespace std;
int n;
stack<int> sta;
int main(){
freopen("exp.in","r",stdin);
freopen("exp.out","w",stdout);
cin>>n;
string str;
cin>>str;
queue<char>hz;
stack<char>ope;
for(int i=0;i<n;++i){
char k=str[i];
if(k=='*' || k=='+'){
if(i!=0 && str[i-1]==')'){
}else{
hz.push('0');
}
while(1){
if(k=='*'){
while(!ope.empty()&&ope.top()=='*'){
hz.push(ope.top());ope.pop();
}
ope.push('*');
break;
}else if(k=='+'){
if(!ope.empty()){
if(ope.top()!='*'){
ope.push(k);
}
break;
}else if(ope.empty()){
ope.push(k);
break;
}else{
hz.push(ope.top());ope.pop();
}
}
}
}else if(k=='('){
ope.push(k);
}else if(k==')'){
if(str[i-1]!=')'){
hz.push('0');
}
while(ope.top()!='('){
// cerr<<ope.top();
hz.push(ope.top());ope.pop();
}
ope.pop();
}
}
while(!ope.empty()){
hz.push(ope.top());ope.pop();
}
//while(!hz.empty()){
// cout<<hz.front();hz.pop();
// }
stack<long>sta0;
stack<long>sta1;
while(!hz.empty()){
char k=hz.front();hz.pop();
if(k=='0'){
sta0.push(1);
sta1.push(1);
}else if(k=='+'){
long a0=sta0.top();sta0.pop();
long b0=sta0.top();sta0.pop();
long a1=sta1.top();sta1.pop();
long b1=sta1.top();sta1.pop();
sta0.push(a0*b0);
sta1.push(a0*b1+a1*b0+a1*b1);
}else if(k=='*'){
long a0=sta0.top();sta0.pop();
long b0=sta0.top();sta0.pop();
long a1=sta1.top();sta1.pop();
long b1=sta1.top();sta1.pop();
sta0.push(a0*b0+a1*b0+a0*b1);
sta1.push(a1*b1);
}
}
cout<<sta0.top()<<endl;
return 0;
}