編寫一個程序,顯示一個菜單,爲您提供加法、減法、乘法或除法的選項。獲得您的選擇後,該程序請求兩個數,然後執行您選擇的操作。該程序應該只接受它所提供的菜單選項。它應該使用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;
}