我刚刚开始学习 C++。我正在阅读 Jumping into C++ 。该问题与第 7 章问题 1 相关:
实现将数字转换为文本的源代码。
这是我到目前为止所做的:
#include <iostream>
#include <string>
using namespace std;
int LessThen20 (int i);
int main () {
int i = 1;
cout << "please input a number: \n";
cin >> i;
if (i < 20) {
cout << LessThen20(i);
}
if ( i >= 20 && i < 30) {
cout <<"Twenty " ??
}
}
int LessThen20 (int i) {
if (i == 0) {
cout << "zero" <<endl;
}
if ( i == 1) {
cout << "one"; <<endl;
}
if (i == 2) {
cout << "two"; <<endl;
}
if ( i == 3) {
cout << "three"; <<endl;
}
if (i == 4) {
cout << "four"; <<endl;
}
if ( i == 5) {
cout << "five"; <<endl;
}
if (i == 6) {
cout << "six"; <<endl;
}
if ( i == 7) {
cout << "seven"; <<endl;
}
if (i == 8) {
cout << "eight" <<endl;
}
if ( i == 9) {
cout << "nine"; <<endl;
}
if (i == 10) {
cout << "ten"; <<endl;
}
if ( i == 11) {
cout << "eleven"; <<endl;
}
if (i == 12) {
cout << "twelve"; <<endl;
}
if ( i == 13) {
cout << "thirteen"; <<endl;
}
if (i == 14) {
cout << "fourteen"; <<endl;
}
if ( i == 15) {
cout << "fifteen"; <<endl;
}
if (i == 16) {
cout << "sixteen"; <<endl;
}
if ( i == 17) {
cout << "seventeen"; <<endl;
}
if (i == 18) {
cout << "eighteen"; <<endl;
}
if ( i == 19) {
cout << "nineteen"; <<endl;
}
}
只要输入的数字小于 20,我的程序就可以工作。但我不知道如何将数字 25 变成“二十五”。
任何帮助将不胜感激!