英文:
How to write a C program to find maximum between three numbers using switch statement?
问题
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("输入三个整数以找到最大值:\n");
scanf("%d%d%d", &num1, &num2, &num3);
switch (num1 > num2)
{
switch (num1 > num3)
{
case 1:
printf("%d 是最大值", num1);
break;
switch (num2 > num1)
{
switch (num2 > num3)
{
case 1:
printf("%d 是最大值", num2);
break;
default:
printf("%d 是最大值", num3);
}
}
}
}
}
英文:
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three integers to find maximum: \n");
scanf("%d%d%d", num1, num2, num3);
switch (num1 > num2)
{
switch (num1 > num3)
{
case 1:
printf("%d is maximum", num1);
break;
switch (num2 > num1)
{
switch (num2 > num3)
{
case 1:
printf("%d is maximum", num2);
break;
default:
printf("%d is maximum", num3);
}
}
}
}
}
This is the switch
statement program I wrote in VS Code to find maximum between 3 numbers, but it doesn't work. I declared 3 variables num1
, num2
, num3
then I compared num1
to num2
and num3
, Similarly num2
to num1
and num3
, num3
is in default
.
Can we write maximum between 3 numbers?
答案1
得分: 2
在你的switch
语句体中缺少case
或default
子句。
还要注意,你必须将目标变量的地址传递给scanf()
,并且应该测试scanf
是否成功转换了3个整数:它会返回成功转换的数量。
以下是修改后的版本:
#include <stdio.h>
int main(void) {
int num1, num2, num3;
printf("Enter three integers to find maximum: \n");
if (scanf("%d%d%d", &num1, &num2, &num3) != 3) {
printf("invalid input\n");
return 1;
}
switch (num1 >= num2) {
case 1:
switch (num1 >= num3) {
case 1:
printf("%d is maximum\n", num1);
break;
default:
printf("%d is maximum\n", num3);
break;
}
default:
switch (num2 >= num3) {
case 1:
printf("%d is maximum\n", num2);
break;
default:
printf("%d is maximum\n", num3);
break;
}
}
return 0;
}
以下是一个使用单个switch
语句的替代版本:
#include <stdio.h>
int main(void) {
int num1, num2, num3;
printf("Enter three integers to find maximum: \n");
if (scanf("%d%d%d", &num1, &num2, &num3) != 3) {
printf("invalid input\n");
return 1;
}
switch ((num1 >= num2) + 2 * (num1 >= num3) + 4 * (num2 >= num3)) {
case 3:
case 7:
printf("%d is maximum\n", num1);
break;
case 4:
case 6:
printf("%d is maximum\n", num2);
break;
case 0:
case 1:
printf("%d is maximum\n", num3);
break;
default:
printf("CPU is broken\n");
break;
}
return 0;
}
最后,滥用规则的情况下,这是一个简化版本,仍然使用了switch
语句,易于扩展到更大的一组数字:
#include <stdio.h>
int main(void) {
int num1, num2, num3, max;
printf("Enter three integers to find maximum: \n");
if (scanf("%d%d%d", &num1, &num2, &num3) != 3) {
printf("invalid input\n");
return 1;
}
switch (max = num1,
max = (max >= num2) * max + (max < num2) * num2,
max = (max >= num3) * max + (max < num3) * num3)
{
default:
printf("%d is maximum\n", max);
break;
}
return 0;
}
为了完整起见,上面的switch
语句可以重写为一个空的语句体:
switch (max = num1,
max = (max >= num2) * max + (max < num2) * num2,
max = (max >= num3) * max + (max < num3) * num3)
/* 空的语句体 */;
printf("%d is maximum\n", max);
英文:
There are missing case
or default
clauses in your switch
statement bodies.
Note also that you must pass the addresses of the target variables to scanf()
and you should test that this scanf
succeeded at converting 3 integers: it returns the number of successful conversions.
Here is a modified version:
#include <stdio.h>
int main(void) {
int num1, num2, num3;
printf("Enter three integers to find maximum: \n");
if (scanf("%d%d%d", &num1, &num2, &num3) != 3) {
printf("invalid input\n");
return 1;
}
switch (num1 > num2) {
case 1:
switch (num1 > num3) {
case 1:
printf("%d is maximum\n", num1);
break;
default:
printf("%d is maximum\n", num3);
break;
}
default:
switch (num2 > num3) {
case 1:
printf("%d is maximum\n", num2);
break;
default:
printf("%d is maximum\n", num3);
break;
}
}
return 0;
}
And here is an alternative with a single switch
statement:
#include <stdio.h>
int main(void) {
int num1, num2, num3;
printf("Enter three integers to find maximum: \n");
if (scanf("%d%d%d", &num1, &num2, &num3) != 3) {
printf("invalid input\n");
return 1;
}
switch ((num1 >= num2) + 2 * (num1 >= num3) + 4 * (num2 >= num3)) {
case 3:
case 7:
printf("%d is maximum\n", num1);
break;
case 4:
case 6:
printf("%d is maximum\n", num2);
break;
case 0:
case 1:
printf("%d is maximum\n", num3);
break;
default:
printf("CPU is broken\n");
break;
}
return 0;
}
Finally, abusing the rules, here is a simplified version still using a switch
statement, that is easy to extend to a larger set of numbers:
#include <stdio.h>
int main(void) {
int num1, num2, num3, max;
printf("Enter three integers to find maximum: \n");
if (scanf("%d%d%d", &num1, &num2, &num3) != 3) {
printf("invalid input\n");
return 1;
}
switch (max = num1,
max = (max >= num2) * max + (max < num2) * num2,
max = (max >= num3) * max + (max < num3) * num3)
{
default:
printf("%d is maximum\n", max);
break;
}
return 0;
}
For completeness, the switch
statement here above can be rewritten with an empty body:
switch (max = num1,
max = (max >= num2) * max + (max < num2) * num2,
max = (max >= num3) * max + (max < num3) * num3)
/* empty body */;
printf("%d is maximum\n", max);
答案2
得分: 2
以下是翻译好的内容:
For starters the arguments of this call of scanf
首先,这个 scanf
的调用参数
scanf("%d%d%d", num1, num2, num3);
are incorrect. You need to write
是不正确的。你需要写成
scanf("%d%d%d", &num1, &num2, &num3);
And in general you should check whether the input was successful as for example
而且一般情况下,你应该检查输入是否成功,例如
if ( scanf("%d%d%d", &num1, &num2, &num3) == 3 )
{
//...
}
else
{
//...
}
Secondly the user can enter at least two equal each other numbers that are gretaer than the third number. You have to take that into account.
其次,用户可以输入至少两个相等的数字,这两个数字都大于第三个数字。你需要考虑这种情况。
To make it simpler to write switch statements it will be useful to perform the task using if statements. They can look the following way
为了更容易编写 switch 语句,最好使用 if 语句来执行任务。它们可以如下所示:
if (!( num1 < num2 ) && !( num1 < num3 ))
{
printf( "%d is maximum\n", num1 );
}
else if ( !( num2 < num3 ) )
{
printf( "%d is maximum\n", num2 );
}
else
{
printf( "%d is maximum\n", num3 );
}
So now it is not hard to rewrite the if statements using switch statements. They can look the following way
所以现在不难使用 switch 语句重写 if 语句。它们可以如下所示:
switch (!( num1 < num2 ) && !( num1 < num3 ))
{
case 1:
printf( "%d is maximum\n", num1 );
break;
case 0:
switch (!( num2 < num3 ))
{
case 1:
printf( "%d is maximum\n", num2 );
break;
case 0:
printf( "%d is maximum\n", num3 );
break;
}
}
Instead of using the constants 0
and 1
in the case labels you can use named constants false
and true
by means of including header <stdbool.h>
as for example
可以使用名为 false
和 true
的命名常量而不是在 case 标签中使用常量 0
和 1
,方法是包含头文件 <stdbool.h>
,例如
#include <stdbool.h>
//...
switch (!( num1 < num2 ) && !( num1 < num3 ))
{
case true:
printf( "%d is maximum\n", num1 );
break;
case false:
switch (!( num2 < num3 ))
{
case true:
printf( "%d is maximum\n", num2 );
break;
case false:
printf( "%d is maximum\n", num3 );
break;
}
}
Of course there are other ways to write the switch statements but this way is enough clear.
当然,还有其他编写 switch 语句的方式,但这种方式足够清晰。
Here is a demonstration program.
下面是一个演示程序:
#include <stdio.h>
#include <stdbool.h>
int main( void )
{
int num1, num2, num3;
printf( "Enter three integers to find maximum: " );
scanf( "%d%d%d", &num1, &num2, &num3 );
if (!( num1 < num2 ) && !( num1 < num3 ))
{
printf( "%d is maximum\n", num1 );
}
else if ( !( num2 < num3 ) )
{
printf( "%d is maximum\n", num2 );
}
else
{
printf( "%d is maximum\n", num3 );
}
switch (!( num1 < num2 ) && !( num1 < num3 ))
{
case true:
printf( "%d is maximum\n", num1 );
break;
case false:
switch (!( num2 < num3 ))
{
case true:
printf( "%d is maximum\n", num2 );
break;
case false:
printf( "%d is maximum\n", num3 );
break;
}
}
}
Its output might look like
它的输出可能如下所示:
Enter three integers to find maximum: 3 2 3
3 is maximum
3 is maximum
The if statements are included in the demonstration program only to show that the results of using if statements and switch statements will be the same. Of course in your program you should remove the if statements.
if 语句仅包含在演示程序中,以显示使用 if 语句和 switch 语句的结果将相同。当然,在你的程序中,你应该删除 if 语句。
英文:
For starters the arguments of this call of scanf
scanf("%d%d%d", num1, num2, num3);
are incorrect. You need to write
scanf("%d%d%d", &num1, &num2, &num3);
And in general you should check whether the input was successful as for example
if ( scanf("%d%d%d", &num1, &num2, &num3) == 3 )
{
//...
}
else
{
//...
}
Secondly the user can enter at least two equal each other numbers that are gretaer than the third number. You have to take that into account.
To make it simpler to write switch statements it will be useful to perform the task using if statements. They can look the following way
if (!( num1 < num2 ) && !( num1 < num3 ))
{
printf( "%d is maximum\n", num1 );
}
else if ( !( num2 < num3 ) )
{
printf( "%d is maximum\n", num2 );
}
else
{
printf( "%d is maximum\n", num3 );
}
So now it is not hard to rewrite the if statements using switch statements. They can look the following way
switch (!( num1 < num2 ) && !( num1 < num3 ))
{
case 1:
printf( "%d is maximum\n", num1 );
break;
case 0:
switch (!( num2 < num3 ))
{
case 1:
printf( "%d is maximum\n", num2 );
break;
case 0:
printf( "%d is maximum\n", num3 );
break;
}
}
Instead of using the constants 0
and 1
in the case labels you can use named constants false
and true
by means of including header <stdbool.h>
as for example
#include <stdbool.h>
//...
switch (!( num1 < num2 ) && !( num1 < num3 ))
{
case true:
printf( "%d is maximum\n", num1 );
break;
case false:
switch (!( num2 < num3 ))
{
case true:
printf( "%d is maximum\n", num2 );
break;
case false:
printf( "%d is maximum\n", num3 );
break;
}
}
Of course there are other ways to write the switch statements but this way is enough clear.
Here is a demonstration program.
#include <stdio.h>
#include <stdbool.h>
int main( void )
{
int num1, num2, num3;
printf( "Enter three integers to find maximum: " );
scanf( "%d%d%d", &num1, &num2, &num3 );
if (!( num1 < num2 ) && !( num1 < num3 ))
{
printf( "%d is maximum\n", num1 );
}
else if ( !( num2 < num3 ) )
{
printf( "%d is maximum\n", num2 );
}
else
{
printf( "%d is maximum\n", num3 );
}
switch (!( num1 < num2 ) && !( num1 < num3 ))
{
case true:
printf( "%d is maximum\n", num1 );
break;
case false:
switch (!( num2 < num3 ))
{
case true:
printf( "%d is maximum\n", num2 );
break;
case false:
printf( "%d is maximum\n", num3 );
break;
}
}
}
Its output might look like
Enter three integers to find maximum: 3 2 3
3 is maximum
3 is maximum
The if statements are included in the demonstration program only to show that the results of using if statements and switch statements will be the same. Of course in your program you should remove the if statements.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论