#define max 60
int change(char str[60],char ep[60])
{
int st[60];
char ch;
int i=0,t=0,y=-1;
while((ch=str[i++])!='\0')
{
if((ch>='0'&&ch<='9')||ch=='.')
{
ep[t]=ch;t++ ;
while((ch=str[i++])!='\0'&&((ch>='0'&&ch<='9')||ch=='.'))
{
ep[t]=ch; t++;
}
i--;
ep[t]=' ';t++;
}
else if(ch=='(')
{
y++;
st[y]=ch;
}
else if(ch==')')
{
while(st[y]!='(')
{
ep[t]=st[y];y--;t++;
}
y--;
}
else if(ch=='+'||ch=='-')
{
while(y>=0&&st[y]!='(')
{
ep[t]=st[y];y--;t++;
}
y++;
st[y]=ch;
}
else if(ch=='*'||ch=='/')
{
while(st[y]=='*'||st[y]=='/')
{
ep[t]=st[y]; y--;t++;
}
y++;st[y]=ch;
}
}
while(y>=0)
{
ep[t]=st[y]; t++;y--;
}
ep[t]='\0';
return 1;
}
int caculate(char ep[],double *n)
{
double st[max],d; int p;
char ch;
int t=0,y=-1;
while((ch=ep[t])!='\0')
{
if((ch>='0'&&ch<='9')||ch=='.')
{
do
{
for(d=0;ep[t]>='0'&&ep[t]<='9';t++)
d=d*10+ep[t]-'0';
if(ep[t]=='.')
{
t++;
for(p=1;ep[t]>='0'&&ep[t]<='9';t++)
{
d=10*d+ep[t]-'0';
p=p*10;
}
d=d/p;
}
}
while((ch=ep[t++])!=' ');
y++;st[y]=d;
}
else
{
switch(ch)
{
case'+':st[y-1]=st[y-1]+st[y];break;
case'-':st[y-1]=st[y-1]-st[y];break;
case'*':st[y-1]=st[y-1]*st[y];break;
case'/':
if(st[y]!=0)
st[y-1]=st[y-1]/st[y];
else
return (0);
break;
}
y--;t++;
}
}
*n=st[y];
y++;
return 1;
}
#include "stdio.h"
void main()
{
char str[max];char ep[max];
double n;
printf("input your expression:");
scanf("%s",str);
printf("%s",str);
if(change(str,ep)==0)
printf("biao da shi cuo wu!");
else
{
if(caculate(ep,&n)==1)
printf("==%lf",n);
}
}