英文:
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 <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);
}
Both of the environments I tested the code in will output Max 9, Min 5.
Can someone explain what is going on?
答案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("%d", &val);
if (val < min) min = val;
if (val > max) max = val;
scanf("%4s", line);
if (strcmp("done", 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 <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 );
}
}
You will need to enter the string "done"
only once to break the loop. In all other cases you will need to enter only integer numbers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论