(C语言)菜单浏览程序中彻底清除缓冲区中的换行符'\n'
在做一些交互式程序时,用户输入模式有缓冲输入和非缓冲输入两种情况。关于非缓冲输入,它主要体现在如游戏或其它一些使用鼠标操作的软件中,能及时快速响应用户输入。而非缓冲输入则是将用户输入信息暂存在缓冲区中,用户在提交确认之前还能够进行更改,如控制台输入。闲话少说,这次练习是关于 getchar() 与 scanf() 接受字符输入,同时要清除用于确认输入的换行符,虽说代码不多,但要控制自如还着实得花一番功夫(不用我多浪费唇舌了,各位都是高手,一看代码就能心领神会的)。
1
#include <stdio.h>
2
float correntfloat(void);
3
char correntchar(void);
4
char choice(void);
5
void operate(float,float,char);
6
int main(void)
7
{
8
char op;
9
float m,n;
10
op=choice();
11![]()
12
while(op!='q')
13
{
14
printf("\nEnter the first number:");
15
m=correntfloat();
16
printf("\nEnter the second number:");
17
n=correntfloat();
18![]()
19
while((op=='d')||(n==0.0))
20
{
21
printf("\nEnter a number than 0:");
22
n=correntfloat();
23
}
24
if(op=='d')
25
printf("\n%f/%f=%f\n",m,n,m/n);
26
else
27
operate(m,n,op);
28
op=choice();
29
}
30
printf("\nBye!");
31![]()
32
return 0;
33
}
34
float correntfloat(void)
35
{
36
float num;
37
char ch;
38
while(scanf("%f",&num)!=1)
39
{
40
while((ch=getchar())!='\n')
41
continue;
42
putchar(ch);
43
printf(" is not float.please enter such as 2.5,-1.735 or 3:");
44
}
45![]()
46
while((ch=getchar())!='\n') //warning:very important to jump the '\n',
47
continue;
48![]()
49
return num;
50
}
51
char correntchar(void)
52
{
53
char ch;
54
ch=getchar();
55
while(getchar()!='\n')
56
continue;
57
return ch;
58
}
59
char choice()
60
{
61
char ch;
62
printf("Enter the operation of your choice:");
63
printf("\na. add s, subtract");
64
printf("\nm.multiply d.divide");
65
printf("\nq.quit\n");
66![]()
67
ch=correntchar();
68
while((ch!='a')&&(ch!='s')&&(ch!='m')&&(ch!='d')&&(ch!='q'))
69
{
70
printf("Some errors appear when you input number,");
71
printf("\nPlease input again:");
72
ch=correntchar();
73
}
74![]()
75
return ch;
76
}
77
void operate(float m,float n,char ch)
78
{
79
//char c=ch;
80
switch(ch)
81
{
82
case 'a': printf("\n%f+%f=%f",m,n,m+n);//return m+n;
83
break;
84
case 'm': printf("\n%f*%f=%f",m,n,m*n);//return m*n;
85
break;
86
case 's': printf("\n%f-%f=%f",m,n,m-n);//return m-n;
87
break;
88
}
89
}
不支持帖C的,只有选C#顶替啦!
#include <stdio.h>2
float correntfloat(void);3
char correntchar(void);4
char choice(void);5
void operate(float,float,char);6
int main(void)7
{8
char op;9
float m,n;10
op=choice();11

12
while(op!='q')13
{14
printf("\nEnter the first number:");15
m=correntfloat();16
printf("\nEnter the second number:");17
n=correntfloat();18

19
while((op=='d')||(n==0.0))20
{21
printf("\nEnter a number than 0:");22
n=correntfloat();23
}24
if(op=='d')25
printf("\n%f/%f=%f\n",m,n,m/n);26
else27
operate(m,n,op);28
op=choice();29
}30
printf("\nBye!");31

32
return 0;33
}34
float correntfloat(void)35
{36
float num;37
char ch;38
while(scanf("%f",&num)!=1)39
{40
while((ch=getchar())!='\n')41
continue;42
putchar(ch);43
printf(" is not float.please enter such as 2.5,-1.735 or 3:");44
}45

46
while((ch=getchar())!='\n') //warning:very important to jump the '\n',47
continue;48

49
return num;50
}51
char correntchar(void)52
{53
char ch;54
ch=getchar();55
while(getchar()!='\n')56
continue;57
return ch;58
}59
char choice()60
{61
char ch;62
printf("Enter the operation of your choice:");63
printf("\na. add s, subtract");64
printf("\nm.multiply d.divide");65
printf("\nq.quit\n");66

67
ch=correntchar();68
while((ch!='a')&&(ch!='s')&&(ch!='m')&&(ch!='d')&&(ch!='q'))69
{70
printf("Some errors appear when you input number,");71
printf("\nPlease input again:");72
ch=correntchar();73
}74

75
return ch;76
}77
void operate(float m,float n,char ch)78
{79
//char c=ch;80
switch(ch)81
{82
case 'a': printf("\n%f+%f=%f",m,n,m+n);//return m+n;83
break;84
case 'm': printf("\n%f*%f=%f",m,n,m*n);//return m*n;85
break;86
case 's': printf("\n%f-%f=%f",m,n,m-n);//return m-n;87
break;88
}89
}


浙公网安备 33010602011771号