要求输入一串低于 10 位的数字,输出这串数字的中文大写

要求输入一串低于 10 位的数字,输出这串数字的中文大写

input:100            output:一佰
input:1001010        output:壹佰万壹仟零壹拾

 C语言版:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void chineseDigits(char num[]);
 5 int myAtoi(char num[], int len);
 6 void move(char num[]);
 7 
 8 char strNumber[][sizeof("")] = {"","","","","","","","","","",""};
 9 char numberOfWeight[][sizeof("")] = {"", "", "", "", "", "", "", "", "亿"};
10 
11 int main(int argc, char const *argv[])
12 {
13     char num[10] = "";
14     do {
15         scanf("%9s", num);
16         if ( num[0] != '-' ) {
17             chineseDigits(num);
18         }
19     } while ( num[0] != '-');
20     
21     return 0;
22 }
23 
24 void chineseDigits(char num[])
25 {
26     while ( num[0] != '\0' ) {
27         if ( myAtoi(num, strlen(num)) > 0 ) {    // C99 可以用 atoi(char *)
28             if ( num[0] == '0' ) {
29                 if ( strlen(num) <= 4 )
30                     printf("%s", strNumber[num[0]-'0']);
31             } else {
32                 printf("%s", strNumber[num[0]-'0']);
33             }
34 
35             if ( num[0] > 0 || strlen(num) > 4 )
36                 printf("%s", numberOfWeight[strlen(num)-1]);
37         }
38         move(num);
39 
40         while ( num[0] == '0' && num[1] == '0') {
41             move(num);
42         }
43     }
44     printf("\n");
45 }
46 
47 int myAtoi(char num[], int len)    // 字符串-->数字
48 {
49     int number = 0;
50     for ( int i = 0; i < len; i++ ) {
51         if ( num[i] != '0' ) {
52             number = number * 10 + (num[i] - '0');
53         }
54     }
55     return number;
56 }
57 
58 void move(char num[]) // 挤掉最左边的那一个字符
59 {
60     int len = strlen(num);
61     for ( int i = 0; i <= len; i++ ) {
62         num[i] = num[i+1];
63     }
64 }

 

 

JavaScript版:

 1 var strNumber = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖','拾'];
 2 var numberOfWeight = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿'];
 3 
 4 var num = "10001";
 5 chineseNumber(num);
 6 
 7 function chineseNumber( num ) {
 8     var onum = num;
 9     if ( onum == 0 ) {
10         print(strNumber[num[0]]);
11         return;
12     }
13     while ( num != '' ) {
14         if ( num > 0 ) {
15             if ( num[0] == 0 ) {
16                 if ( num.length <= 4 )
17                     print(strNumber[num[0]]);
18             } else {
19                 print(strNumber[num[0]]);
20             }
21 
22             if ( num[0] > 0 || num.length > 4)
23                 print(numberOfWeight[num.length-1]);
24         }
25         num = num.substring(1);
26         while ( num[0] == 0 && num[1] == 0) {
27             num = num.substring(1);
28         }
29     }
30 }
31 
32 function print( s ) {
33     document.write(s);
34 }

 

posted @ 2018-08-15 11:15  墨鱼茄子  阅读(1254)  评论(0编辑  收藏  举报