使用此复杂逻辑的switch语句的意外输出

时间:2011-05-24 22:49:45

标签: c switch-statement

我正在尝试为3个条件使用switch语句。条件是:

  1. 当a,b和c都为零时,x的任何值都是一个解。打印:x的任何值都是解决方案。
  2. 当a和b为零且c不为时,不存在解。打印:没有解决方案。
  3. 当a为零且b不为零时,唯一的解决方案是x = -c / b。计算x的值并打印解决方案。
  4. 当我尝试运行程序时,显示错误的结果。我的意见是

    a = 0
    b = 0
    c = 0
    

    所以它应该打印“x的任何值是一个解决方案”,但它没有。

    我的节目是:

    #include <stdio.h>
    
    //Function declarations
    void getData (int* a, int* b, int* c);
    float calculateX (int a, int b, int c);
    
    //===================================================
    int main (void)
    {
        //Local declarations
        int a;
        int b;
        int c;
        float x;
    
        //Statements
        getData (&a, &b, &c);
        calculateX (a, b, c);
    
        int temp;
        printf("\nEnter an integer and press enter to exit the program: ");
        scanf("%d", &temp);
    
        return 0;
    }
    
    //----------------------------------------------------
    void getData (int* a, int* b, int* c)
    {
        printf("Enter three integers: ");
        scanf("%d %d %d", a, b, c);
        return;
    }
    
    //----------------------------------------------------
    float calculateX (int a, int b, int c)
    {
        float x;
    
        printf("Input is: %d %d %d\n", a, b, c);
        switch(a, b, c)
        {
            case 1: (a, b, c == 0);
                    printf("Any value of x is a solution.");
                    break;
            case 2: (a, b == 0 && c!= 0);
                    printf("No solution exists.");
                    break;
            case 3: (a == 0 && b!= 0);
                    x = (float)(-c/b);
                    printf("The value of x is: %.1f", x);
                    break;
            default: printf("Cannot calculate.");
        }
        return a, b, c;
    }
    

    我的输出是:

    Enter three integers: 0 0 0
    Input is: 0 0 0
    Cannot calculate.
    Enter an integer and press enter to exit the program:
    

3 个答案:

答案 0 :(得分:7)

这不是switch语句的工作原理。它编译,但非常模糊的原因。显然,它在运行时没有达到预期效果。

一般来说,您在单个表达式上使用switch语句,每个case标签代表该表达式的一个可能值。 e.g:

switch (x)
{
case 1:
    // Code here runs when x == 1
    break;
case 2:
    // Code here runs when x == 2
    break;
default:
    // Code here runs for all other values of x
    break;
}

在您的应用程序中,您希望测试多个变量,并以复杂的方式将它们组合在一起。 switch没有简洁的方法可以做到这一点。您应该考虑使用一组if语句。

答案 1 :(得分:2)

你有什么理由要使用switch吗?只是做

if (a == 0 && b == 0 && c == 0)
  ...
else if (a == 0 && b == 0 && c != 0)
  ....
...

答案 2 :(得分:1)

实际上,这是使用switch语句解决此问题的有效方法:

switch ((a != 0) * 4 + (b != 0) * 2 + (c != 0))
{
case 0: // a, b, c == 0
    printf("Any value of x is a solution.");
    break;

case 1: // a, b == 0 && c!= 0
    printf("No solution exists.");
    break;

case 2: // a == 0 && b!= 0
case 3:
    x = (float)(-c/b);
    printf("The value of x is: %.1f", x);
    break;

default:
    printf("Cannot calculate.");
}

我的代码基于你的代码,除了我使用条件(评估为0或1)来编码表达式中每个变量的状态(分别为零或不分别),将每个变量分配给一个单独的位。然后switch对其进行解码 - 作为初学者,有趣的部分是case 2落到case 3,因为我们不关心c是否为零

您的代码还有其他一些问题,但我将自己限制在您询问的switch。祝你好运。