标签:style io ar color os 使用 sp for on
2 1.000+2/4= ((1+2)*5+1)/4=
1.50 4.00
代码:
#include<stdio.h>
double fun(double a,double b,char ch)
{
if(ch==‘+‘)
return b+a;
if(ch==‘-‘)
return b-a;
if(ch==‘*‘)
return b*a;
if(ch==‘/‘)
return b/a;
}
int main(void)
{
int i,top1,top2;
int n;
double x,t,a,b;
double num[1000];
char str[1000],ch[1000];
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
top1=-1;
top2=-1;
for(i=0;str[i]!=‘\0‘;i++)
{
x=0;
if(str[i]>=‘0‘&&str[i]<=‘9‘)
{
while(str[i]>=‘0‘&&str[i]<=‘9‘)
{
x=x*10+str[i]-‘0‘;
i++;
}
if(str[i]==‘.‘)
{
i++;
t=0.1;
while(str[i]>=‘0‘&&str[i]<=‘9‘)
{
x=x+(str[i]-‘0‘)*t;
t=t*0.1;
i++;
}
}
top1++;
num[top1]=x;
}
if(str[i]==‘\0‘)
break;
else if(str[i]==‘(‘)
{
ch[++top2]=str[i];
}
else if(str[i]==‘)‘)
{
while(top2>=0&&ch[top2]!=‘(‘)
{
a=num[top1];
top1--;
b=num[top1];
num[top1]=fun(a,b,ch[top2]);
top2--;
}
top2--;
}
else if(str[i]==‘*‘||str[i]==‘/‘)
{
while(ch[top2]==‘*‘||ch[top2]==‘/‘)
{
a=num[top1];
top1--;
b=num[top1];
num[top1]=fun(a,b,ch[top2]);
top2--;
}
ch[++top2]=str[i];
}
else
{
while(top2>=0&&ch[top2]!=‘(‘)
{
a=num[top1];
top1--;
b=num[top1];
num[top1]=fun(a,b,ch[top2]);
top2--;
}
ch[++top2]=str[i];
}
}
while(top2>=0)
{
a=num[top1];
top1--;
b=num[top1];
num[top1]=fun(a,b,ch[top2]);
top2--;
}
printf("%.2f\n",num[0]);
}
return 0;
}
标签:style io ar color os 使用 sp for on
原文地址:http://blog.csdn.net/qq_16997551/article/details/41923483