c语言怎么换行输入多组数据

大部分人偶尔会写一些中看不中用的程序。还好,C提供了大量工具让输入更顺畅,处理过程更顺利。不过,学习这些工具会导致新的问,比如printf与scanf的快捷形式putchar与getchar。

1.使用缓冲输入

缓冲输入用起来比较方便,因为在把输入发送给程序之前,用户可以编辑输入。但是,在使用输入的字符时,它也会给程序员带来麻烦。前面示例中看到的问题是,缓冲输入要求用户按下Enter键发送输入。这一动作也传送了换行符,程序必须妥善处理这个麻烦的换行符。我们以一个猜谜程序为例。用户选择一个数字,程序猜用户选中的数字是多少。该程序使用的方法单调乏味,先不要在意算法,我们关注的重点在输入和输出。下面是猜谜程序的最初版本,后面我们会改进。

/* guess.c -- an inefficient and faulty number-guesser */
#include <stdio.h>
int main(void)
{
int guess = 1;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right and with");
printf("\nan n if it is wrong.\n");
printf("Uh...is your number %d?\n", guess);
while (getchar() != 'y')      /* get response, compare to y */
printf("Well, then, is it %d?\n", ++guess);
printf("I knew I could do it!\n");
return 0;
}

下面是程序的运行示例:

Pick an integer from 1 to 100. I will try to guess it.

Respond with a y if my guess is right and with

an n if it is wrong.

Uh...is your number 1?

n

Well, then, is it 2?

Well, then, is it 3?

n

Well, then, is it 4?

Well, then, is it 5?

y

I knew I could do it!

撇开这个程序糟糕的算法不谈,我们先选择一个数字。注意,每次输入n时,程序打印了两条消息。这是由于程序读取n作为用户否定了数字1,然后还读取了一个换行符作为用户否定了数字2。

一种解决方案是,使用while循环丢弃输入行最后剩余的内容,包括换行符。这种方法的优点是,能把no和no-way这样的响应视为简单的n。而第一个版本会把no当作两个响应。下面用循环修正这个问题:

while (getchar() != 'y')   /* get response, compare to y */
{
printf("Well, then, is it %d?n", ++guess);
while (getchar() != '\n')
continue;          /* skip rest of input line    */
}

下面是程序的运行示例:

Pick an integer fromxs 1 to 100. I will try to guess it.
Respond with a y if my guess is right and with
an n if it is wrong.
Uh...is your number 1?
n
Well, then, is it 2?
no
Well, then, is it 3?
no sir
Well, then, is it 4?
forget it
Well, then, is it 5?
y
I knew I could do it

这的确是解决了换行符的问题。但是,该程序还是会把f视为n。我们用if语句筛选其他响应。首先,添加一个char类型的变量存储响应:

: char response;

修改后的循环如下:

现在,程序的运行示例如下:

while ((response = getchar()) != 'y')     /* get response */
{
if (response == 'n')
printf("Well, then, is it %d?n", ++guess);
else
printf("Sorry, I understand only y or n.n");
while (getchar() != 'n')
continue;                 /* skip rest of input line */
}

在编写交互式程序时,应该事先预料到用户可能会输入错误,然后设计程序处理用户的错误输入。在用户出错时通知用户再次输入。当然,无论你的提示写得多么清楚,总会有人误解,然后抱怨这个程序设计得多么糟糕。

2 混合数值和字符输入

假设程序要求用getchar()处理字符输入,用scanf()处理数值输入,这两个函数都能很好地完成任务,但是不能把它们混用。因为getchar()读取每个字符,包括空格、制表符和换行符;而scanf()在读取数字时则会跳过空格、制表符和换行符。

我们通过程序清单8.5来解释这种情况导致的问题。该程序读入一个字符和两个数字,然后根据输入的两个数字指定的行数和列数打印该字符。

/* showchar1.c -- program with a BIG I/O problem */
#include <stdio.h>
void display(char cr, int lines, int width);
int main(void)
{
int ch;             /* character to be printed    */
int rows, cols;     /* number of rows and columns */
printf("Enter a character and two integers:n");
while ((ch = getchar()) != 'n')
{
scanf("%d %d", &rows, &cols);
display(ch, rows, cols);
printf("Enter another character and two integers;\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.n");
return 0;
}
void display(char cr, int lines, int width)
{
int row, col;
for (row = 1; row <= lines; row++)
{
for (col = 1; col <= width; col++)
putchar(cr);
putchar('n');  /* end line and start a new one */
}
}

注意,该程序以int类型读取字符(这样做可以检测EOF),但是却以char类型把字符传递给display()函数。因为char比int小,一些编译器会给出类型转换的警告。可以忽略这些警告,或者用下面的强制类型转换消除警告:

display(char(ch), rows, cols);

在该程序中,main()负责获取数据,display()函数负责打印数据。下面是该程序的一个运行示例,看看有什么问题:

Enter a character and two integers:

c 2 3

ccc

ccc

Enter another character and two integers;

Enter a newline to quit.

Bye.

该程序开始时运行良好。你输入c 2 3,程序打印c字符2行3列。然后,程序提示输入第2组数据,还没等你输入数据程序就退出了!这是什么情况?又是换行符在捣乱,这次是输入行中紧跟在3后面的换行符。scanf()函数把这个换行符留在输入队列中。和scanf()不同,getchar()不会跳过换行符,所以在进入下一轮迭代时,你还没来得及输入字符,它就读取了换行符,然后将其赋给ch。而ch是换行符正式终止循环的条件。

要解决这个问题,程序要跳过一轮输入结束与下一轮输入开始之间的所有换行符或空格。另外,如果该程序不在getchar()测试时,而在scanf()阶段终止程序会更好。修改后的版本如下。

/* showchar2.c -- prints characters in rows and columns */
#include <stdio.h>
void display(char cr, int lines, int width);
int main(void)
{
int ch;             /* character to be printed      */
int rows, cols;     /* number of rows and columns   */
printf("Enter a character and two integers:n");
while ((ch = getchar()) != 'n')
{
if (scanf("%d %d",&rows, &cols) != 2)
break;
display(ch, rows, cols);
while (getchar() !=  'n')
continue;
printf("Enter another character and two integers;n");
printf("Enter a newline to quit.n");
}
printf("Bye.n");
return 0;
}
void display(char cr, int lines, int width)
{
int row, col;
for (row = 1; row <= lines; row++)
{
for (col = 1; col <= width; col++)
putchar(cr);
putchar('n');  /* end line and start a new one */
}
}

while循环实现了丢弃scanf()输入后面所有字符(包括换行符)的功能,为循环的下一轮读取做好了准备。该程序的运行示例如下:

Enter a character and two integers:
c 1 2
cc
Enter another character and two integers;
Enter a newline to quit.
! 3 6
!!!!!!
!!!!!!
!!!!!!
Enter another character and two integers;
Enter a newline to quit.
Bye.

在if语句中使用一个break语句,可以在scanf()的返回值不等于2时终止程序,即如果一个或两个输入值不是整数或者遇到文件结尾就终止程序。

© 版权声明
THE END
喜欢就支持一下吧
点赞3 分享