#include<bits/stdc++.h>
using namespace std;
char str[1000];
int oper(int x,char ch,int y)
{
if(ch=='+') return x+y;
if(ch=='*') return x*y;
}
char cmp(char ch1,char ch2)
{
if(ch1=='('){if(ch2=='('||ch2=='*'||ch2=='+'||ch2==')'||ch2=='=') return '>';}
if(ch1=='*'){if(ch2=='*'||ch2=='+'||ch2==')'||ch2=='=') return '>';}
if(ch1=='+'){if(ch2=='+'||ch2==')'||ch2=='=')return '>';}
if(ch2==')'){if(ch2==')'||ch2=='=') return '>';}
return '<';
}
int solve(int from,int to)
{
stack<char> c;
stack<int> num;
while(!c.empty())c.pop();while(!num.empty()) num.pop();
c.push('=');
printf("from=%d to=%d\n",from,to);
for(int i=from;i<=to;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
int st=i,st1=i;
while(str[st1]>='0'&&str[st1]<='9') st1++;st1--;
printf("%d-%d\n",st,st1);
int val=0;for(int j=st;j<=st1;j++) val=val*10+str[j]-'0';
i=st1;printf("val=%d\n",val);
num.push(val);continue;
}
printf("%c %c\n",c.top(),str[i]);
if(str[i]==')')
{
while(cmp(c.top(),str[i])=='>')
{
if(c.top()=='(') {c.pop();break;}
int x=num.top();num.pop();
int y=num.top();num.pop();
int xx=oper(x,c.top(),y);
printf("x=%d y=%d xx=%d\n",x,y,xx);
num.push(xx);c.pop();
}
continue;
}
if(cmp(c.top(),str[i])=='>')
{
if(c.top()=='(') {c.push(str[i]);continue;}
int x=num.top();num.pop();
int y=num.top();num.pop();
int xx=oper(x,c.top(),y);
printf("x=%d y=%d xx=%d\n",x,y,xx);
c.pop();num.push(xx);
}
c.push(str[i]);
}
while(!c.empty())
{
if(c.top()=='='){return num.top();}
int x=num.top();num.pop();
int y=num.top();num.pop();
int xx=oper(x,c.top(),y);
num.push(xx);
c.pop();
}
//return oper(x,c.top(),y);
}
int main()
{
while(scanf("%s",str+1)!=EOF)
{
int len=strlen(str+1);
cout<<solve(1,len)<<endl;
}
return 0;
}//无函数测试用,可将函数运算同化这个模板计算。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
char str[2000];
LL oper(LL x,char ch,LL y)
{
if(ch=='+') return x+y;
if(ch=='*') return x*y;
return -1;
}
char cmp(char ch1,char ch2)
{
if(ch1=='('||ch1=='S'){if(ch2=='S'||ch2=='('||ch2=='*'||ch2=='+'||ch2==')'||ch2=='='||ch2==',') return '>';}
if(ch1=='*'){if(ch2=='*'||ch2=='+'||ch2==')'||ch2=='='||ch2==',') return '>';}
if(ch1=='+'){if(ch2=='+'||ch2==')'||ch2=='='||ch2==',')return '>';}
if(ch2==')'||ch2==','){if(ch2==')'||ch2=='='||ch2==',') return '>';}
return '<';
}
int main()
{
LL n;
cin>>n;
while(n--)
{
scanf("%s",str+1);
stack<char> c;
stack<LL> num;
while(!c.empty())c.pop();while(!num.empty()) num.pop();
c.push('=');
LL len=strlen(str+1);
for(LL i=1;i<=len;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
LL st=i,st1=i;
while(str[st1]>='0'&&str[st1]<='9') st1++;st1--;
LL val=0;for(int j=st;j<=st1;j++) val=val*10+str[j]-'0';
i=st1;
//printf("val=%d\n",val);
num.push(val);continue;
}
if(str[i]=='S'){c.push('S');i+=4;continue;}
if(str[i]==')'||str[i]==',')
{
while(cmp(c.top(),str[i])=='>')
{
if(c.top()=='(') {c.pop();break;}
if(c.top()=='S')
{
c.pop();c.push(',');
break;
}
if(c.top()==',')
{
c.pop();
LL p1=num.top();num.pop();
LL p2=num.top();num.pop();
LL x=0,y=0;//printf("p1=%lld p2=%lld \n",p1,p2);
while(p1){x+=p1%10;p1/=10;}
while(p2){y+=p2%10;p2/=10;}//printf("~~~%lld %lld~~~~\n",x,y);
num.push(max(x,y));
break;
}
LL x=num.top();num.pop();
LL y=num.top();num.pop();
LL xx=oper(x,c.top(),y);
num.push(xx);c.pop();
}
continue;
}
if(cmp(c.top(),str[i])=='>')
{
if(c.top()=='('||c.top()=='S') {c.push(str[i]);continue;}
LL x=num.top();num.pop();
LL y=num.top();num.pop();
LL xx=oper(x,c.top(),y);
c.pop();num.push(xx);
}
c.push(str[i]);
}
while(!c.empty())
{
if(c.top()=='='){cout<<num.top()<<endl;break;}
LL x=num.top();num.pop();
LL y=num.top();num.pop();
LL xx=oper(x,c.top(),y);
num.push(xx);
c.pop();
}
}
return 0;
}//可ac之丑陋代码,有一定适用范围,加了*/运算就有问题了,即使调整了运算符的优先级表达式求值
#include<bits/stdc++.h>
using namespace std;
char str[2000];
int oper(int x,char ch,int y)
{
if(ch=='a'){return max(x,y);}
if(ch=='i'){return min(x,y);}
if(ch=='d'){return x+y;}
return -1;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",str+1);
int len=strlen(str+1);
stack<char>c;
stack<int>num;
while(!c.empty()) c.pop();while(!num.empty()) num.pop();
for(int i=1;i<=len;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
int st=i,st1=i;
while(str[st1]>='0'&&str[st1]<='9') st1++;st1--;
int val=0;for(int j=st;j<=st1;j++) val=val*10+str[j]-'0';
num.push(val);//printf("val=%d\n",val);
i=st1;
continue;
}
if(str[i]==',') continue;
if(str[i]=='m')
{
if(str[i+1]=='i') c.push('i');
if(str[i+1]=='a') c.push('a');
i+=3;continue;
}
if(str[i]=='a'){c.push('d');i+=3;continue;}
if(str[i]==')')
{
int x=num.top();num.pop();
int y=num.top();num.pop();
//printf("%d %d ch=%c ",x,y,c.top());
int xx=oper(x,c.top(),y);c.pop();//printf("%d\n",xx);
num.push(xx);
continue;
}
c.push(str[i]);
}
printf("%d\n",num.top());
}
return 0;
}
//仅有括号符,逗号当成括号处理,栈的裸题。表达式求值
#include<bits/stdc++.h>
using namespace std;
char str[2000];
double oper(double x,char ch,double y)
{
if(ch=='+') return x+y;
if(ch=='*') return x*y;
if(ch=='/') return y/x;
if(ch=='-') return y-x;
return -1;
}
int into(char ch)
{
if(ch=='(') return 4;
if(ch=='/'||ch=='*') return 3;
if(ch=='+'||ch=='-') return 2;
if(ch=='='||ch==')') return 1;
return -1;
}
char cmp(char ch1,char ch2)
{
if(into(ch1)>=into(ch2)) return '>';
return '<';
}
double solve(int from,int to)
{
stack<char> c;
stack<double> num;
while(!c.empty())c.pop();while(!num.empty()) num.pop();
c.push('=');
//printf("from=%d to=%d\n",from,to);
for(int i=from;i<=to;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
int st=i,st1=i;
while((str[st1]>='0'&&str[st1]<='9')||(str[st1]=='.')) st1++;st1--;
//printf("%d-%d\n",st,st1);
char str1[2000];
int p;
for(int j=st,k=0;j<=st1;j++,k++,p=k) str1[k]=str[j];str1[p]='\0';
i=st1;
double val=atof(str1);//printf("val=%lf\n",val);
num.push(val);continue;
}
//printf("%c %c\n",c.top(),str[i]);
while(cmp(c.top(),str[i])=='>')
{
//if(c.top()=='=') break;
if(c.top()=='(') {if(str[i]==')') c.pop();break;}
double x=num.top();num.pop();
double y=num.top();num.pop();
double xx=oper(x,c.top(),y);
//printf("x=%lf y=%lf %c xx=%lf\n",x,y,c.top(),xx);
c.pop();num.push(xx);
}
if(str[i]!=')') c.push(str[i]);
}
while(!c.empty())
{
if(c.top()=='='){return num.top();}
double x=num.top();num.pop();
double y=num.top();num.pop();
double xx=oper(x,c.top(),y);
//printf("x=%lf y=%lf %c xx=%lf\n",x,y,c.top(),xx);
num.push(xx);
c.pop();
}
return -1;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",str+1);
int len=strlen(str+1);
printf("%.2lf\n",solve(1,len-1));
}
return 0;
}
//附数据:1/2/3/4= 同级一定要按顺序算,套第一个模板会从后往前算哎 2*3/2*2*2*4/2=似乎是先算除再算乘 已调整至依次运算,不清楚问题实质,也就要debug下去了,,让代码变形进阶表达式求值