是否可以使用switch从float函数更改char函数类型的值?

时间:2013-02-03 15:43:08

标签: c

我想使用get选择函数让用户从menue中选择一个字母,我想用这个值在float函数中切换它...我知道它只能使用switch作为int类型,但我得到的价值是浮动吗?

  

float calc(float number1,float number2)

     

{       int answer = 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 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");

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; }

2 个答案:

答案 0 :(得分:1)

float calc(float number1, float number2)

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

  switch (choice)

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

答案 1 :(得分:0)

任何非零值都为真,因此您应该编码

while ((choice = getchar()) != EOF && choice != 'q')