0

我想使用 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; }
4

2 回答 2

1
float calc(float number1, float number2)

{ int choice = get_choice();
  float answer = 0.0;

  switch (choice)

无需重用该变量。您也可以char直接切换变量。

于 2013-02-03T15:45:16.123 回答
0

任何非零值都是真的,所以你应该编码

while ((choice = getchar()) != EOF && choice != 'q') 
于 2013-02-03T15:45:48.990 回答