essi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:
如22:twenty two,123:one hundred and twenty three。
知识点:递归;数组
全局变量:
string Num[10] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
string ShiJi[10] = { "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "eighteen" };
string JiShi[10] = { "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
程序主体:
void funa(int x){
if (x > 10 && x < 20){ x=x-11; cout << ShiJi[x] << " "; }
else if (x < 10)cout << Num[x]<<" ";
else if (x==10){
cout << JiShi[0] << " ";
}
if (x>=20 && x < 100){
int q = x / 10;
cout << JiShi[q - 1] << " ";
x = x% 10;
if (x == 0)cout << "";
else{ cout << Num[x] << " "; }
}
if (x>=100&&x < 1000){
if (x % 100 == 0){
funa(x / 100); cout << "hundred" << " ";
}
else{
funa(x / 100); cout << "hundred and "; funa(x % 100);
}
}
if (x >= 1000 && x < 1000000){
if (x / 1000 == 0){ funa(x / 1000); cout << "thousand "; }
else{
funa(x / 1000); cout << "thousand "; funa(x % 1000);
}
}
if (x >= 1000000 && x < 1000000000){
if (x / 1000000 == 0){ funa(x / 1000000); cout << "million "; }
else{
funa(x / 1000000); cout << "million "; funa(x % 1000000);
}
}
}
void HWoj(){
int x = 0;
cin >> x;
if (x == 0)cout << "zero";
else
funa(x);
cout << endl;
}