如何使用switch语句编写C程序来找到三个数字中的最大值?

huangapple go评论80阅读模式
英文:

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语句体中缺少casedefault子句。

还要注意,你必须将目标变量的地址传递给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 &lt;stdio.h&gt;

int main(void) {
    int num1, num2, num3;

    printf(&quot;Enter three integers to find maximum: \n&quot;);

    if (scanf(&quot;%d%d%d&quot;, &amp;num1, &amp;num2, &amp;num3) != 3) {
        printf(&quot;invalid input\n&quot;);
        return 1;
    }

    switch (num1 &gt; num2) {
    case 1:
        switch (num1 &gt; num3) {
        case 1:
            printf(&quot;%d is maximum\n&quot;, num1);
            break;
        default:
            printf(&quot;%d is maximum\n&quot;, num3);
            break;
        }
    default:
        switch (num2 &gt; num3) {
        case 1:
            printf(&quot;%d is maximum\n&quot;, num2);
            break;
        default:
            printf(&quot;%d is maximum\n&quot;, num3);
            break;
        }
    }
    return 0;
}

And here is an alternative with a single switch statement:

#include &lt;stdio.h&gt;

int main(void) {
    int num1, num2, num3;

    printf(&quot;Enter three integers to find maximum: \n&quot;);

    if (scanf(&quot;%d%d%d&quot;, &amp;num1, &amp;num2, &amp;num3) != 3) {
        printf(&quot;invalid input\n&quot;);
        return 1;
    }

    switch ((num1 &gt;= num2) + 2 * (num1 &gt;= num3) + 4 * (num2 &gt;= num3)) {
    case 3:
    case 7:
        printf(&quot;%d is maximum\n&quot;, num1);
        break;
    case 4:
    case 6:
        printf(&quot;%d is maximum\n&quot;, num2);
        break;
    case 0:
    case 1:
        printf(&quot;%d is maximum\n&quot;, num3);
        break;
    default:
        printf(&quot;CPU is broken\n&quot;);
        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 &lt;stdio.h&gt;

int main(void) {
    int num1, num2, num3, max;

    printf(&quot;Enter three integers to find maximum: \n&quot;);

    if (scanf(&quot;%d%d%d&quot;, &amp;num1, &amp;num2, &amp;num3) != 3) {
        printf(&quot;invalid input\n&quot;);
        return 1;
    }

    switch (max = num1,
            max = (max &gt;= num2) * max + (max &lt; num2) * num2,
            max = (max &gt;= num3) * max + (max &lt; num3) * num3)
    {
    default:
        printf(&quot;%d is maximum\n&quot;, max);
        break;
    }
    return 0;
}

For completeness, the switch statement here above can be rewritten with an empty body:

    switch (max = num1,
            max = (max &gt;= num2) * max + (max &lt; num2) * num2,
            max = (max &gt;= num3) * max + (max &lt; num3) * num3)
        /* empty body */;

    printf(&quot;%d is maximum\n&quot;, max);

答案2

得分: 2

以下是翻译好的内容:

For starters the arguments of this call of scanf

首先,这个 scanf 的调用参数

    scanf(&quot;%d%d%d&quot;, num1, num2, num3);

are incorrect. You need to write

是不正确的。你需要写成

    scanf(&quot;%d%d%d&quot;, &amp;num1, &amp;num2, &amp;num3);

And in general you should check whether the input was successful as for example

而且一般情况下,你应该检查输入是否成功,例如

    if ( scanf(&quot;%d%d%d&quot;, &amp;num1, &amp;num2, &amp;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 &lt; num2 ) &amp;&amp; !( num1 &lt; num3 ))
		{
			printf( &quot;%d is maximum\n&quot;, num1 );
		}
		else if ( !( num2 &lt; num3 ) )
		{
			printf( &quot;%d is maximum\n&quot;, num2 );
		}
		else
		{
			printf( &quot;%d is maximum\n&quot;, 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 &lt; num2 ) &amp;&amp; !( num1 &lt; num3 ))
		{
		case 1:
			printf( &quot;%d is maximum\n&quot;, num1 );
			break;

		case 0:
			switch (!( num2 &lt; num3 ))
			{
			case 1:
				printf( &quot;%d is maximum\n&quot;, num2 );
                break;

			case 0:
				printf( &quot;%d is maximum\n&quot;, 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 &lt;stdbool.h&gt; as for example

可以使用名为 falsetrue 的命名常量而不是在 case 标签中使用常量 01,方法是包含头文件 &lt;stdbool.h&gt;,例如

    #include &lt;stdbool.h&gt;
    
    //...

		switch (!( num1 &lt; num2 ) &amp;&amp; !( num1 &lt; num3 ))
		{
		case true:
			printf( &quot;%d is maximum\n&quot;, num1 );
			break;

		case false:
			switch (!( num2 &lt; num3 ))
			{
			case true:
				printf( &quot;%d is maximum\n&quot;, num2 );
                break;

			case false:
				printf( &quot;%d is maximum\n&quot;, 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 &lt;stdio.h&gt;
    #include &lt;stdbool.h&gt;

    int main( void )
	{
		int num1, num2, num3;

		printf( &quot;Enter three integers to find maximum: &quot; );
		scanf( &quot;%d%d%d&quot;, &amp;num1, &amp;num2, &amp;num3 );

		if (!( num1 &lt; num2 ) &amp;&amp; !( num1 &lt; num3 ))
		{
			printf( &quot;%d is maximum\n&quot;, num1 );
		}
		else if ( !( num2 &lt; num3 ) )
		{
			printf( &quot;%d is maximum\n&quot;, num2 );
		}
		else
		{
			printf( &quot;%d is maximum\n&quot;, num3 );
		}

		switch (!( num1 &lt; num2 ) &amp;&amp; !( num1 &lt; num3 ))
		{
		case true:
			printf( &quot;%d is maximum\n&quot;, num1 );
			break;

		case false:
			switch (!( num2 &lt; num3 ))
			{
			case true:
				printf( &quot;%d is maximum\n&quot;, num2 );
                break;

			case false:
				printf( &quot;%d is maximum\n&quot;, 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(&quot;%d%d%d&quot;, num1, num2, num3);

are incorrect. You need to write

scanf(&quot;%d%d%d&quot;, &amp;num1, &amp;num2, &amp;num3);

And in general you should check whether the input was successful as for example

if ( scanf(&quot;%d%d%d&quot;, &amp;num1, &amp;num2, &amp;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 &lt; num2 ) &amp;&amp; !( num1 &lt; num3 ))
	{
		printf( &quot;%d is maximum\n&quot;, num1 );
	}
	else if ( !( num2 &lt; num3 ) )
	{
		printf( &quot;%d is maximum\n&quot;, num2 );
	}
	else
	{
		printf( &quot;%d is maximum\n&quot;, num3 );
	}

So now it is not hard to rewrite the if statements using switch statements. They can look the following way

	switch (!( num1 &lt; num2 ) &amp;&amp; !( num1 &lt; num3 ))
	{
	case 1:
		printf( &quot;%d is maximum\n&quot;, num1 );
		break;

	case 0:
		switch (!( num2 &lt; num3 ))
		{
		case 1:
			printf( &quot;%d is maximum\n&quot;, num2 );
            break;

		case 0:
			printf( &quot;%d is maximum\n&quot;, 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 &lt;stdbool.h&gt; as for example

#include &lt;stdbool.h&gt;

//...

	switch (!( num1 &lt; num2 ) &amp;&amp; !( num1 &lt; num3 ))
	{
	case true:
		printf( &quot;%d is maximum\n&quot;, num1 );
		break;

	case false:
		switch (!( num2 &lt; num3 ))
		{
		case true:
			printf( &quot;%d is maximum\n&quot;, num2 );
            break;

		case false:
			printf( &quot;%d is maximum\n&quot;, 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 &lt;stdio.h&gt;
#include &lt;stdbool.h&gt;

int main( void )
{
	int num1, num2, num3;

	printf( &quot;Enter three integers to find maximum: &quot; );
	scanf( &quot;%d%d%d&quot;, &amp;num1, &amp;num2, &amp;num3 );

	if (!( num1 &lt; num2 ) &amp;&amp; !( num1 &lt; num3 ))
	{
		printf( &quot;%d is maximum\n&quot;, num1 );
	}
	else if ( !( num2 &lt; num3 ) )
	{
		printf( &quot;%d is maximum\n&quot;, num2 );
	}
	else
	{
		printf( &quot;%d is maximum\n&quot;, num3 );
	}

	switch (!( num1 &lt; num2 ) &amp;&amp; !( num1 &lt; num3 ))
	{
	case true:
		printf( &quot;%d is maximum\n&quot;, num1 );
		break;

	case false:
		switch (!( num2 &lt; num3 ))
		{
		case true:
			printf( &quot;%d is maximum\n&quot;, num2 );
            break;

		case false:
			printf( &quot;%d is maximum\n&quot;, 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.

huangapple
  • 本文由 发表于 2023年6月22日 17:39:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定