第1个回答 推荐于2017-10-10
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void)
{
float a,b,f;
int ctl=0,p;
char Cflag='Y';
char c;
char sa[10],sb,sc[10];
while((Cflag == 'Y') || (Cflag == 'y') )
{
for(p=0;p<10;p++) sa[p]='\0';
sb='\0';
for(p=0;p<10;p++) sc[p]='\0';
p=0;
ctl=1;
printf("加、减、乘、除四则运算算式:\n");
scanf("%c", &c);
while(c!='\n')
{
if (c>='0' && c<='9' || c=='.')
{
if ( ctl==1 ) sa[p]=c;
if ( ctl==2 ) sc[p]=c;
p++;
}else if ( c=='+'||c=='-'||c=='*'||c=='/')
{
ctl=2;
sa[p]='\0';
sb=c;
p=0;
}
scanf("%c", &c);
}
sc[p]='\0';
//printf("[%s%c%s]",sa,sb,sc);
a=atof(sa);
b=atof(sc);
switch(sb)
{
case '+':
f=a+b;
break;
case '-':
f=a-b;
break;
case '*':
f=a*b;
break;
case '/':
f=a/b;
break;
defalt:
;
}
printf("[%.2f%c%.2f]=%.2f",a,sb,b,f);
printf("\n是否继续计算(Y/N)?");
scanf("%c", &Cflag);
getchar();
}
}本回答被提问者采纳