请求帮助,一道练习题!
c primer plus第5版 8-8的一个练习8.编写一个程序,显示一个菜单,为您提供加法、减法、乘法或除法的选项。获得您的选择后,该程序请求两个数,然后执行您选择的操作。该程序应该只接受它所提供的菜单选项。它应该使用float类型的数,并且如果用户未能输入数字应允许其重新输入。在除法的情况中,如果用户输入O作为第二个数,该程序应该提示用户输入一个新的值。一个典型的程序运行应该如下所示:我实现除法输入第二个数的时候,有问题!其他的似乎还比较正常!各位帮我看看吧。谢谢了!
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quic
Enter first number: 22.4
Enter second number: one
one is not an number.
Please enter a number, such as 2.5. -1.78E8, or 3. 1
22.4 + 1 = 23.4
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
Enter first number: 18.4
Enter second number: O
Enter a number other than 0: 0.2 //我实现除法输入第二个数的时候,有问题!其他的似乎还比较正常!各位帮我看看吧。谢谢了!
18.4 / 0.2 = 92
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
q
Bye.
程序代码:
#include <STDIO.H> #include <CTYPE.H> void menu(void); void add(void); void substract(void); void multiply(void); void divide(void); char get_choice(void); char get_first(void); float get_float(void); int main(void) { int choice; while ((choice = get_choice()) != 'q') { switch (choice) { case 'a': add(); break; case 's': substract(); break; case 'm': multiply(); break; case 'd': divide(); break; default : puts("error"); break; } } puts("Bye"); return 0; } void menu(void) { puts("Enter the operation of your choice:"); printf("a. add s. subtract\n"); printf("m. multiply d. divide\n"); puts("q. quit"); } void add(void) { float firstnumber, secondnumber; puts("Enter first number:"); firstnumber = get_float(); puts("Enter second number:"); secondnumber = get_float(); printf("%.1f + %.1f = %.1f\n", firstnumber, secondnumber, firstnumber + secondnumber); } void substract(void) { float firstnumber, secondnumber; puts("Enter first number:"); firstnumber = get_float(); puts("Enter second number:"); secondnumber = get_float(); printf("%.1f - %.1f = %.1f\n", firstnumber, secondnumber, firstnumber - secondnumber); } void multiply(void) { float firstnumber, secondnumber; puts("Enter first number:"); firstnumber = get_float(); puts("Enter second number:"); secondnumber = get_float(); printf("%.1f * %.1f = %.1f\n", firstnumber, secondnumber, firstnumber * secondnumber); } void divide(void) { float firstnumber, secondnumber; int ch; puts("Enter first number:"); firstnumber = get_float(); puts("Enter second number:"); secondnumber = get_float(); while((secondnumber = get_float()) == 0) { do { ch = getchar(); } while (isspace(ch)); puts("Enter a number more than 0:"); continue; } printf("%.1f / %.1f = %.1f\n", firstnumber, secondnumber, firstnumber / secondnumber); } char get_choice(void) { int ch; menu(); ch = get_first(); //while ( (ch < 'a' || ch > 's') && ch != 'q') while(ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q') { printf("Please respond with a, d, s, m, or q.\n"); ch = get_first(); } return ch; } char get_first(void) { int ch; do { ch = getchar(); } while (isspace(ch)); while (getchar() != '\n') continue; return ch; } float get_float(void) { int ch; float float_num; while(scanf("%f", &float_num) != 1) { while((ch = getchar()) != '\n') putchar(ch); printf("is not an number.\n"); printf("enter a number,such as 2.5,-1.78E8 or 3:"); } return float_num; }我实现除法输入第二个数的时候,有问题!其他的似乎还比较正常!各位帮我看看吧。谢谢了!
[ 本帖最后由 迷途的菜鸟 于 2012-8-17 14:06 编辑 ]