正如在 cmets 部分中已经指出的,您的代码中的逻辑不适用于以零开头的代码。
如果将代码存储为整数,则无法区分代码01234 和代码1234。因此,如果您希望能够区分这两个代码,您必须至少在开始时将数字读取为字符串,而不是数字:
char buffer[100];
//prompt user for input
printf("Enter a 5 digit area code: ");
//attempt to read one line of input
if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
{
fprintf( stderr, "error reading input!\n" );
exit( EXIT_FAILURE );
}
请注意,上面的代码还需要包含头文件stdlib.h。
现在,您必须计算输入的字符数,并确认确实有 5。此外,您必须验证所有输入的字符都是数字。如果输入无效,则必须提供适当的错误消息并再次提示用户。这最好使用无限循环来完成,该循环会重复直到输入有效,此时会执行显式的break 语句,这将跳出无限循环。
通过创建指向字符串的指针数组可以轻松地将单个数字转换为单词,这样第 0th 元素指向字符串 "zero",第 1st 元素指向"one" 等:
const char * const digit_names[] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
由于 ISO C 标准保证字符集中的所有数字都是连续的(无论您使用哪个字符集),您可以简单地从字符中减去 '0'(数字 0 的字符代码)代码以便将数字从字符代码转换为0 和9 之间的实际数字。现在,您可以使用该数字索引到数组digit_names 以打印相应的单词。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main( void )
{
const char * const digit_names[] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
char buffer[100];
for (;;) //infinite loop, equivalent to while(1)
{
int i;
char *p;
//prompt user for input
printf("Enter a 5 digit area code: ");
//attempt to read one line of input
if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
{
fprintf( stderr, "Unrecoverable error reading input!\n" );
exit( EXIT_FAILURE );
}
//verify that entire line of input was read in
p = strchr( buffer, '\n' );
if ( p == NULL )
{
fprintf( stderr, "Unrecoverable error: Line too long!\n" );
exit( EXIT_FAILURE );
}
//remove newline character
*p = '\0';
//verify that exactly 5 characters were entered and that
//each characters is a digit
for ( i = 0; i < 5; i++ )
{
//verify that we are not yet at end of line
if ( buffer[i] == '\0' )
{
printf( "Too few characters!\n" );
//we cannot use "continue" here, because that would apply to
//the innermost loop, but we want to continue to the next
//iteration of the outer loop
goto continue_outer_loop;
}
//verify that character is digit
if ( !isdigit( (unsigned char)buffer[i] ) )
{
printf( "Only digits allowed!\n" );
//we cannot use "continue" here, because that would apply to
//the innermost loop, but we want to continue to the next
//iteration of the outer loop
goto continue_outer_loop;
}
}
//verify that we are now at end of line
if ( buffer[i] != '\0' )
{
printf( "Too many characters!\n" );
continue;
}
//everything is ok with user input, so we can break loop
break;
continue_outer_loop:
continue;
}
printf( "You entered this valid input: %s\n", buffer );
printf( "In words, that is: " );
for ( int i = 0; i < 5; i++ )
{
//don't print space on first iteration
if ( i != 0 )
putchar( ' ' );
//casting to "unsigned char" is necessary to prevent
//negative character codes
fputs( digit_names[ ((unsigned char)buffer[i]) - '0' ], stdout );
}
printf( "\n" );
}
这是程序的一些示例输入和输出:
Enter a 5 digit area code: 1234
Too few characters!
Enter a 5 digit area code: 123456
Too many characters!
Enter a 5 digit area code: 12h45
Only digits allowed!
Enter a 5 digit area code: 12345
You entered this valid input: 12345
In words, that is: one two three four five
此外,如您所见,以0 开头的代码也可以:
Enter a 5 digit area code: 1234
Too few characters!
Enter a 5 digit area code: 01234
You entered this valid input: 01234
In words, that is: zero one two three four