输入一个整数数N,输出它的英文表达。例如:输入1,输出one;输入12,输出twelve;输入135,输出one hundred thirty five;编写程序实现。
我们知道数字1-19中每一个数字对应一个英文单词;数字20-99的十位也对应一个单词,而百以上使用的上面两种情况的拼凑,而千位的数字,也一样。因此我们可以将1-19存放在一个数组中,20-99的十位存放在另外一个数组中。我们可以先用数字除于1000求得是多少千,除于100求得多少百,然后用求余的方法,进行递归操作,即可。代码如下:
- #include <stdio.h>
-
-
char data_1[19][10] = {"one","two","three","four","five","six","seven","eight","nine","ten",
-
"eleven","twelve","thirteen","fortheen","fifteen","sixteen",
-
"seventeen","eighteen","ninteen"};
-
-
char data_2[8][7] = {"twenty","thirthy","forty","fifty","sixty","seventy","eighty","ninty"};
-
-
-
void translation_A(long n);
-
void translation_B(long n);
-
void translation_C(long n);
-
int main(int argc, char *argv[])
-
{
-
long n;
-
printf("please input a longeger from 1~999999\n");
-
scanf("%ld",&n);
-
translation_A(n);
-
printf("\n");
-
-
return 0;
-
}
-
-
void translation_A(long n)
-
{
-
long a;
-
if(n == 0){
-
printf("zero\n");
-
return;
-
}
-
a = n/1000;
-
if(a != 0){
-
translation_B(a);
-
printf("thousand ");
-
}
-
a = n%1000;
-
if(a != 0)
-
translation_B(a);
-
}
-
-
void translation_C(long n)
-
{
-
long a;
-
if(n <= 19)
-
printf("%s ",data_1[n-1]);
-
else{
-
a = n/10;
-
printf("%s ",data_2[a-1]);
-
a = n%10;
-
if(a != 0)
-
printf("%s ",data_1[a-1]);
-
}
-
}
-
-
void translation_B(long n)
-
{
-
long a;
-
a = n/100;
-
if(a != 0){
-
translation_C(a);
-
printf("hundred ");
-
}
-
a = n%100;
-
if(a != 0)
-
translation_C(a);
-
}
输出结果如下:
peng@ubuntu:~/src/test/c/suanfa/miaoqu$ ./a.out
please input a longeger from 1~999999
1234
one thousand two hundred forty four
peng@ubuntu:~/src/test/c/suanfa/miaoqu$ ./a.out
please input a longeger from 1~999999
1
one
阅读(1838) | 评论(0) | 转发(0) |