对于这个复杂的逻辑使用 switch 语句会产生意外的输出

发布于 2024-11-09 05:31:41 字数 1808 浏览 1 评论 0原文

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

  1. 当a、b和c都为零时,x的任何值都是解。打印: x 的任意值都是解。
  2. 当 a 和 b 为零且 c 不为零时,不存在解。打印:不存在解决方案。
  3. 当a为零且b不为零时,唯一的解是x = -c/b。计算 x 的值并打印解决方案。

当我尝试运行我的程序时,它显示了错误的结果。我的输入是

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:

I am trying to use a switch statement for 3 conditions. Conditions were:

  1. When a, b, and c are all zero, any value of x is a solution. Print: Any value of x is a solution.
  2. When a and b are zero and c is not, no solution exists. Print: No solution exists.
  3. When a is zero and b is not zero, the only solution is x = -c/b. Calculate the value of x and print the solution.

When I tried to run my program, it displayed the wrong results. My input were

a = 0
b = 0
c = 0

So it's supposed to print "Any value of x is a solution", but it didn't.

My program is:

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

And my output was:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若水微香 2024-11-16 05:31:41

这不是 switch 语句的工作原理。它可以编译,但出于非常隐晦的原因。显然,它运行时并没有达到您的预期。

一般来说,您可以在单个表达式上使用 switch 语句,并且每个 case 标签代表该表达式的一个可能值。例如:

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 语句。

This is not how a switch statement works. It compiles, but for very obscure reasons. Obviously, it doesn't do what you expect when it runs.

Generally speaking, you use a switch statement on a single expression, and each of the case labels represents one possible value of that expression. 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;
}

In your application, you want to test multiple variables, and combine them in complex ways. There is no neat way to do that with switch. You should consider a set of if statements instead.

神魇的王 2024-11-16 05:31:41

您有什么理由必须使用 switch 吗?只要做

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

Is there any reason you have to use switch? Just do

if (a == 0 && b == 0 && c == 0)
  ...
else if (a == 0 && b == 0 && c != 0)
  ....
...
琉璃繁缕 2024-11-16 05:31:41

实际上,这是使用 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 上。祝你好运。

Actually, here's a valid way to use the switch statement for this problem:

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.");
}

I've based this on your code, except that I use conditionals (which evaluate to 0 or 1) to encode the state of each variable (zero or not, respectively) in the expression, assigning each to a separate bit. The switch then decodes it -- the interesting part for you, as a beginner, is that case 2 falls through to case 3 because we don't care whether c is zero.

Your code has some other issues, but I'm restricting myself to the switch that you asked about. Best of luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文