创建更友好的用户界面:
一. 使用缓冲输入:
缓冲输入的优点在于在按下Enter键发送输入之前,用户可以任意编辑输入内容。
但是,如果输入的是字符,在按下Enter键之后,这一动作也传送了换行符,程序必须妥善地处理这个换行符。请看下面的示例1:
//示例1:
#include<stdio.h>
int main(void)
{
int guess = 1;
printf("Pick an integer from 1 to 100, I will try to guess it.\n");
printf("Respond with a y if my guess is right with\n");
printf("an n if it is wrong.\n");
printf("Uh...is your number %d?\n", guess);
while(getchar() != 'y')
{
printf("Well, is it %d?\n", ++guess);
}
printf("I knew I could do it!\n");
return 0;
}
下面是程序的运行示例:
程序解释与评价: 这是一个猜数程序。显然,这个程序很糟糕! 我们先选择了一个数字。每次输入n时,程序打印了两条消息。这是由于程序读取n作为用户否定了数字1,然后还读取了一个换行符否定了数字2。
一个解决的方案是: 使用while循环丢弃输入行最后剩余的内容,包括换行符。这种该方法的优点是,能把no和no way这样的响应视为简单的n。
优化的示例1如下:
//示例2:
#include<stdio.h>
int main(void)
{
int guess = 1;
printf("Pick an integer from 1 to 100, I will try to guess it.\n");
printf("Respond with a y if my guess is right with\n");
printf("an n if it is wrong.\n");
printf("Uh...is your number %d?\n", guess);
while(getchar() != 'y')
{
printf("Well, is it %d?\n", ++guess);
while(getchar() != '\n')
continue;
}
printf("I knew I could do it!\n");
return 0;
}
下面是程序的运行示例:
程序解释与评价: 示例2就解决了换行符的问题。但是,该程序中如果输入forget it,则视为n。
问题解决: 用if语句筛选其它响应,添加一个char类型的变量储存响应。
//示例3:
#include<stdio.h>
int main(void)
{
int guess = 1;
char response;
printf("Pick an integer from 1 to 100, I will try to guess it.\n");
printf("Respond with a y if my guess is right with\n");
printf("an n if it is wrong.\n");
printf("Uh...is your number %d?\n", guess);
while((response = getchar()) != 'y')
{
if(response == 'n')
printf("Well, is it %d?\n", ++guess);
else
printf("Sorry, I understand only y or n:\n");
while(getchar() != '\n')
continue;
}
printf("I knew I could do it!\n");
return 0;
}
优化的示例2如下:
在编写交互程序时,应该是事先预料到用户可能会输入错误,然后设计程序处理用户的错误输入。在用户出错时通知用户再次输入。
二. 混合输入数字和字符
1. getchar()进行字符输入和使用scanf()进行数字输入。
请看下面程序,该程序读取一个字符和两个数字,然后使用由所输入的两个数字指定的行数和列数来打印该字符。
#include<stdio.h>
void display(char cr, int lines, int width);
int main(void)
{
int ch;
int rows, cols;
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 intergers:\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');
}
}
程序运行如下:
程序解释与评价:
程序第一轮运行完了后直接退出了程序。这个问题跟上面出现的问题相同,即换行符的问题。
2. 优化的程序如下:
#include<stdio.h>
void display(char cr, int lines, int width);
int main(void)
{
int ch;
int rows, cols;
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 intergers:\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');
}
}
程序运行如下:
通过使用一个if语句和一个break,如果scanf()的返回值不是2,您就中止了该程序。这种情况在有一个或两个输入值不是整数或者遇到文件尾时发生。