[英]4-bit BCD to 7-segment
我正在 C 中制作一个 4 位 BCD 解码器到7 段显示器。
显示 output 后,我询问用户是否要再次输入。 可以显示想要的output,不想再输入时可以退出程序。
问题是当我想再次输入时,它不会停止询问我的输入并打印以前的输入。
这是 output:
This program displays the decimal equivalent of a
4-bit binary-coded decimal input on a 7-segment LED Display.
Please enter a 4-bit binary-coded decimal: 1000
You've entered 1000.
The decimal equivalent is 8.
_
|_|
LED Display: |_|
Would you like to input another 4-bit BCD?
Enter [1] if yes or [0] if no. 1
Please enter a 4-bit binary-coded decimal: You've entered 1000.
The decimal equivalent is 8.
_
|_|
LED Display: |_|
Would you like to input another 4-bit BCD?
Enter [1] if yes or [0] if no.
这是我的代码:
int main() {
int i, dec, retry = 1; //declare integers i ,dec, and retry
int bit[4]; //declare integer array with 4 elements
unsigned char input[5]; //declare string with 5 elements
printf("This program displays the decimal equivalent of a\n 4-bit binary-coded decimal input on a 7-segment LED Display.\n\n");
do {
printf("Please enter a 4-bit binary-coded decimal: "); //instructs user to enter 4-bit bcd
scanf("%[^\n]%*c", input); //read string
printf("You've entered %s.\n\n", input); //shows the 4-bit bcd input
for (i=0; i<5; i++) {
bit[i] = input[i] - '0';
}
dec = bit[0]*8 + bit[1]*4 +bit[2]*2 + bit[3];
printf("The decimal equivalent is %d.\n\n", dec); //shows decimal equivalent
switch (dec) { //displays 7-segment display depending on the value of dec
case 0:
printf(" _ \n | |\nLED Display: |_|\n");
break;
...
}
printf("\n\nWould you like to input another 4-bit BCD?\nEnter [1] if yes or [0] if no. ");
scanf("%d", &retry);
}
while (retry == 1);
}
我在使用fgets(input, sizeof(input), stdin);
时也遇到了同样的问题而不是scanf("%[^\n]%*c", input);
在输入1\n
后,为了 go 在下一个7 段号上, scanf()
仅捕获1
并且\n
留下了后续的一个。
但是格式%[^\n]
告诉scanf()
不匹配任何以\n
开头的输入,因此剩余的内容使其返回没有任何值 scanf'd。 检查它的返回值你会发现第二次是 0,所以变量没有更新,这就是使用前一个值的原因。
为了修复它,由于您需要%[^\n]
来修剪输入(您只需要数字),因此只需在当前输入格式之前添加一个空格:
scanf(" %[^\n]%*c", input);
这样,任何空白字符( isspace()返回 true 的所有字符,包括'\n'
、 ' '
和'\t'
)都会被消耗。
请注意,添加
fflush(stdin);
在您的第一个scanf()
是标准 C 的未定义行为之前。 在某些环境中它可以工作,但应该避免这种解决方案,至少可以说,它使代码不可移植!
注意:您与fgets()
具有相同的行为,因为它会读取直到找到\n
为止,并且就像之前使用scanf
发生的那样,这正是先前用户输入插入中剩余的字符。
我建议使用不同的方法来检查您的输入,而不是使用%[^\n]
格式:
while( 1 )
{
printf("Please enter a 4-bit binary-coded decimal: "); //instructs user to enter 4-bit bcd
scanf(" %4s", input); //read string
if( validateBinaryInput( input, 4 ) == 0 )
{
break;
}
else
{
printf( "Wrong input!\n" );
}
}
使用scanf(" %4s", input);
您确保准确捕获四个字符,并将它们存储在input
数组中。 该循环确保只要输入无效,输入序列就会重复。 可以使用validateBinaryInput()
function 检查输入的有效性。 请在下面找到一个实现示例:
/* Returns 0 on success, -1 on failure */
int validateBinaryInput( unsigned char *input, size_t len )
{
int ret = 0;
if( !input )
{
ret = -1;
}
else
{
int i;
for( i=0; i<len; i++ )
{
if( input[i] != '0' && input[i] != '1' )
ret = -1;
}
}
return ret;
}
它不仅确保每个插入的字符都是数字,而且验证它是二进制数字。 通过这种方式,您可以避免在插入诸如5
之类的输入时发生的奇怪行为:您对
dec = bit[0]*8 + bit[1]*4 +bit[2]*2 + bit[3];
将产生值5 ,即使输入不是有效的二进制字,也会打印相应的 7 段数字。
这里的这一行是一个问题:
scanf("%[^\n]%*c", input); //read string
前一个%d
仅在格式说明符之前使用空格,而不是之后。 这意味着这个scanf
将读取一个空字符串。
要解决此问题,请在前面添加一个空格,以便使用空格:
scanf(" %[^\n]%*c", input); //read string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.