我想使用 get 选择函数让用户从菜单中选择一个字母,并且我想使用这个值在浮点函数中切换它......我知道它只能使用 switch 作为 int 类型,但是我将获得的价值将是浮动的?
浮点计算(浮点数 1,浮点数 2)
{ int 答案 = get_choice();
switch (answer) { case 'a': answer = number1 + number2; break; case 's': answer = number1 - number2; break; case 'm': answer = number1 * number2; break; case 'd': answer = number1 / number2; break; } return answer; }
char get_choice(void)
{ int 选择;
printf("Enter the operation of your choice:\n"); printf("a. add s. subtract\n"); printf("m. multiply d. divide\n"); printf("q. quit\n"); while ((choice = getchar()) == 1 && choice != 'q') { if (choice != 'a' || choice != 's' || choice != 'm' || choice != 'd') { printf("Enter the operation of your choice:\n"); printf("a. add s. subtract\n"); printf("m. multiply d. divide\n"); printf("q. quit\n"); continue; } } return choice; }