主题:接收字母输入的函数,非首次输入字母时需要输入两次
get_choice函数要求输入字母A、B、C、D、Q或者a、b、c、d、q,其他均为非法输入,返回字母a、b、c、d、q。
[code=c]
int get_choice(void)
{
int ch;
printf("Please the operation of your choice:\n");
printf("A.add \tB.subtract\n");
printf("C.multiply\tD.divide\n");
printf("Q.quit\n");
ch=get_first();
while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&ch!='q'&&ch!='Q'){
/*使用while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&(ch!='q'||ch!='Q')),
*则不能响应字母“Q”和“q”,属于非法输入
*/
printf("Please input A, B, C, D, Q or a, b, c, d, q.\n");
//不是首次输入的话,任何输入都需要输入两次
ch=get_first();
}
if((ch>='A'&&ch<='D')||ch=='Q')
ch+=32;
return ch;
}
[/code]
在第二次以及其后输入字母的时候,不管输入其他什么都会提示“Please input A, B, C, D, Q or a, b, c, d, q.”,需要再次输入才能进行判断。
还有,语句while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&(ch!='q'||ch!='Q'))跟语句while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&ch!='q'&&ch!='Q')在逻辑上不是一样的么?怎么前者就无法接受Q和q呢?
这是输出结果:
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
a
Please input a float number: 2
Please input a float number: 3
2.0 + 3.0 = 5.0.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
b
Please input A, B, C, D, Q or a, b, c, d, q.
b
Please input a float number: 6
Please input a float number: 4
6.0 - 4.0 = 2.0.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
c
Please input A, B, C, D, Q or a, b, c, d, q.
c
Please input a float number: 3
Please input a float number: 0
3.0 * 0.0 = 0.0.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
d
Please input A, B, C, D, Q or a, b, c, d, q.
d
Please input a float number: 6
Please input a float number: 8
6.0 / 8.0 = 0.8.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
q
Please input A, B, C, D, Q or a, b, c, d, q.
q
[code=c]
int get_choice(void)
{
int ch;
printf("Please the operation of your choice:\n");
printf("A.add \tB.subtract\n");
printf("C.multiply\tD.divide\n");
printf("Q.quit\n");
ch=get_first();
while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&ch!='q'&&ch!='Q'){
/*使用while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&(ch!='q'||ch!='Q')),
*则不能响应字母“Q”和“q”,属于非法输入
*/
printf("Please input A, B, C, D, Q or a, b, c, d, q.\n");
//不是首次输入的话,任何输入都需要输入两次
ch=get_first();
}
if((ch>='A'&&ch<='D')||ch=='Q')
ch+=32;
return ch;
}
[/code]
在第二次以及其后输入字母的时候,不管输入其他什么都会提示“Please input A, B, C, D, Q or a, b, c, d, q.”,需要再次输入才能进行判断。
还有,语句while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&(ch!='q'||ch!='Q'))跟语句while((ch<'a'||ch>'d')&&(ch<'A'||ch>'D')&&ch!='q'&&ch!='Q')在逻辑上不是一样的么?怎么前者就无法接受Q和q呢?
这是输出结果:
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
a
Please input a float number: 2
Please input a float number: 3
2.0 + 3.0 = 5.0.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
b
Please input A, B, C, D, Q or a, b, c, d, q.
b
Please input a float number: 6
Please input a float number: 4
6.0 - 4.0 = 2.0.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
c
Please input A, B, C, D, Q or a, b, c, d, q.
c
Please input a float number: 3
Please input a float number: 0
3.0 * 0.0 = 0.0.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
d
Please input A, B, C, D, Q or a, b, c, d, q.
d
Please input a float number: 6
Please input a float number: 8
6.0 / 8.0 = 0.8.
Please the operation of your choice:
A.add B.subtract
C.multiply D.divide
Q.quit
q
Please input A, B, C, D, Q or a, b, c, d, q.
q