点击(此处)折叠或打开
- /*
- *输入12,如何转换成壹拾贰;输入1020,转成壹千零贰拾
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void get4num(char *dest, char *src);
- void output4num(char *src);
- int output_unit(int left_bits);
- char *my_array[10] = {"零","壹","贰","叁","肆",\
- "伍","陆","柒","捌","玖"};
- char *my_unit[5] = {"拾","佰","仟","万","亿"};
- int FLAG_FIRST;
- int
- main(int argc, char *argv[])
- {
- /*test my_array*/
- int i;
- for(i=0; i<10; i++){
- printf("%s\n", *(my_array+i));
- }
- char input_num[100];
- printf("input_num:\n");
- scanf("%s", input_num);
- printf("%s\n", input_num);
-
- int input_len = strlen(input_num);
- char nums_4[6];
- /*output first nums*/
- FLAG_FIRST = 1;
- int first_num = input_len % 4;
- if(first_num != 0){
- strncpy(nums_4, input_num, first_num);
- output4num(nums_4);
- output_unit(input_len);
- }
- /*output the left 4 nums*/
- FLAG_FIRST = 1;
- for(i=first_num; i<input_len; i+=4){
- get4num(nums_4, input_num+i);
- output4num(nums_4);
- output_unit(input_len-i);
- FLAG_FIRST = 0;
- }
- printf("\0");
- return 0;
- }
- /*
- *get 4 numbers
- */
- void
- get4num(char *dest, char *src)
- {
- int i = 4;
- strncpy(dest, src, i);
- }
- /*
- * output 4 numbers
- */
- void
- output4num(char *src)
- {
- int i, j;
- int num, nums[4];
- num = atoi(src);
- j = strlen(src);
- /*low number is at low place*/
- for(i=0; i<4; i++){
- nums[i] = (num % 10);
- num /= 10;
- }
- /*output first nums*/
- if(nums[j-1] == 0){
- if((j == 4) && (FLAG_FIRST == 0)){
- printf("%s", *(my_array+nums[j-1]));
- }
- }else{
- printf("%s", *(my_array+nums[j-1]));
- printf("%s", *(my_unit+j-2));
- }
- /*output number and unit*/
- for(i=j-2; i>=1; i--){
- if(nums[i] == 0){
- if(nums[i+1] != 0){
- printf("%s", *(my_array+nums[i]));
- }
- }else{
- printf("%s", *(my_array+nums[i]));
- printf("%s", *(my_unit+i-1));
- }
- }
- if(nums[0] != 0){
- printf("%s", *(my_array+nums[i]));
- }
- }
- /*
- * output the unit of 4 nums
- */
- int
- output_unit(int left_bits)
- {
- int m, n;
- m = (left_bits-1) / 4;
- switch(m){
- case 0:
- return 0;
- case 1:
- printf("%s", my_unit[3]);
- return 0;
- default:
- for(n=m; n>=2; n--){
- printf("%s", my_unit[4]);
- }
- }
- printf(",");
- }
看到有用shell script写的,。我尝试用C语言写下,终于拼凑出来了。
input_num:
9283749817509843219827409812365908
9283749817509843219827409812365908
玖拾贰亿亿亿亿亿亿亿,捌仟叁佰柒拾肆亿亿亿亿亿亿,玖仟捌佰壹拾柒亿亿亿亿亿,伍仟零玖拾捌亿亿亿亿,肆仟叁佰贰拾壹亿亿亿,玖仟捌佰贰拾柒亿亿,肆仟零玖拾捌亿,壹仟贰佰叁拾陆万伍仟玖佰零捌
/////////////////////////////////////////////////////////////////////////////////
2012.12.1
非常感谢楼下的回复,陪我写玩具程序。
发现我上面的程序每4位输出的单位上万亿后都是错的。把重写了int output_unit(int left_bits),还有修改了穿过来的left_bits是4的整数倍。
根据人的读数习惯(以4个位数为一个单元),第一感觉也就这么写了。相信还有跟好的方法。
2楼的就是根据每位数来判断单位,不过我编译运行你的程序后,输出是乱码,这个我没有搞明白。我可是很想抓你小辫子哦!哇咔咔~对你再次表示感谢。
- /*
- *输入12,如何转换成壹拾贰,输入1020,转成壹千零贰拾
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void get4num(char *dest, char *src);
- void output4num(char *src);
- int output_unit(int left_bits);
- char *my_array[10] = {"零","壹","贰","叁","肆",\
- "伍","陆","柒","捌","玖"};
- char *my_unit[5] = {"拾","佰","仟","万","亿"};
- int FLAG_FIRST;
- int
- main(int argc, char *argv[])
- {
- /*test my_array*/
- int i;
- for(i=0; i<10; i++){
- printf("%s\n", *(my_array+i));
- }
- char input_num[100];
- printf("input_num:\n");
- scanf("%s", input_num);
- printf("%s\n", input_num);
-
- int input_len = strlen(input_num);
- char nums_4[6];
- /*output first nums*/
- FLAG_FIRST = 1;
- int first_num = input_len % 4;
- if(first_num != 0){
- strncpy(nums_4, input_num, first_num);
- output4num(nums_4);
- output_unit(input_len - first_num);
- }
- /*output the left 4 nums*/
- FLAG_FIRST = 1;
- for(i=first_num; i<input_len; i+=4){
- get4num(nums_4, input_num+i);
- output4num(nums_4);
- output_unit(input_len - i - 4);
- FLAG_FIRST = 0;
- }
- printf("\0\0");
- return 0;
- }
- /*
- *get 4 numbers
- */
- void
- get4num(char *dest, char *src)
- {
- int i = 4;
- strncpy(dest, src, i);
- }
- /*
- * output 4 numbers
- */
- void
- output4num(char *src)
- {
- int i, j;
- int num, nums[4];
- num = atoi(src);
- j = strlen(src);
- /*low number is at low place*/
- for(i=0; i<4; i++){
- nums[i] = (num % 10);
- num /= 10;
- }
- /*output first nums*/
- if(nums[j-1] == 0){
- if((j == 4) && (FLAG_FIRST == 0)){
- printf("%s", *(my_array+nums[j-1]));
- }
- }else{
- printf("%s", *(my_array+nums[j-1]));
- printf("%s", *(my_unit+j-2));
- }
- /*output number and unit*/
- for(i=j-2; i>=1; i--){
- if(nums[i] == 0){
- if(nums[i+1] != 0){
- printf("%s", *(my_array+nums[i]));
- }
- }else{
- printf("%s", *(my_array+nums[i]));
- printf("%s", *(my_unit+i-1));
- }
- }
- if(nums[0] != 0){
- printf("%s", *(my_array+nums[i]));
- }
- }
- /*
- * output the unit of 4 nums
- */
- int
- output_unit(int left_bits)
- {
- int m, n;
- /*the last one*/
- if(left_bits < 4){
- return 0;
- } else{
- m = left_bits % 8;
- switch(m){
- case 4:
- printf("%s", my_unit[3]);
- break;
- case 0:
- n = left_bits / 8;
- for(m=0; m<n; m++){
- printf("%s", my_unit[4]);
- }
- }
- }
- printf(",");
- return 0;
- }
input_num:
123456789012345678901234567890
123456789012345678901234567890
壹拾贰万,叁仟肆佰伍拾陆亿亿亿,柒仟捌佰玖拾万,壹仟贰佰叁拾肆亿亿,伍仟陆佰柒拾捌万,玖仟零壹拾贰亿,叁仟肆佰伍拾陆万,柒仟捌佰玖拾
////////////////////////////////////////////////////////////////////
2012.12.2
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char *my_array[10] = {"零","壹","贰","叁","肆",\
- "伍","陆","柒","捌","玖"};
- char *my_unit[5] = {"拾","佰","仟","万","亿"};
- void
- output_unit(int position)
- {
- if(position == 1){
- return;
- }
- int left_bits = position -1;
- int i, many_yi;
- int case_unit;
- if(left_bits % 8 == 0){
- many_yi = left_bits / 8;
- for(i=0; i<many_yi; i++){
- printf("%s", my_unit[4]);
- }
- printf(";");
- } else{
- case_unit = left_bits % 4;
- switch(case_unit){
- case 0:
- printf("%s,", my_unit[3]);
- break;
- case 1:
- printf("%s", my_unit[0]);
- break;
- case 2:
- printf("%s", my_unit[1]);
- break;
- case 3:
- printf("%s", my_unit[2]);
- break;
- }
- }
- }
- int
- str_convert(char *src)
- {
- int i;
- /*check the input string*/
- int src_len = strlen(src);
- if(src_len < 1){
- printf("dont't have enough digital");
- }
- /*keep the string is legal*/
- for(i=0; i<src_len; i++){
- if(*(src+i) >= '0' && *(src+i) <= '9'){
- continue;
- } else{
- printf("the number is illegal\n");
- return 0;
- }
- }
- /*output Chinese*/
- int first_bit = 0;
- for(i=0; i<src_len && *(src+i) == '0'; i++){
- ;
- }
- first_bit = i;
- char *cur_bit;
- int cur_dig;
- for(i=first_bit; i<src_len; i++){
- cur_bit = src + i;
- cur_dig = *cur_bit - '0';
- printf("%s",*(my_array + cur_dig));
- output_unit(src_len - i);
- }
- printf("\n");
- return 0;
- }
- int main(int argc, char *argv[])
- {
- char tmp[100];
- printf("input a number\n");
- scanf("%s", tmp);
- printf("src:%s\n", tmp);
- str_convert(tmp);
- fflush(stdout);
- return 0;
- }
input a number
src:00123 4567 | 8901 2345 | 6789 0123 | 4567 8901
壹佰贰拾叁万,肆仟伍佰陆拾柒亿亿亿;捌仟玖佰零拾壹万,贰仟叁佰肆拾伍亿亿;陆仟柒佰捌拾玖万,零仟壹佰贰拾叁亿;肆仟伍佰陆拾柒万,捌仟玖佰零拾壹
阅读(2625) | 评论(1) | 转发(0) |