题目如下:
8.编写一个程序,显示一个提供加法、减法、乘法、除法的菜单。获得用户选择的选项后,程序提示用户输入两个数字,然后执行用户刚才选择的操作。该程序只接受菜单提供的选项。程序使用float 类型的变量储存用户输入的数字,如果用户输入失败,则允许再次输入。进行除法运算时,如果用户输入0 作为第2个数(除数),程序应提示用户重新输入一个新值。该程序的一个运行示例如下:
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
a
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
d
Enter first number: 18.4
Enter second number: 0
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.
笔者的解答:
/*
*显示菜单
*用户输入选项
*得到选项-->检查选项--截取首字符+判断
*提示用户输入数据
*检查数据
*计算
*add:
* 提示输入数字
* 计算
* 输出结果
*其他运算同理
*除法需判断第二个输入值是否为0
*/
#include <stdio.h>
#include <stdbool.h>
char get_choice(void);//选项输入
char get_first(void);//截取首字母
void add(void);//+
void subtract(void);//-
void multiply(void);//*
void divide(void);///
float get_float(void);//判断输入是否为浮点型数据
int main(void)
{
int choice;
while ((choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' : add();
break;
case 's' : subtract();
break;
case 'm' : multiply();
break;
case 'd' : divide();
break;
default : printf("Program error!\n");
break;
}
}
printf("Bye.\n");
return 0;
}
char get_choice(void)
{
int ch;
char menu[] = {'a', 's', 'm', 'd', 'q'};
int num = sizeof(menu) / sizeof(menu[0]);
bool check = true;
printf("Enter the operation of your choice:\n");
printf("a.add s.subtract\n");
printf("m.muptiply d.divide\nq.quit\n");
ch = get_first();
while (check)
{
for (int i = 0; i < num; i++)
{
if (ch == menu[i])
{
check = false;
break;
}
}
if (check)
{
printf("Please respond with a, b, c, or q.\n");
ch = get_first();
}
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
float get_float(void)
{
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:");
}
return input;
}
void add(void)
{
float num1, num2, sum;
printf("Enter first number:");
num1 = get_float();
printf("\nEnter second number:");
num2 = get_float();
sum = num1 + num2;
printf("%f + %f = %f\n", num1, num2, sum);
}
void subtract(void)
{
float num1, num2, dif;
printf("Enter first number:");
num1 = get_float();
printf("\nEnter second number:");
num2 = get_float();
dif = num1 - num2;
printf("%f - %f = %f\n", num1, num2, dif);
}
void multiply(void)
{
float num1, num2, pro;
printf("Enter first number:");
num1 = get_float();
printf("\nEnter second number:");
num2 = get_float();
pro = num1 * num2;
printf("%f * %f = %f\n", num1, num2, pro);
}
void divide(void)
{
float num1, num2, quo;
printf("Enter first number:");
num1 = get_float();
printf("\nEnter second number:");
num2 = get_float();
while (num2 == 0)
{
printf("\nEnter a number other than 0: ");
num2 = get_float();
}
quo = num1 / num2;
printf("%f / %f = %f\n", num1, num2, quo);
}
初学C,欢迎指教!