如:求(22+3)*4=100
输入 :22(空格)3+4*(回车)
输出:100
#include<stdio.h>
#include<string.h>
void compute(char s[])
{
int st[20];
int i=0,n,flag,top=-1,b,c,d,e;
n=strlen(s);
while(i<n)
{
flag=0;
b=0;
while(i<n&&s[i]>='0'&&s[i]<='9')
{
b=b*10;
b=b+s[i]-'0';
i++;
flag=1;
}
if(flag)
{
top++;
st[top]=b;
}
else if(s[i]=='/'||s[i]=='+'||s[i]=='*'||s[i]=='-')
{
c=st[top];top--;
d=st[top];top--;
switch(s[i])
{
case '/':
e=d/c;
break;
case '+':
e=d+c;
break;
case '*':
e=d*c;
break;
case '-':
e=d-c;
break;
}
top++;
st[top]=e;
i++;
}
else
i++;
}
printf("%d",st[top]);
}
int main()
{
char s[20];
gets(s);
compute(s);
return 0;
}