C 输出混淆

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

C output confusion

问题

我正在尝试学习一些C语言,但遇到了困难。
我有这个任务:'编写一个C程序,确定输入的一系列整数数字的最小值和最大值,以字符串"done"结束。' 非常简单的任务,就像输入示例一样:
5
2
9
done
但是输出结果是错误的,我无法弄清楚原因。我非常感谢帮助。谢谢!

代码:

#include <stdio.h>
#include <string.h>
int main() {
    char line[1000];
    int val, min = 10, max = -1;
    while ( 1 ) {
      scanf("%d", &val);
      if (val < min) min = val;
      if (val > max) max = val;
      scanf("%4s", line);
      if (strcmp("done", line) == 0) break;
    }
    printf("Maximum %d\n", max);
    printf("Minimum %d", min);
}

我测试过的两个环境都会输出最大值9,最小值5。
有人能解释发生了什么吗?

英文:

I'm trying to learn some C, but it's a pain.
I have this task: 'Write a C program to determine the minmum and maximum of a sequence of integer numbers on input, terminged by the string "done".' Very simple task, just like the input:
5
2
9
done
However the output is wrong, and I can't figure it out. I would very much appreciate some help. Thx!

The code:

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int main() {
    char line[1000];
    int val, min = 10, max = -1;
    while ( 1 ) {
      scanf(&quot;%d&quot;, &amp;val);
      if (val &lt; min) min = val;
      if (val &gt; max) max = val;
      scanf(&quot;%4s&quot;, line);
      if (strcmp(&quot;done&quot;, line) == 0) break;
    }
	printf(&quot;Maximum %d\n&quot;, max);
  	printf(&quot;Minimum %d&quot;, min);
}

Both of the environments I tested the code in will output Max 9, Min 5.
Can someone explain what is going on?
C 输出混淆
C 输出混淆

答案1

得分: 2

根据这个while循环:

while ( 1 ) {
  scanf("%d", &val);
  if (val < min) min = val;
  if (val > max) max = val;
  scanf("%4s", line);
  if (strcmp("done", line) == 0) break;
}

在每次输入一个数字后,你需要输入一个字符串。所以第二个数字2被视为字符串输入。

此外,整数可以是负数(小于-1)或正数(大于10)。所以这个声明:

int val, min = 10, max = -1;

没有意义。

相反,你可以将所有数字读入字符数组,然后例如使用atoi函数将输入的字符串转换为数字。

例如,一个简单的程序可以如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( void ) 
{
    char line[16];
    int min = 0, max = 0;
    int empty = 1;

    while ( scanf( "%15s", line ) == 1 && strcmp( line, "done" ) != 0 )
    {
        int val = atoi( line );

        if ( empty )
        {
            min   = val;
            max   = val;
            empty = 0;
        }
        else if ( max < val )
        {
            max = val;
        }
        else if ( val < min )
        {
            min = val;
        }
    }
     
    if ( empty )
    {
        puts( "The sequence of numbers is empty." );
    }
    else
    {
        printf( "Maximum %d\n", max );
        printf( "Minimum %d\n", min );
    }
}

你只需要输入字符串"done"一次来跳出循环。在所有其他情况下,你只需要输入整数。

英文:

According to the while loop

while ( 1 ) {
  scanf(&quot;%d&quot;, &amp;val);
  if (val &lt; min) min = val;
  if (val &gt; max) max = val;
  scanf(&quot;%4s&quot;, line);
  if (strcmp(&quot;done&quot;, line) == 0) break;
}

you have to enter a string after each input of a number.

So the second number 2 is considered as an input of a string.

Also integer numbers can be negative less than -1 or positive greater than 10. So this declaration

int val, min = 10, max = -1;

does not make sense.

Instead you can read all numbers in a character array and then to use for example the function atoi to convert the entered string to a number.

For example a simple program can look like

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

int main( void ) 
{
    char line[16];
    int min = 0, max = 0;
    int empty = 1;

    while ( scanf( &quot;%15s&quot;, line ) == 1 &amp;&amp; strcmp( line, &quot;done&quot; ) != 0 )
    {
        int val = atoi( line );

        if ( empty )
        {
            min   = val;
            max   = val;
            empty = 0;
        }
        else if ( max &lt; val )
        {
            max = val;
        }
        else if ( val &lt; min )
        {
            min = val;
        }
    }
     
    if ( empty )
    {
        puts( &quot;The sequence of numbers is empty.&quot; );
    }
    else
    {
        printf( &quot;Maximum %d\n&quot;, max );
        printf( &quot;Minimum %d\n&quot;, min );
    }
}

You will need to enter the string &quot;done&quot; only once to break the loop. In all other cases you will need to enter only integer numbers.

huangapple
  • 本文由 发表于 2023年1月9日 04:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050906.html
匿名

发表评论

匿名网友

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

确定