Chinaunix首页 | 论坛 | 博客
  • 博客访问: 444329
  • 博文数量: 67
  • 博客积分: 2468
  • 博客等级: 上尉
  • 技术积分: 1050
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-05 01:21
文章分类

全部博文(67)

文章存档

2013年(1)

2012年(65)

2011年(1)

分类: C/C++

2012-11-30 20:19:09

点击(此处)折叠或打开

  1. /*
  2.  *输入12,如何转换成壹拾贰;输入1020,转成壹千零贰拾
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>

  7. void get4num(char *dest, char *src);
  8. void output4num(char *src);
  9. int output_unit(int left_bits);

  10. char *my_array[10] = {"零","壹","贰","叁","肆",\
  11.      "伍","陆","柒","捌","玖"};
  12. char *my_unit[5] = {"拾","佰","仟","万","亿"};
  13. int FLAG_FIRST;
  14. int
  15. main(int argc, char *argv[])
  16. {
  17.     /*test my_array*/
  18.     int i;
  19.     for(i=0; i<10; i++){
  20.         printf("%s\n", *(my_array+i));
  21.     }

  22.     char input_num[100];
  23.     printf("input_num:\n");
  24.     scanf("%s", input_num);
  25.     printf("%s\n", input_num);
  26.     
  27.     int input_len = strlen(input_num);
  28.     char nums_4[6];

  29.     /*output first nums*/
  30.     FLAG_FIRST = 1;
  31.     int first_num = input_len % 4;
  32.     if(first_num != 0){
  33.         strncpy(nums_4, input_num, first_num);
  34.         output4num(nums_4);
  35.         output_unit(input_len);
  36.     }

  37.     /*output the left 4 nums*/
  38.     FLAG_FIRST = 1;
  39.     for(i=first_num; i<input_len; i+=4){
  40.         get4num(nums_4, input_num+i);
  41.         output4num(nums_4);
  42.         output_unit(input_len-i);
  43.         FLAG_FIRST = 0;
  44.     }

  45.     printf("\0");
  46.     return 0;
  47. }

  48. /*
  49.  *get 4 numbers
  50.  */
  51. void
  52. get4num(char *dest, char *src)
  53. {
  54.     int i = 4;
  55.     strncpy(dest, src, i);
  56. }

  57. /*
  58.  * output 4 numbers
  59.  */
  60. void
  61. output4num(char *src)
  62. {
  63.     int i, j;
  64.     int num, nums[4];
  65.     num = atoi(src);
  66.     j = strlen(src);

  67.     /*low number is at low place*/
  68.     for(i=0; i<4; i++){
  69.         nums[i] = (num % 10);
  70.         num /= 10;
  71.     }

  72.     /*output first nums*/
  73.     if(nums[j-1] == 0){
  74.         if((j == 4) && (FLAG_FIRST == 0)){
  75.             printf("%s", *(my_array+nums[j-1]));
  76.         }
  77.     }else{
  78.         printf("%s", *(my_array+nums[j-1]));
  79.         printf("%s", *(my_unit+j-2));
  80.     }

  81.     /*output number and unit*/
  82.     for(i=j-2; i>=1; i--){
  83.         if(nums[i] == 0){
  84.             if(nums[i+1] != 0){
  85.                 printf("%s", *(my_array+nums[i]));
  86.             }
  87.         }else{
  88.             printf("%s", *(my_array+nums[i]));
  89.             printf("%s", *(my_unit+i-1));
  90.         }
  91.     }
  92.     if(nums[0] != 0){
  93.         printf("%s", *(my_array+nums[i]));
  94.     }
  95. }

  96. /*
  97.  * output the unit of 4 nums
  98.  */
  99. int
  100. output_unit(int left_bits)
  101. {
  102.     int m, n;
  103.     m = (left_bits-1) / 4;
  104.     switch(m){
  105.         case 0:
  106.             return 0;
  107.         case 1:
  108.             printf("%s", my_unit[3]);
  109.             return 0;
  110.         default:
  111.             for(n=m; n>=2; n--){
  112.                 printf("%s", my_unit[4]);
  113.             }
  114.     }

  115.     printf(",");
  116. }
看到有用shell script写的,。我尝试用C语言写下,终于拼凑出来了。
input_num:
9283749817509843219827409812365908
9283749817509843219827409812365908
玖拾贰亿亿亿亿亿亿亿,捌仟叁佰柒拾肆亿亿亿亿亿亿,玖仟捌佰壹拾柒亿亿亿亿亿,伍仟零玖拾捌亿亿亿亿,肆仟叁佰贰拾壹亿亿亿,玖仟捌佰贰拾柒亿亿,肆仟零玖拾捌亿,壹仟贰佰叁拾陆万伍仟玖佰零捌

/////////////////////////////////////////////////////////////////////////////////

2012.12.1
非常感谢楼下的回复,陪我写玩具程序。

发现我上面的程序每4位输出的单位上万亿后都是错的。把重写了int output_unit(int left_bits),还有修改了穿过来的left_bits是4的整数倍。
根据人的读数习惯(以4个位数为一个单元),第一感觉也就这么写了。相信还有跟好的方法。
2楼的就是根据每位数来判断单位,不过我编译运行你的程序后,输出是乱码,这个我没有搞明白。我可是很想抓你小辫子哦!哇咔咔~对你再次表示感谢。

点击(此处)折叠或打开

  1. /*
  2.  *输入12,如何转换成壹拾贰,输入1020,转成壹千零贰拾
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>

  7. void get4num(char *dest, char *src);
  8. void output4num(char *src);
  9. int output_unit(int left_bits);

  10. char *my_array[10] = {"零","壹","贰","叁","肆",\
  11.                           "伍","陆","柒","捌","玖"};
  12. char *my_unit[5] = {"拾","佰","仟","万","亿"};
  13. int FLAG_FIRST;
  14. int
  15. main(int argc, char *argv[])
  16. {
  17.         /*test my_array*/
  18.         int i;
  19.         for(i=0; i<10; i++){
  20.                 printf("%s\n", *(my_array+i));
  21.         }

  22.         char input_num[100];
  23.         printf("input_num:\n");
  24.         scanf("%s", input_num);
  25.         printf("%s\n", input_num);
  26.         
  27.         int input_len = strlen(input_num);
  28.         char nums_4[6];

  29.         /*output first nums*/
  30.         FLAG_FIRST = 1;
  31.         int first_num = input_len % 4;
  32.         if(first_num != 0){
  33.                 strncpy(nums_4, input_num, first_num);
  34.                 output4num(nums_4);
  35.                 output_unit(input_len - first_num);
  36.         }

  37.         /*output the left 4 nums*/
  38.         FLAG_FIRST = 1;
  39.         for(i=first_num; i<input_len; i+=4){
  40.                 get4num(nums_4, input_num+i);
  41.                 output4num(nums_4);
  42.                 output_unit(input_len - i - 4);
  43.                 FLAG_FIRST = 0;
  44.         }

  45.         printf("\0\0");
  46.         return 0;
  47. }

  48. /*
  49.  *get 4 numbers
  50.  */
  51. void
  52. get4num(char *dest, char *src)
  53. {
  54.         int i = 4;
  55.         strncpy(dest, src, i);
  56. }

  57. /*
  58.  * output 4 numbers
  59.  */
  60. void
  61. output4num(char *src)
  62. {
  63.         int i, j;
  64.         int num, nums[4];
  65.         num = atoi(src);
  66.         j = strlen(src);

  67.         /*low number is at low place*/
  68.         for(i=0; i<4; i++){
  69.                 nums[i] = (num % 10);
  70.                 num /= 10;
  71.         }

  72.         /*output first nums*/
  73.         if(nums[j-1] == 0){
  74.                 if((j == 4) && (FLAG_FIRST == 0)){
  75.                         printf("%s", *(my_array+nums[j-1]));
  76.                 }
  77.         }else{
  78.                 printf("%s", *(my_array+nums[j-1]));
  79.                 printf("%s", *(my_unit+j-2));
  80.         }

  81.         /*output number and unit*/
  82.         for(i=j-2; i>=1; i--){
  83.                 if(nums[i] == 0){
  84.                         if(nums[i+1] != 0){
  85.                                 printf("%s", *(my_array+nums[i]));
  86.                         }
  87.                 }else{
  88.                         printf("%s", *(my_array+nums[i]));
  89.                         printf("%s", *(my_unit+i-1));
  90.                 }
  91.         }
  92.         if(nums[0] != 0){
  93.                 printf("%s", *(my_array+nums[i]));
  94.         }
  95. }

  96. /*
  97.  * output the unit of 4 nums
  98.  */
  99. int
  100. output_unit(int left_bits)
  101. {
  102.         int m, n;
  103.         /*the last one*/
  104.         if(left_bits < 4){
  105.                 return 0;
  106.         } else{
  107.                 m = left_bits % 8;
  108.                 switch(m){
  109.                         case 4:
  110.                                 printf("%s", my_unit[3]);
  111.                                 break;
  112.                         case 0:
  113.                                 n = left_bits / 8;
  114.                                 for(m=0; m<n; m++){
  115.                                         printf("%s", my_unit[4]);
  116.                                 }
  117.                 }
  118.         }

  119.         printf(",");
  120.         return 0;
  121. }
input_num:
123456789012345678901234567890
123456789012345678901234567890
壹拾贰万,叁仟肆佰伍拾陆亿亿亿,柒仟捌佰玖拾万,壹仟贰佰叁拾肆亿亿,伍仟陆佰柒拾捌万,玖仟零壹拾贰亿,叁仟肆佰伍拾陆万,柒仟捌佰玖拾

////////////////////////////////////////////////////////////////////
2012.12.2

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. char *my_array[10] = {"零","壹","贰","叁","肆",\
  5.      "伍","陆","柒","捌","玖"};
  6. char *my_unit[5] = {"拾","佰","仟","万","亿"};

  7. void
  8. output_unit(int position)
  9. {
  10.         if(position == 1){
  11.                 return;
  12.         }

  13.         int left_bits = position -1;
  14.         int i, many_yi;
  15.         int case_unit;
  16.         if(left_bits % 8 == 0){
  17.                 many_yi = left_bits / 8;
  18.                 for(i=0; i<many_yi; i++){
  19.                         printf("%s", my_unit[4]);
  20.                 }
  21.                 printf(";");
  22.         } else{
  23.                 case_unit = left_bits % 4;
  24.                 switch(case_unit){
  25.                     case 0:
  26.                                 printf("%s,", my_unit[3]);
  27.                                 break;
  28.                     case 1:
  29.                         printf("%s", my_unit[0]);
  30.                             break;
  31.                     case 2:
  32.                         printf("%s", my_unit[1]);
  33.                         break;
  34.                     case 3:
  35.                         printf("%s", my_unit[2]);
  36.                             break;
  37.                 }
  38.         }
  39. }

  40. int
  41. str_convert(char *src)
  42. {
  43.         int i;
  44.         /*check the input string*/
  45.         int src_len = strlen(src);
  46.         if(src_len < 1){
  47.                 printf("dont't have enough digital");
  48.         }

  49.         /*keep the string is legal*/
  50.         for(i=0; i<src_len; i++){
  51.                 if(*(src+i) >= '0' && *(src+i) <= '9'){
  52.                         continue;
  53.                 } else{
  54.                         printf("the number is illegal\n");
  55.                         return 0;
  56.                 }
  57.         }

  58.         /*output Chinese*/
  59.         int first_bit = 0;
  60.         for(i=0; i<src_len && *(src+i) == '0'; i++){
  61.                 ;
  62.         }
  63.         first_bit = i;

  64.         char *cur_bit;
  65.         int cur_dig;
  66.         for(i=first_bit; i<src_len; i++){
  67.                 cur_bit = src + i;
  68.                 cur_dig = *cur_bit - '0';
  69.                 printf("%s",*(my_array + cur_dig));
  70.                 output_unit(src_len - i);
  71.         }

  72.         printf("\n");
  73.         return 0;
  74. }

  75. int main(int argc, char *argv[])
  76. {
  77.         char tmp[100];
  78.         printf("input a number\n");
  79.         scanf("%s", tmp);
  80.         printf("src:%s\n", tmp);
  81.         str_convert(tmp);

  82.         fflush(stdout);
  83.         return 0;
  84. }

input a number
src:00123 4567 | 8901 2345 | 6789 0123 | 4567 8901
壹佰贰拾叁万,肆仟伍佰陆拾柒亿亿亿;捌仟玖佰零拾壹万,贰仟叁佰肆拾伍亿亿;陆仟柒佰捌拾玖万,零仟壹佰贰拾叁亿;肆仟伍佰陆拾柒万,捌仟玖佰零拾壹

阅读(2604) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

时间看来2012-11-30 20:22:35

看到有用shell script写的,我尝试用C语言写下,终于拼凑出来了。
http://www.linuxsir.org/bbs/thread47558.html