题意:表达式求值
思路:栈的运用。
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
using namespace std;
const int maxn=500;
char ch[maxn];
void calc(char *ch){
stack<int>s; //数字
stack<char>ss; // 运算符号
stack<char>sss; //比较符号
int len=strlen(ch);
for(int i=0; i<len; i++){
if(ch[i]==',') continue;
if(ch[i]=='m') continue;
if(ch[i]=='a' && ch[i+1]=='d') {
sss.push(ch[i+1]); i+=2;
}else if(ch[i]=='i'){
sss.push(ch[i]); i+=1;
}else if(ch[i]=='a' && ch[i+1]=='x'){
sss.push(ch[i]); i+=1;
}
else if(ch[i]=='('){
ss.push(ch[i]);
}
else if(ch[i]==')'){
int xx=s.top();
s.pop();
int yy=s.top();
s.pop();
if(sss.top()=='i'){
s.push(min(xx,yy));
}else if(sss.top()=='d'){
s.push(xx+yy);
}else if(sss.top()=='a') {
s.push(max(xx,yy));
}
sss.pop();
ss.pop();
}
else if(ch[i]>='0' && ch[i]<='9'){
int xx=0;
while(ch[i]>='0' && ch[i]<='9'){
xx=xx*10+ch[i]-'0'; i++;
}
i=i-1;
s.push(xx);
}
}
printf("%d\n",s.top());
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s",ch);
calc(ch);
}
return 0;
}