编写一个程序,显示一个菜单,为您提供加法、减法、乘法或除法的选项。获得您的选择后,该程序请求两个数,然后执行您选择的操作。该程序应该只接受它所提供的菜单选项。它应该使用float类型的数。并且如果用户未能输入数字应允许其重新输入。在出发的情况中,如果用户输入0作为第二个数,该程序应该提示用户输入一个新的值。
# include <stdio.h>
float add(float one, float two);
float sub(float one, float two);
float mul(float one, float two);
float div(float one, float two);
float get();
int main(void)
{
float one;
float two;
char ch;
printf("********计算器********\n\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n\n");
printf("**********************\n");
while((ch = getchar()) != 'q')
{
switch(ch)
{
case 'a':
one = get();
two = get();
printf("%.3f + %.3f = %.3f\n", one, two, add( one, two));
break;
case 's':
one = get();
two = get();
printf("%.3f - %.3f = %.3f\n", one, two, sub( one, two));
break;
case 'm':
one = get();
two = get();
printf("%.3f * %.3f = %.3f\n", one, two, mul( one, two));
break;
case 'd':
one = get();
two = get();
if(two == 0)
two = get();
printf("%.3f / %.3f = %.3f\n", one, two, div( one, two));
break;
}
}
return 0;
}
float get()
{
float num;
printf("Please enter a number :\n");
while(scanf("%f", &num) != 1)
printf("Please enter anther number :\n");
return num;
}
float add(float one, float two)
{
float sum;
sum = one + two;
return sum;
}
float sub(float one, float two)
{
float sub;
sub = one - two;
return sub;
}
float mul(float one, float two)
{
float mul;
mul = one * two;
return mul;
}
float div(float one, float two)
{
float div;
div = one / two;
return div;
}