【问题标题】:Convert numbers (up to 4 digit) into words using switch statement ( C program)使用 switch 语句将数字(最多 4 位)转换为单词(C 程序)
【发布时间】:2020-02-22 22:29:26
【问题描述】:

切换语句 将数字转换为单词 (3221) = 三千二百二十一。

【问题讨论】:

  • 嗨,欢迎来到 Stack Overflow。这是一个编程问答的地方,而不是你可以给出程序描述和索要代码的地方(即不是“免费编码服务”)。
  • 请添加您已经尝试过的代码(您自己的)。访问 SO 帮助中心并查看“询问”部分以获取更多指南,包括如何生成“最小可重现示例”>>>stackoverflow.com/help/asking

标签: c type-conversion switch-statement digits


【解决方案1】:

此 C 程序以 word 格式打印 0 到 9999 范围内的数字。只需将数字作为字符串发送到 main() 中的 convert_to_words() 函数中的参数,程序将计算数字的长度并使用后续数组根据个位、十分位、百分位和千分位来格式化单词。请记住,此程序中使用的条件/决策结构是 if-else 语句而不是 switch-case;如果你想编辑它以使用switch-case,它会相对简单。

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

/* A function that prints given number in words */
void convert_to_words(char *num) 
{ 
    int len = strlen(num); // Get number of digits in given number 

    /* Base cases */
    if (len == 0) { 
        fprintf(stderr, "empty string\n"); 
        return; 
    } 
    if (len > 4) { 
        fprintf(stderr, "Length more than 4 is not supported\n"); 
        return; 
    } 

    /* The first string is not used, it is to make 
        array indexing simple */
    char *single_digits[] = { "zero", "one", "two",  
                           "three", "four","five",  
                           "six", "seven", "eight", "nine"}; 

    /* The first string is not used, it is to make  
        array indexing simple */
    char *two_digits[] = {"", "ten", "eleven", "twelve",  
                           "thirteen", "fourteen", 
                           "fifteen", "sixteen",  
                           "seventeen", "eighteen", "nineteen"}; 

    /* The first two string are not used, they are to make  
        array indexing simple */
    char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty", 
                        "sixty", "seventy", "eighty", "ninety"}; 

    char *tens_power[] = {"hundred", "thousand"}; 

    /* Used for debugging purpose only */
    printf("\n%s: ", num); 

    /* For single digit number */
    if (len == 1) { 
        printf("%s\n", single_digits[*num - '0']); 
        return; 
    } 

    /* Iterate while num is not '\0' */
    while (*num != '\0') { 

        /* Code path for first 2 digits */
        if (len >= 3) { 
            if (*num -'0' != 0) { 
                printf("%s ", single_digits[*num - '0']); 
                printf("%s ", tens_power[len-3]); // here len can be 3 or 4 
            } 
            --len; 
        } 

        /* Code path for last 2 digits */
        else { 
            /* Need to explicitly handle 10-19. Sum of the two digits is 
            used as index of "two_digits" array of strings */
            if (*num == '1') { 
                int sum = *num - '0' + *(num + 1)- '0'; 
                printf("%s\n", two_digits[sum]); 
                return; 
            } 

            /* Need to explicitely handle 20 */
            else if (*num == '2' && *(num + 1) == '0') { 
                printf("twenty\n"); 
                return; 
            } 

            /* Rest of the two digit numbers i.e., 21 to 99 */
            else { 
                int i = *num - '0'; 
                printf("%s ", i? tens_multiple[i]: ""); 
                ++num; 
                if (*num != '0') 
                    printf("%s ", single_digits[*num - '0']); 
            } 
        } 
        ++num; 
    } 
} 

/* Driver program to test above function */
int main(void) 
{ 
    convert_to_words("9923"); 
    convert_to_words("523"); 
    convert_to_words("89"); 
    convert_to_words("8989"); 

    return 0; 
}

输出:

9923: nine thousand nine hundred twenty three
523: five hundred twenty three
89: eighty nine
8989: eight thousand nine hundred eighty nine

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多