将数字以单词形式输出(C语⾔实现)
  程序简单地实现了输⼊不⼤于九位的数字,以英⽂单词的形式输出。
  在⼩的位数上使⽤了暴⼒穷举的⽅法实现数字转换为单词,使得⽐较代码冗长……
  基本⽅法是先将九位数分割成三段分别处理,不⾜九位,则⾼位补零。
定义头⽂件和函数:
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4void written_amount(int num,char *buffer);
5void written_three(int num,char *buffer);
6void written_two(int a,char *buffer);
主函数:
int main(void)
{
int num;
char *buffer;
buffer=malloc(sizeof(char *)*200) ;
printf("Enter the number:");
scanf("%d",&num);
written_amount(num,buffer);
printf("The %d's homologous string is :\n %s\n",num,buffer);
return0;
}
write_amount函数:
  先将输⼊的数字分成三部分,分别赋给num3、num2、num1;例如:输⼊的是12345,则num3=0;num2=12,num1=345;
  声明三个字符串指针,⽤来将num1、num2、num3分别转换成字符串后赋给buffer1、buffer2、buffer3.这需要调⽤
written_three函数。
  最后将buffer1、2和3分别判断是否为“zero”,不是的话将它们拼接到⼀起赋值给buffer。
3//分配三个字符串指针,⽤来接收分别来⾃num1,num2和num3的字符串
4
5char *buffer1=NULL,*buffer2=NULL,*buffer3=NULL;
6    buffer1=malloc(sizeof(char*)*40);
7    buffer2=malloc(sizeof(char*)*40);
8    buffer3=malloc(sizeof(char*)*40);
9int count=1;
10int x=num/10;
11while(x!=0){
12        count++;
13        x=x/10;
14    }
15//    printf("%d\n",count);
16int num1=0,num2=0,num3=0;
17//将要转换的数值num以每三位为⼀组分割给num1,num2,num3
18if(count>=1 && count<=3){
19        num1=num;
20    }
21else if(count>3 && count<=6){
22        num1=num%1000;
23        num2=num/1000;
24    }
25else{
26        num1=num%1000;
27        num2=(num/1000)%1000;
28        num3=num/1000000;
c++strcpy函数用法
29    }
30//调⽤written_three函数三次,分别将三个数字输出为英语字符形式
31    written_three(num1,buffer1);
32
33    written_three(num2,buffer2);
34
35    written_three(num3,buffer3);
36//将buffer1、2、3的值copy给buffer,如果为零则忽略
37if(!strcmp(buffer3,"zero")) {
38
39if(!strcmp(buffer2,"zero")){
40
41            strcpy(buffer,buffer1);
42
43        }
44else{
45            strcpy(buffer,buffer2);
46            strcat(buffer," thousand and ");
47            strcat(buffer,buffer1);
48        }
49    }
50else{
51        strcpy(buffer,buffer3);
52        strcat(buffer," million and ");
53        strcat(buffer,buffer2);
54        strcat(buffer," thousand and ");
55        strcat(buffer,buffer1);
56    }
57//接收完毕将buffer1、buffer2和buffer3连接起来copy给buffer
58 }
written_three函数:
  这个简单的函数⽤同样的原理,先判断是否满三位数,不满三位,则直接调⽤write_two函数,否则,先判断最⾼位,将其转化为字符串buffer,然后和调⽤write_two函数返回的字符串
拼接,返回结果。
3int n=num%100;
4char *p;
5    p=malloc(sizeof(char *)*40);
6//    printf("%d\n",n);
7//判断num是否是0,是的话直接返回buffer=NULL,否则判断它的位数,返回对应的英⽂字符 8if(num/100==0){
9        written_two(num,buffer);
10//        printf("%s\n",buffer);
11    }
12else {
13switch(num/100){
14case1:        strcpy(buffer,"one hundred ");        break;
15case2:        strcpy(buffer,"two hundred ");        break;
16case3:        strcpy(buffer,"three hundred ");    break;
17case4:        strcpy(buffer,"four hundred ");        break;
18case5:        strcpy(buffer,"five hundred ");        break;
19case6:        strcpy(buffer,"six hundred ");        break;
20case7:        strcpy(buffer,"seven hundred ");    break;
21case8:        strcpy(buffer,"eight hundred ");    break;
22case9:        strcpy(buffer,"nine hundred ");        break;
23        }
24        written_two(n,p);
25//        printf("%s\n",p);
26        strcat(buffer,p);
27//        printf("%s\n",buffer);
28    }
29 }
write_two函数(糟糕的穷举法,⼤神请忽略):
  将⼆位数转化为对应的字符串。
1void written_two(int a,char *buffer)
2 {
3
4int count=1,i;
5int x=a/10;
6while(x!=0){
7        count++;
8        x=x/10;
9    }
10switch(count){
11case1:{
12switch(a){
13case0:        strcpy(buffer,"zero");  break;
14case1:        strcpy(buffer,"one");    break;
15case2:        strcpy(buffer,"two");    break;
16case3:        strcpy(buffer,"three");    break;
17case4:        strcpy(buffer,"four");    break;
18case5:        strcpy(buffer,"five");    break;
19case6:        strcpy(buffer,"six");    break;
20case7:        strcpy(buffer,"seven");    break;
21case8:        strcpy(buffer,"eight");    break;
22case9:        strcpy(buffer,"nine");    break;
23            }
24        }break;
25case2:{
26if(a%10==0){
27switch(a/10){
28case1:        strcpy(buffer,"ten");        break;
29case2:        strcpy(buffer,"twenty");    break;
30case3:        strcpy(buffer,"thirty");    break;
31case4:        strcpy(buffer,"fourty");    break;
32case5:        strcpy(buffer,"fifty");        break;
33case6:        strcpy(buffer,"sixty");        break;
34case7:        strcpy(buffer,"seventy");    break;
35case8:        strcpy(buffer,"eighty");    break;
36case9:        strcpy(buffer,"ninety");    break;
37                }
38            }
39else if(a/10==1){
40switch(a%10){
41case1:        strcpy(buffer,"eleven");    break;
42case2:        strcpy(buffer,"twelve");    break;
43case3:        strcpy(buffer,"thirteen");    break;
44case4:        strcpy(buffer,"fourteen");    break;
45case5:        strcpy(buffer,"fifteen");    break;
46case6:        strcpy(buffer,"sixteen");    break;
47case7:        strcpy(buffer,"seventeen");    break;
48case8:        strcpy(buffer,"eighteen");    break;
49case9:        strcpy(buffer,"nineteen");    break;
50                }
51            }
52else {
53switch(a/10){
54case2:{
55switch(a%10){
56case1:strcpy(buffer,"twenty-one");            break; 57case2:strcpy(buffer,"twenty-two");            break; 58case3:strcpy(buffer,"twenty-three");        break; 59case4:strcpy(buffer,"twenty-four");        break; 60case5:strcpy(buffer,"twenty-five");        break; 61case6:strcpy(buffer,"twenty-six");            break; 62case7:strcpy(buffer,"twenty-seven");        break; 63case8:strcpy(buffer,"twenty-eight");        break; 64case9:strcpy(buffer,"twenty-nine");        break;
65                        }
66                    }break;
67case3:{
68switch(a%10){
69case1:strcpy(buffer,"thirty-one");            break; 70case2:strcpy(buffer,"thirty-two");            break; 71case3:strcpy(buffer,"thirty-three");        break; 72case4:strcpy(buffer,"thirty-four");        break;
73case5:strcpy(buffer,"thirty-five");        break;
74case6:strcpy(buffer,"thirty-six");            break; 75case7:strcpy(buffer,"thirty-seven");        break; 76case8:strcpy(buffer,"thirty-eight");        break; 77case9:strcpy(buffer,"thirty-nine");        break;
78                        }
79                    } break;
80case4:{
81switch(a%10){
82case1:strcpy(buffer,"fourty-one");            break; 83case2:strcpy(buffer,"fourty-two");            break; 84
case3:strcpy(buffer,"fourty-three");        break; 85case4:strcpy(buffer,"fourty-four");        break;
86case5:strcpy(buffer,"fourty-five");        break;
87case6:strcpy(buffer,"fourty-six");            break; 88case7:strcpy(buffer,"fourty-seven");        break; 89case8:strcpy(buffer,"fourty-eight");        break; 90case9:strcpy(buffer,"fourty-nine");        break;
91                        }
92                    }break;
93case5:{
94switch(a%10){
95case1:strcpy(buffer,"fifty-one");            break;
96case2:strcpy(buffer,"fifty-two");            break;
97case3:strcpy(buffer,"fifty-three");        break;
98case4:strcpy(buffer,"fifty-four");              break;
99case5:strcpy(buffer,"fifty-five");            break; 100case6:strcpy(buffer,"fifty-six");            break; 101case7:strcpy(buffer,"fifty-seven");        break; 102case8:strcpy(buffer,"fifty-eight");        break;
103case9:strcpy(buffer,"fifty-nine");            break; 104                        }
105                    }break;
106case6:{
107switch(a%10){
108case1:strcpy(buffer,"sixty-one");            break; 109case2:strcpy(buffer,"sixty-two");            break; 110case3:strcpy(buffer,"sixty-three");        break; 111case4:strcpy(buffer,"sixty-four");              break; 112case5:strcpy(buffer,"sixty-five");            break; 113case6:strcpy(buffer,"sixty-six");            break; 114case7:strcpy(buffer,"sixty-seven");        break; 115case8:strcpy(buffer,"sixty-eight");        break; 116case9:strcpy(buffer,"sixty-nine");            break; 117                        }
118                    }break;
119case7:{
120switch(a%10){
121case1:strcpy(buffer,"seventy-one");        break; 122case2:strcpy(buffer,"seventy-two");        break; 123case3:strcpy(buffer,"seventy-three");        break; 124case4:strcpy(buffer,"seventy-four");        break; 125case5:strcpy(buffer,"seventy-five");        break; 126case6:strcpy(buffer,"seventy-six");        break; 127case7:strcpy(buffer,"seventy-seven");        break; 128case8:strcpy(buffer,"seventy-eight");        break; 129case9:strcpy(buffer,"seventy-nine");        break; 130                        }
131                    }break;
132case8:{
133switch(a%10){
134case1:strcpy(buffer,"eighty-one");            break; 135case2:strcpy(buffer,"eighty-two");            break; 136case3:strcpy(buffer,"eighty-three");        break; 137case4:strcpy(buffer,"eighty-four");        break; 138case5:strcpy(buffer,"eighty-five");        break; 139case6:strcpy(buffer,"eighty-six");            break; 140case7:strcpy(buffer,"eighty-seven");        break; 141case8:strcpy(buffer,"eighty-eight");        break; 142case9:strcpy(buffer,"eighty-nine");        break; 143                        }
144                    }break;
145case9:{
146switch(a%10){
147case1:strcpy(buffer,"ninety-one");            break; 148case2:strcpy(buffer,"ninety-two");            break; 149case3:strcpy(buffer,"ninety-three");        break; 150case4:strcpy(buffer,"ninety-four");        break; 151case5:strcpy(buffer,"ninety-five");        break; 152case6:strcpy(buffer,"ninety-six");            break; 153case7:strcpy(buffer,"ninety-seven");        break; 154case8:strcpy(buffer,"ninety-eight");        break; 155case9:strcpy(buffer,"ninety-nine");        break; 156                        }
157                    }break;
158                }
159            }
160        }break;
161default:printf("ERROR!"); exit(EXIT_FAILURE);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。