8. Write a program that shows you a menu offering you the choice of addition, subtraction, multiplication, or division. After getting your choice, the program asks for two numbers then performs the requested operation. The program should accept only the offered menu choice. It should use type float for the numbers and allow the user to try again if he or she fails to enter a number. In the case of subtraction, the program should prompt the user to enter a new value if 0 is entered as the value for second number. A typical program run should look like this.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
#include <stdio.h> void f_add(void); void f_subtract(void); void f_multiply(void); void f_divide(void); void show_option(void); int main(void) { int choice; show_option(); while ( (choice = getchar()) != 'q' ) { switch (choice) { case 'a' : f_add(); break; case 's' : f_subtract(); break; case 'm' : f_multiply(); break; case 'd': f_divide(); break; default : show_option(); break; } } printf("Thank you !"); return 0; } void show_option() { printf("\n"); printf("Enter the operation of your choice: \n"); printf("a. add s. subtract \n"); printf("m. multiply d. divide \n"); printf("q. quit \n"); } void f_add(void) { float i1; //第一個輸入的數字 float i2; //第二個輸入的數字 char ch; //判別輸入是否為數字 printf("You select the add function \n"); //告知使用者選擇了加法 printf("Please enter the first number \n"); //顯示提示訊息 while (scanf("%f", &i1) != 1) //判斷i1是不是數字 { while ((ch=getchar()) !='\n') //如果是數字跳過 putchar(ch); //把輸入的非數字再次輸出到螢幕 printf(" is not a number.\n"); printf("Please enter a number, such as 2.5, -1, -1.78E8, or 3\n"); } printf("Please enter the second number \n"); //顯示提示訊息 while (scanf("%f", &i2) != 1) //判斷i2是不是數字 { while ((ch=getchar()) !='\n') putchar(ch); printf(" is not a number.\n"); printf("Please enter a number, such as 2.5, -1, -1.78E8, or 3\n"); } printf("%1.3f + %1.3f = %1.3f \n", i1, i2, i1 + i2); //用來計算加法結果 } void f_subtract(void) { float i1; //第一個輸入的數字 float i2; //第二個輸入的數字 char ch; //判別輸入是否為數字 printf("You select the subtract function \n"); //告知使用者選擇了加法 printf("Please enter the first number \n"); //顯示提示訊息 while (scanf("%f", &i1) != 1) //判斷i1是不是數字 { while ((ch=getchar()) !='\n') //如果是數字跳過 putchar(ch); //把輸入的非數字再次輸出到螢幕 printf(" is not a number.\n"); printf("Please enter a number, such as 2.5, -1, -1.78E8, or 3\n"); } printf("Please enter the second number \n"); //顯示提示訊息 while (scanf("%f", &i2) != 1) //判斷i2是不是數字 { while ((ch=getchar()) !='\n') putchar(ch); printf(" is not a number.\n"); printf("Please enter a number, such as 2.5, -1, -1.78E8, or 3\n"); } printf("%1.3f - %1.3f = %1.3f \n", i1, i2, i1 - i2); //用來計算減法結果 } void f_multiply(void) { float i1; //第一個輸入的數字 float i2; //第二個輸入的數字 char ch; //判別輸入是否為數字 printf("You select the multiply function \n"); //告知使用者選擇了加法 printf("Please enter the first number \n"); //顯示提示訊息 while (scanf("%f", &i1) != 1) //判斷i1是不是數字 { while ((ch=getchar()) !='\n') //如果是數字跳過 putchar(ch); //把輸入的非數字再次輸出到螢幕 printf(" is not a number.\n"); printf("Please enter a number, such as 2.5, -1, -1.78E8, or 3\n"); } printf("Please enter the second number \n"); //顯示提示訊息 while (scanf("%f", &i2) != 1) //判斷i2是不是數字 { while ((ch=getchar()) !='\n') putchar(ch); printf(" is not a number.\n"); printf("Please enter a number, such as 2.5, -1, -1.78E8, or 3\n"); } printf("%1.3f * %1.3f = %1.3f \n", i1, i2, i1 * i2); //用來計算乘法結果 } void f_divide(void){ float i1; //第一個輸入的數字 float i2; //第二個輸入的數字 char ch; //判別輸入是否為數字 printf("You select the divide function \n"); //告知使用者選擇了加法 printf("Please enter the first number \n"); //顯示提示訊息 while (scanf("%f", &i1) != 1) //判斷i1是不是數字 { while ((ch=getchar()) !='\n') //如果是數字跳過 putchar(ch); //把輸入的非數字再次輸出到螢幕 printf(" is not a number.\n"); printf("Please enter a number, such as 2.5, -1, -1.78E8, or 3\n"); } printf("Please enter the second number \n"); //顯示提示訊息 while (scanf("%f", &i2) != 1) //判斷i2是不是數字 { while ((ch=getchar()) !='\n') putchar(ch); printf(" is not a number.\n"); printf("Please enter a number, such as 2.5, -1, -1.78E8, or 3\n"); } if (i2 == 0) { printf("Please enter other number than 0 :"); scanf("%f", &i2); } printf("%1.3f / %1.3f = %1.3f \n", i1, i2, i1 / i2); //用來計算除法結果 } |