对于这个复杂的逻辑使用 switch 语句会产生意外的输出
我尝试在 3 个条件下使用 switch 语句。条件是:
- 当a、b和c都为零时,x的任何值都是解。打印: x 的任意值都是解。
- 当 a 和 b 为零且 c 不为零时,不存在解。打印:不存在解决方案。
- 当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:
- When a, b, and c are all zero, any value of x is a solution. Print: Any value of x is a solution.
- When a and b are zero and c is not, no solution exists. Print: No solution exists.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是
switch
语句的工作原理。它可以编译,但出于非常隐晦的原因。显然,它运行时并没有达到您的预期。一般来说,您可以在单个表达式上使用
switch
语句,并且每个case
标签代表该表达式的一个可能值。例如:在您的应用程序中,您想要测试多个变量,并以复杂的方式组合它们。
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 thecase
labels represents one possible value of that expression. e.g.: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 ofif
statements instead.您有什么理由必须使用
switch
吗?只要做Is there any reason you have to use
switch
? Just do实际上,这是使用
switch
语句解决此问题的有效方法:我基于您的代码进行了此操作,只是我使用条件语句(计算结果为 0 或 1)来对每个状态进行编码表达式中的变量(分别为零或非零),将每个变量分配给一个单独的位。然后
switch
对其进行解码 - 对于初学者来说,有趣的部分是case 2
会落到case 3
因为我们不知道不在乎c
是否为零。您的代码还有一些其他问题,但我将自己限制在您询问的
switch
上。祝你好运。Actually, here's a valid way to use the
switch
statement for this problem: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 thatcase 2
falls through tocase 3
because we don't care whetherc
is zero.Your code has some other issues, but I'm restricting myself to the
switch
that you asked about. Best of luck.