|
|
发表于 2013-8-30 17:11:43
|
显示全部楼层
稍微按照我自己的习惯改了下- #include<stdio.h>
- char get_choice();
- char get_first();
- void count1();
- void count2();
- void count3();
- void count4();
- float get_float();
- int main()
- {
- char choice;
- void count1();
- void count2();
- void count3();
- void count4();
-
- while((choice=get_choice())!='q')
- {
- switch(choice)
- {
- case 'a':count1();
- break;
-
- case 's':count2();
- break;
-
- case 'm':count3();
- break;
-
- case 'd':count4();
- break;
-
- case 'q':break;
-
- default:printf("Program error.\n");
- break;
- }
- }
- printf("Done.");
- return 0;
- }
- char get_choice()
- {
- char ch;
- printf("Enter the operation of your choice:\n");
- printf("a.add s.subtract\n");
- printf("m.multiply d.divide\n");
- printf("q.quit\n");
-
- //ch=get_first();
- ch = getchar();
- //while(ch!='a'||ch!='s'||ch!='m'||ch!='d'&&ch!='q')
- while(ch!='a' && ch!='s' && ch!='m' && ch!='d' && ch!='q' )
- {
- printf("Please enter the character followed the tip.\n");
-
- ch=get_first();
- }
-
-
- return ch;
- }
- void count1()
- {
- float num1,num2,num;
- printf("Enter the first number:");
- //num1=get_float();
- scanf("%f",&num1);
- printf("Enter second number:");
- //num2=get_float();
- scanf("%f",&num2);
-
- num=num1+num2;
- printf("%.1f + %.1f=%.1f.",num1,num2,num);
-
- }
- void count2()
- {
- float num1,num2,num;
- printf("Enter the first number:");
- num1=get_float();
- printf("Enter second number:");
- num2=get_float();
-
- num=num1-num2;
- printf("%.1f - %.1f=%.1f.",num1,num2,num);
-
- }
- void count3()
- {
- float num1,num2,num;
- printf("Enter the first number:");
- num1=get_float();
- printf("Enter second number:");
- num2=get_float();
-
- num=num1*num2;
- printf("%.1f * %.1f=%.1f.",num1,num2,num);
-
- }
- void count4()
- {
- float num1,num2,num;
- printf("Enter the first number:");
- num1=get_float();
- printf("Enter second number:");
- num2=get_float();
- while(num2==0)
- printf("Enter a number other than 0:");
- num2=get_float();
-
- num=num1/num2;
- printf("%.1f / %.1f=%.1f.",num1,num2,num);
-
- }
- float get_float()
- {
- float input;
- char ch;
-
- while(scanf("%f",&input)!=1)
- {
- while((ch=getchar())!='\n')
- putchar(ch);
- printf(" is not an number.\n");
- printf("Please enter a number,such as 2.5,-1.78E8,or 3.1.\n");
- }
- return input;
- }
- char get_first()
- {
- int ch;
-
- ch=getchar();
- while(getchar()!='\n')
- continue;
-
- return ch;
- }
复制代码 主要是while的条件写错了
还有你干嘛放着好好的break不用 用continue
还有像 getchar不用 非 自己定义一个 get_first??
首先 这种预防等你功能实现之后再去弄吧
还有我和你说的你就是不听是吧
|
|