数字翻译器及其实现

【问题描述】
输入一个正整数N(N最大是4位数),输出它的英文表达。
【样例】
输入:1
输出:one
又输入:12
输出:twelve
右输入:135
输出:one hundred thirty five

思路:1、首先19以内的数字,可以直接输出。。

2、20~~~99以内的数字,整十的整数可以直接输出。否则,除以10输出十位数字,与10取模输出个位数字。

3、100~~~999以内的数字,除以100输出百位数字,与100取模得到一个两位的数字,转到2

4、1000~~~9999以内的数字,除以1000输出千位数字,与1000取模得到一个三位的数字,转到3

完整的实现代码如下:

#include "iostream" using namespace std; char table[20][20]= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen" }; char tens[12][20]={"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninty","hundred","thousand" }; void print(int n) { if(n >= 0 && n <= 19) { cout<<table[n]; //直接输出19以内的数字 } else if(n >= 20 && n <= 99 && n%10 == 0) //整十 { cout<<tens[n/10]; } else if(n >= 20 && n <= 99) //先输出十位,再输出个位 { cout<<tens[n/10]<<" "<<table[n%10]; } else if(n >= 100 && n<= 999) { print(n/100); cout<<" "<<tens[10]<<" "; //输出百位 print(n%100); //递归调用,输出十位和个位 } else if(n >= 1000 && n <= 999999) { print(n/1000); cout<<" "<<tens[11]<<" "; //输出千位 print(n%1000); //递归调用,输出百位、十位和个位 } } int main(void) { int n; while(cin>>n) { print(n); cout<<endl; } system("pause"); return 0; }

运行效果图如下:


 

转载于:https://www.cnblogs.com/newthing/archive/2011/08/22/2157463.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值