#include<stdio.h>
char Computing_method(void); //检查第一个字符是否输入正确
char menu(void); //处理第一个字符
float get_int1(void); //处理第一个数字
float get_int2(void); //处理第二个数字
int main(void)
{
char ch;
float result,act1,act2;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\nm. multiply d. divide\n");
printf("q. quit\n");
while((ch=menu())!='q')
{
printf("Enter first number:\n");
switch(ch){
case 'a': act1=get_int1();
act2=get_int2();
result=act1+act2;
printf("%.1f+%.1f=%.1f\n",act1,act2,result);
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\nm. multiply d. divide\n");
printf("q. quit\n");
break;
case 's': act1=get_int1();
act2=get_int2();
result=act1-act2;
printf("%.1f-%.1f=%.1f\n",act1,act2,result);
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\nm. multiply d. divide\n");
printf("q. quit\n");
break;
case 'm': act1=get_int1();
act2=get_int2();
result=act1*act2;
printf("%.1f*%.1f=%.1f\n",act1,act2,result);
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\nm. multiply d. divide\n");
printf("q. quit\n");
break;
case 'd':
act1=get_int1();
act2=get_int2();
if(act2==0){ //除数不可以是0
printf("Ender a numer other than 0:");
act2=get_int2();
}
result=act1/act2;
printf("%.1f/%.1f=%.1f\n",act1,act2,result);
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\nm. multiply d. divide\n");
printf("q. quit\n");
break;
default : break;
}
while(getchar()!='\n')
continue;
}
printf("Bye!\n");
return 0;
}
char menu(void){
char ch;
ch=Computing_method();
while(ch!='a'&&ch!='s'&&ch!='m'&&ch!='d'&&ch!='q'){
printf("Please respond with a, s, m, d, or q.\n");
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\nm. multiply d. divide\n");
printf("q. quit\n");
ch=Computing_method();
}
return ch;
}
char Computing_method(void){ //检查是否有效输入
int ch;
ch=getchar();
while(getchar()!='\n')
continue;
return ch;
}
float get_int1(void) //处理第一个数字
{
float act1;
char ch;
while(scanf("%f",&act1)!=1){
while((ch=getchar())!='\n')
putchar(ch);
printf(" is not an number.\n");
printf("Please enter a numbr, such as 2.5, -1, or 3:");
}
printf("Ender second number:\n");
return act1;
}
float get_int2(void) //处理第二个数字
{
float act2;
char ch;
while(scanf("%f",&act2)!=1){
while((ch=getchar())!='\n')
putchar(ch);
printf(" is not an number.\n");
printf("Please enter a numbr, such as 2.5, -1, or 3:");
}
return act2;
}