边想边写的!不怎么简洁,自己整理下
#include<stdio.h>
int main()
{
char a,b='0',c='0',ch='0';
double s1=0.0,s=0.0;
int n=0;
printf("请输入你的式子:");
while((a=getchar())!='\n'){
if(a>='0'&&a<='9'){
if(b!='.')
s1=s1*10+(a-'0');
else{
s1=s1+0.1*(a-'0');
b='0';
}
}
if(a=='.')
b='.';
if(a=='+'||a=='-'||a=='*'||a=='/'){
n++;
if(n==1){
s=s1;
s1=0.0;
}
}switch(ch){
case '+':c='+';
break;
case '-':c='-';
break;
case '*':c='*';
break;
case '/':c='/';
break;
}
if(n>=2&&(a=='+'||a=='-'||a=='*'||a=='/'))
switch(c){
case '+':s=s1+s;s1=0.0;break;
case '-':s=s-s1;s1=0.0;break;
case '*':s=s*s1;s1=0.0;break;
case '/':s=s/s1;s1=0.0;break;
}
if(a=='+'||a=='-'||a=='*'||a=='/')
ch=a;
}
switch(ch){
case '+':s=s+s1;break;
case '-':s=s-s1;break;
case '*':s=s*s1;break;
case '/':s=s/s1;break;
}
printf("=%f",s);
scanf("%lf",&a);
return 0;
}