首页 > 解决方案 > 将输入的 5 位数字转换为单词

问题描述

我正在尝试编写一个程序来读取一个五位数的邮政编码,然后以单词的形式输出这些数字。该程序也只接受 5 位数字。因此,如果输入为 123,则输出将是“您输入了 3 位数字”并循环返回,直到用户输入了 5 位数字。一旦他们这样做,它将以word格式输出数字。因此,如果输入 12345,则输出将是一二三四五。这是我到目前为止所拥有的:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

int main(void) {
int ac, count = 0;

printf("Enter a 5 digit area code: ");
scanf("%d", &ac);


do {
    ac /= 10;
    ++count;

} while (ac != 0);

while (count != 5) {
        printf("You intered in %d digits.\n", count);
        printf("Enter a 5 digit area code: ");
        scanf(" %d", &ac);
}

while (count == 5) {


}

return(0);

}

有两个问题我需要帮助。第一个是只要输入一个不是五位数的数字,就正确输出一次,然后循环无限。如果我输入 123,输出将是“您输入了 3 位数字”,然后它会提示我输入另一个数字。如果我随后输入 12345,它将再次输出“您输入了 3 位数字”。我是循环新手,不确定这个问题来自哪里。

第二个问题只是从数字到单词的转换,我从来没有遇到过这个问题,我不知道从哪里开始。

标签: cloops

解决方案


正如评论部分已经指出的那样,您的代码中的逻辑不适用于以零开头的代码。

如果将代码存储为整数,则无法区分代码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执行显式语句,该语句将跳出无限循环。

通过创建一个指向字符串的指针数组,可以轻松地将单个数字转换为单词,这样第 0元素指向 string "zero",第一个元素指向"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也以 work 开头:

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

推荐阅读