/*表达式求值
输入
3
12+2*3
12*(2+3)
12*(2+3)+Smax(333,220+280)
输出
18
60
69
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stack>
using namespace std;
stack<char> s1;//字符栈
stack<int> s2; //数据栈
char s[1010];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,j;
getchar();
scanf("%s",&s[1]);
int len = strlen(&s[1]);
s[0] = '(';
s[len+1] = ')';
len++;
for(i=0;i<=len;i++)
{
if(s[i]=='(')
{
s1.push(s[i]);
}
else if(s[i]=='S')
{
s1.push('(');
s1.push('S');
i += 4;
}
else if(s[i]>='0' && s[i]<='9')
{
int v = 0;
while(s[i]>='0' && s[i]<='9')
{
v = v*10+(s[i++]-'0');
}
i--;
s2.push(v);
}
else if(s[i]=='+' || s[i]=='-')
{
while(s1.top()!='(' && s1.top()!='S')
{
int a = s2.top(); s2.pop();
int b = s2.top(); s2.pop();
int c;
switch(s1.top())
{
case '+': c = b + a; break;
case '-': c = b - a; break;
case '*': c = b * a; break;
case '/': c = b / a; break;
}
s2.push(c);
s1.pop();
}
s1.push(s[i]);
}
else if(s[i]=='*' || s[i]=='/')
{
if(s1.top()=='*' )
{
int a = s2.top(); s2.pop();
int b = s2.top(); s2.pop();
s2.push(b*a);
s1.pop();
}
else if(s1.top()=='/')
{
int a = s2.top();s2.pop();
int b = s2.top();s2.pop();
s2.push(b/a);
s1.pop();
}
s1.push(s[i]);
}
else if(s[i]==')')
{
while(s1.top()!='(' && s1.top()!='S')
{
int a = s2.top();s2.pop();
int b = s2.top();s2.pop();
int c,da = 0 ,db = 0;
switch(s1.top())
{
case '+': c = b + a; break;
case '-': c = b - a; break;
case '*': c = b * a; break;
case '/': c = b / a; break;
}
s2.push(c);
s1.pop();
}
if(s1.top()=='S')
{
int a=s2.top();s2.pop();
int b=s2.top();s2.pop();
int c,da = 0 ,db = 0;
while(b!=0)
{
db += b%10;
b /= 10;
}
while(a!=0)
{
da += a%10;
a /= 10;
}
c = max(da,db);
s2.push(c);
s1.pop();
}
s1.pop();
}
}
printf("%d\n",s2.top());
s2.pop();
}
return 0;
}
输入
3
12+2*3
12*(2+3)
12*(2+3)+Smax(333,220+280)
输出
18
60
69
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stack>
using namespace std;
stack<char> s1;//字符栈
stack<int> s2; //数据栈
char s[1010];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,j;
getchar();
scanf("%s",&s[1]);
int len = strlen(&s[1]);
s[0] = '(';
s[len+1] = ')';
len++;
for(i=0;i<=len;i++)
{
if(s[i]=='(')
{
s1.push(s[i]);
}
else if(s[i]=='S')
{
s1.push('(');
s1.push('S');
i += 4;
}
else if(s[i]>='0' && s[i]<='9')
{
int v = 0;
while(s[i]>='0' && s[i]<='9')
{
v = v*10+(s[i++]-'0');
}
i--;
s2.push(v);
}
else if(s[i]=='+' || s[i]=='-')
{
while(s1.top()!='(' && s1.top()!='S')
{
int a = s2.top(); s2.pop();
int b = s2.top(); s2.pop();
int c;
switch(s1.top())
{
case '+': c = b + a; break;
case '-': c = b - a; break;
case '*': c = b * a; break;
case '/': c = b / a; break;
}
s2.push(c);
s1.pop();
}
s1.push(s[i]);
}
else if(s[i]=='*' || s[i]=='/')
{
if(s1.top()=='*' )
{
int a = s2.top(); s2.pop();
int b = s2.top(); s2.pop();
s2.push(b*a);
s1.pop();
}
else if(s1.top()=='/')
{
int a = s2.top();s2.pop();
int b = s2.top();s2.pop();
s2.push(b/a);
s1.pop();
}
s1.push(s[i]);
}
else if(s[i]==')')
{
while(s1.top()!='(' && s1.top()!='S')
{
int a = s2.top();s2.pop();
int b = s2.top();s2.pop();
int c,da = 0 ,db = 0;
switch(s1.top())
{
case '+': c = b + a; break;
case '-': c = b - a; break;
case '*': c = b * a; break;
case '/': c = b / a; break;
}
s2.push(c);
s1.pop();
}
if(s1.top()=='S')
{
int a=s2.top();s2.pop();
int b=s2.top();s2.pop();
int c,da = 0 ,db = 0;
while(b!=0)
{
db += b%10;
b /= 10;
}
while(a!=0)
{
da += a%10;
a /= 10;
}
c = max(da,db);
s2.push(c);
s1.pop();
}
s1.pop();
}
}
printf("%d\n",s2.top());
s2.pop();
}
return 0;
}