我在一本书中发现了一个将数字转换为文字的程序。该初始程序可以转换1-1000之间的数字,但是你需要修改程序以接受高达1,000,000的数字。我已经让它可以处理20,999以内的数字,但无法处理更大的数字。我整天都在调试它,并查看了很多在线程序示例,但我看到的每个示例都使用了超出我作为初学者的技术。任何建议都将不胜感激。以下是我目前的代码。
#include <iostream>
#include <string>
using namespace std;
string digitName(int digit);
string teenName(int number);
string tensName(int number);
string intName(int number);
int main()
{
int input;
do
{
cout << "Please enter a positive integer: ";
cin >> input;
cout << "\n" << intName(input) << endl;
cout << "\n\n" << endl;
}while (input > 0);
return 0;
}
string digitName(int digit)
{
if (digit == 1) return "one";
if (digit == 2) return "two";
if (digit == 3) return "three";
if (digit == 4) return "four";
if (digit == 5) return "five";
if (digit == 6) return "six";
if (digit == 7) return "seven";
if (digit == 8) return "eight";
if (digit == 9) return "nine";
return "";
}
string teenName(int number)
{
if (number == 10) return "ten";
if (number == 11) return "eleven";
if (number == 12) return "twelve";
if (number == 13) return "thirteen";
if (number == 14) return "fourteen";
if (number == 15) return "fifteen";
if (number == 16) return "sixteen";
if (number == 17) return "seventeen";
if (number == 18) return "eighteen";
if (number == 19) return "nineteen";
return "";
}
string tensName(int number)
{
if (number >= 90) return "ninety";
if (number >= 80) return "eighty";
if (number >= 70) return "seventy";
if (number >= 60) return "sixty";
if (number >= 50) return "fifty";
if (number >= 40) return "fourty";
if (number >= 30) return "thirty";
if (number >= 20) return "twenty";
return "";
}
string intName(int number)
{
int part = number;
string name;
if (part >= 20000)
{
if (part % 10000 == 0)
{
name = tensName(part / 1000) + " thousand ";
part = part % 1000;
}else
{
name = tensName(part / 1000) + " ";
part = part % 10000;
}
}
if (part >= 10000)
{
name = teenName(part / 1000) + " thousand ";
part = part % 1000;
}
if (part >= 1000)
{
name = digitName(part / 1000) + " thousand ";
part = part % 1000;
}
if (part >= 100)
{
name = name + digitName(part / 100) + " hundred";
part = part % 100;
}
if (part >= 20)
{
name = name + " " + tensName(part);
part = part % 10;
}else if (part >= 10)
{
name = name + " " + teenName(part);
part = 0;
}
if (part > 0)
{
name = name + " " + digitName(part);
}
return name;
}
digitname
开始说“五十万……” - macrolandif
语句。例如,string units [] = {"", "one", "two", "three"..}
,当你想要打印单词“three”时,只需使用units[3]
。 - PaulMcKenzie