Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2539612
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2011-04-18 17:58:03

    输入一个整数数N,输出它的英文表达。例如:输入1,输出one;输入12,输出twelve;输入135,输出one hundred thirty five;编写程序实现。
    我们知道数字1-19中每一个数字对应一个英文单词;数字20-99的十位也对应一个单词,而百以上使用的上面两种情况的拼凑,而千位的数字,也一样。因此我们可以将1-19存放在一个数组中,20-99的十位存放在另外一个数组中。我们可以先用数字除于1000求得是多少千,除于100求得多少百,然后用求余的方法,进行递归操作,即可。代码如下:
  1. #include <stdio.h>

  2. char data_1[19][10] = {"one","two","three","four","five","six","seven","eight","nine","ten",
  3.          "eleven","twelve","thirteen","fortheen","fifteen","sixteen",
  4.                    "seventeen","eighteen","ninteen"};

  5. char data_2[8][7] = {"twenty","thirthy","forty","fifty","sixty","seventy","eighty","ninty"};


  6. void translation_A(long n);
  7. void translation_B(long n);
  8. void translation_C(long n);
  9. int main(int argc, char *argv[])
  10. {
  11.   long n;
  12.   printf("please input a longeger from 1~999999\n");
  13.   scanf("%ld",&n);
  14.   translation_A(n);
  15.   printf("\n");

  16.   return 0;
  17. }

  18. void translation_A(long n)
  19. {
  20.   long a;
  21.   if(n == 0){
  22.     printf("zero\n");
  23.     return;
  24.   }
  25.   a = n/1000;
  26.   if(a != 0){
  27.     translation_B(a);
  28.     printf("thousand ");
  29.   }
  30.   a = n%1000;
  31.   if(a != 0)
  32.     translation_B(a);
  33. }

  34. void translation_C(long n)
  35. {
  36.   long a;
  37.   if(n <= 19)
  38.     printf("%s ",data_1[n-1]);
  39.   else{
  40.     a = n/10;
  41.     printf("%s ",data_2[a-1]);
  42.     a = n%10;
  43.     if(a != 0)
  44.       printf("%s ",data_1[a-1]);
  45.   }
  46. }

  47. void translation_B(long n)
  48. {
  49.   long a;
  50.   a = n/100;
  51.   if(a != 0){
  52.     translation_C(a);
  53.     printf("hundred ");
  54.   }
  55.   a = n%100;
  56.   if(a != 0)
  57.     translation_C(a);
  58. }
输出结果如下:
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) |
0

上一篇:亲密数

下一篇:递归实现数制转换

给主人留下些什么吧!~~