楽 你觉得有我不能用数组写的树吗?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#define YURI 赛高
#define N 10005
char s[N];
int l;
int cal(int l,int r) {
int minpos=-1,prio=0,p[1005],minn;
memset(p,0x3f,sizeof(p));
minn=p[0];
for(int i=r; i>=l; i--) {
if(s[i]==')')prio+=100;
if(s[i]=='(')prio-=100;
if(s[i]=='*')p[i]=prio+2;
if(s[i]=='/')p[i]=prio+2;
if(s[i]=='+')p[i]=prio+1;
if(s[i]=='-')p[i]=prio+1;
if(p[i]<minn)minn=p[i],minpos=i;
}
if(minpos==-1) {
int num=0;
for(int i=l; i<=r; i++)
if(s[i]>='0'&&s[i]<='9')
num=num*10+s[i]-'0';
return num;
} else {
if(s[minpos]=='+')return cal(l,minpos-1)+cal(minpos+1,r);
if(s[minpos]=='-')return cal(l,minpos-1)-cal(minpos+1,r);
if(s[minpos]=='*')return cal(l,minpos-1)*cal(minpos+1,r);
if(s[minpos]=='/')return cal(l,minpos-1)/cal(minpos+1,r);
}
}
int loc;
int pd[4]= {-1,-1,-1,-1};
char op[4];
int dig[4];
void find(int l,int r,int id) {
int minpos=-1,prio=0,p[1005],minn;
memset(p,0x3f,sizeof(p));
minn=p[0];
for(int i=r; i>=l; i--) {
if(s[i]==')')prio+=100;
if(s[i]=='(')prio-=100;
if(s[i]=='*')p[i]=prio+2;
if(s[i]=='/')p[i]=prio+2;
if(s[i]=='+')p[i]=prio+1;
if(s[i]=='-')p[i]=prio+1;
if(p[i]<minn)minn=p[i],minpos=i;
}
if(minpos==-1) {
pd[id]=0;
int num=0;
for(int i=l; i<=r; i++)
if(s[i]>='0'&&s[i]<='9')
num=num*10+s[i]-'0';
dig[id]=num;
} else {
if(id==1)loc=minpos;
pd[id]=1;
op[id]=s[minpos];
}
}
#ifdef YURI
signed main() {
gets(s);
l=strlen(s);
find(0,l-1,1);
if(pd[1]==0)printf("%d\n",dig[1]);
else {
printf("%c ",op[1]);
find(0,loc-1,2);
find(loc+1,l-1,3);
if(pd[2]==0)printf("%d ",dig[2]);
else printf("%c ",op[2]);
if(pd[3]==0)printf("%d \n",dig[3]);
else printf("%c\n",op[3]);
}
int ans=cal(0,l-1);
printf("%d\n",ans);
return 0;
}
#endif