8.8編寫一個程式,顯示提供加法、減法、乘法的選單,該程式只接受選單提供的選項,程式提示使用者輸入兩個數字......
#include <stdio.h> char get_choice(void); int check_first_input (void); int check_second_input (void); float add(void); float subtract(void); float multiply(void); float divide(void); int main(void) { char 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("please enter the right word.\n");continue; } } printf("done!"); return 0; } char get_choice(void) { char choice; printf("Enter the operation of your choice: \n"); printf("a. add s. subtract \n"); printf("m. multiply d. divide\n"); printf("q. quit\n"); choice = getchar(); while(getchar() !='\n') continue; return choice; } float add(void) { float first_number,second_number; float result; first_number = check_first_input(); second_number = check_second_input(); result = first_number + second_number; printf("%.1f + %.1f = %.1f\n",first_number,second_number,result); return result; } float subtract(void) { float first_number,second_number; float result; first_number = check_first_input(); second_number = check_second_input(); result = first_number - second_number; printf("%.1f - %.1f = %.1f\n",first_number,second_number,result); return result; } float multiply(void) { float first_number,second_number; float result; first_number = check_first_input(); second_number = check_second_input(); result = first_number * second_number; printf("%.1f * %.1f = %.1f\n",first_number,second_number,result); return result; } float divide(void) { float first_number,second_number; float result; first_number = check_first_input(); second_number = check_second_input(); while (second_number == 0) { printf("enter a number other than 0: "); second_number = check_second_input(); } result = first_number / second_number; printf("%.1f / %.1f = %.1f\n",first_number,second_number,result); return result; } int check_first_input (void) { int input; printf("please enter the first number: "); while ((scanf("%d",&input)) == 0) { printf("%s is not an number.\n",input); printf("please enter a number,such as 2.5,-1.78E8,or 3:1"); scanf("%d",&input); } return input; } int check_second_input (void) { int input; printf("please enter the second number: "); while ((scanf("%d",&input)) == 0) { printf("%s is not an number.",input); printf("please enter a number,such as 2.5,-1.78E8,or 3"); scanf("%d",&input); } return input; }
在輸入都為數字時候程式可以執行相應的操作,但是有兩個問題: 1. 當輸入為字母的時候會出錯, 2. 在一次輸入計算完畢後 在輸入時候選單欄會顯示兩次 如圖:
第一個錯誤:沒有搞懂 函式scanf ,我的想法是scanf(“%d”,choice)當choice輸入的不是整數型時候,利用printf的%s來輸出字母,這裡出錯了,對於%d輸入的scanf 如果輸入的是字元比如a 時候,scanf()將會停在那裡,並把a放回輸入中,不會把值賦給指定的變數。這樣在程式後面如果有%c的字元輸入語句將先讀取字母a,但是如果程式中只有%d形式的轉換說明,則scanf就一直無法越過a讀取下一個字元。
修改:
在scanf(“%d”,input)不成功時,利用getchar()來輸出非數字字元,這裡的getchar()獲得是上次input的值,並不是再從鍵盤獲取的值。
第二個:錯誤出現在於scanf與getchar混用時候的錯誤,如圖在輸入22【enter】 1【enter】時候,後面的那個enter 這個換行符留在了輸入佇列中,而下一次的重新輸入m時遇到的是getchar()函式,這就使得getchar()函式先獲取【enter】的字元‘\n’,這樣就不符合輸入出現提示再次重新輸入。 那麼問題來了 為什麼兩次獲取數字 first_number second_number 都使用了scanf(“%d”),那麼為什麼前一次獲取first_number的操作不會影響到下一次的 seond_number的操作呢? 因為scanf函式
修改:
在scanf後面的加上 while(getchar() !='\n') continue;
我之前想法是這邊的不等於改成等於,但是一旦這樣修改那麼Enter將會進入while, 那麼這個enter鍵將被使用而不會將輸入送入快取區,這樣表現出的就是第二次輸入後無法進入程式,如圖:
其實這個while的作用並不是為了檢測到Enter鍵的時候進入while(這樣就會導致輸入無法進入快取區),而是利用getchar()這個函式將在輸入佇列中的【Enter】提取出來,這樣下一個get_choice中的getchar()函式就不會提取到上一次輸入的【Enter】。
修改後的程式碼:
#include <stdio.h>
char get_choice(void);
int check_first_input (void);
int check_second_input (void);
float add(void);
float subtract(void);
float multiply(void);
float divide(void);
int main(void)
{
char 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("please enter the right word.\n");continue;
}
}
printf("done!");
return 0;
}
char get_choice(void)
{
char choice;
printf("Enter the operation of your choice: \n");
printf("a. add s. subtract \n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
choice = getchar();
while(getchar() !='\n')
continue;
return choice;
}
float add(void)
{
float first_number,second_number;
float result;
first_number = check_first_input();
second_number = check_second_input();
result = first_number + second_number;
printf("%.1f + %.1f = %.1f\n",first_number,second_number,result);
return result;
}
float subtract(void)
{
float first_number,second_number;
float result;
first_number = check_first_input();
second_number = check_second_input();
result = first_number - second_number;
printf("%.1f - %.1f = %.1f\n",first_number,second_number,result);
return result;
}
float multiply(void)
{
float first_number,second_number;
float result;
first_number = check_first_input();
second_number = check_second_input();
result = first_number * second_number;
printf("%.1f * %.1f = %.1f\n",first_number,second_number,result);
return result;
}
float divide(void)
{
float first_number,second_number;
float result;
first_number = check_first_input();
second_number = check_second_input();
while (second_number == 0)
{
printf("enter a number other than 0: ");
second_number = check_second_input();
}
result = first_number / second_number;
printf("%.1f / %.1f = %.1f\n",first_number,second_number,result);
return result;
}
int check_first_input (void)
{
int input;
char ch;
printf("please enter the first number: ");
while ((scanf("%d",&input)) == 0)
{
while ((ch = getchar()) != '\n') //這裡的getchar()獲得是上次input的值,並不是再從鍵盤獲取新的值
putchar(ch);
printf(" is not an number.\n");
printf("please enter a number,such as 2.5,-1.78E8,or 3 : ");
}
while(getchar() !='\n') //我之前想法是這邊的不等於改成等於,但是一旦這樣修改那麼Enter將會進入while
continue; // 那麼這個enter鍵將被使用而不會將輸入送入快取區。
return input;
}
int check_second_input (void)
{
int input;
char ch;
printf("please enter the second number: ");
while ((scanf("%d",&input)) == 0)
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an number.");
printf("please enter a number,such as 2.5,-1.78E8,or 3 :");
}
while(getchar() !='\n')
continue;
return input;
}