Unexpected output using the switch statement for this complex logic
I am trying to use a switch s开发者_开发知识库tatement 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:
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.
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)
....
...
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.
精彩评论