1082 Read Number in Chinese (25point(s)) - C語言 PAT 甲級

1082 Read Number in Chinese (25point(s))

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input:

-123456789

Sample Output:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input:

100800

Sample Output:

yi Shi Wan ling ba Bai

題目大意:

輸入一個整數,輸出這個整數的漢語讀法

設計思路:

主要是 ling 的處理,和位的處理

  • 一個字符數組記錄數字,一個字符數組記錄位
    • 字符數組第 0 位爲空是因爲使其下標從 1 開始用
    • 字符數組最後一位爲空是因爲個位不輸出
  • 把輸入的數字,先處理正負號,然後用前導 0 填充滿 10 位數,
  • 每四位爲一組,以輸出特殊的億位,萬位,個位
  • 輸出位信息前一定會先輸出數字信息,所以在位的字符數組中提前加入空格,輸出時就不用再考慮空格了,而個位的位爲空
  • 輸出過程中注意 ling 的判斷,多個連續的 0,要符合漢字讀法,省略成一個 ling
編譯器:C (gcc)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
        char *num[]={"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
        char *wei[]={"", " Yi", " Qian", " Bai", " Shi", " Wan", " Qian", " Bai", " Shi", ""};
        char s[11] = {0};
        int i, j, len, flag;

        gets(s);
        if (atoi(s) == 0) {
                printf("%s", num[0]);
        } else {
                if (s[0] == '-') {
                        printf("Fu ");
                        s[0] = '0';
                }
                len = strlen(s);
                for (i = len - 1, j = 9; i >= 0; i--, j--)
                        s[j] = s[i];
                for (;j >= 0; j--)
                        s[j] = '0';

                for (i = 0; s[i] == '0'; i++)
                        ;
                printf("%s%s", num[s[i] - '0'], wei[i]);
                flag = 0;
                for (i++; i <= 9; i++) {
                        if (i % 4 == 1) {
                                if (s[i] == '0') {
                                        printf("%s", wei[i]);
                                } else {
                                        if (flag == 1) {
                                                printf(" ling %s%s", num[s[i] - '0'], wei[i]);
                                                flag = 0;
                                        } else {
                                                printf(" %s%s", num[s[i] - '0'], wei[i]);
                                        }
                                }
                        } else {
                                if (s[i] == '0') {
                                        flag = 1;
                                } else {
                                        if (flag == 1) {
                                                printf(" ling %s%s", num[s[i] - '0'], wei[i]);
                                                flag = 0;
                                        } else {
                                                printf(" %s%s", num[s[i] - '0'], wei[i]);
                                        }
                                }
                        }
                }

        }
        return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章