#include <string>
using namespace std;
int main()
{
int num,
leftDigit,
rightDigit;
string ones[] = { "Zero",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen",
};
string tens[] = { "Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety",
};
cout << "Enter a Number " ;
cin >> num;
if (num <=0 || num >=100 )
{
cout << "the Number is not Between 1-99" << endl;
}
else if (num >= 1 && num <= 19)
{
cout << "The Number you have entered is " << ones[num] << endl;
}
else if (num >= 20 && num <= 99)
{
leftDigit = num / 10;
rightDigit = num % 10;
cout << "the Number you have entered is " << tens[leftDigit - 2] << " " << ones[rightDigit] << endl;
}
system("PAUSE");
return 0;
}
我一直在玩数字到单词,并找到了一个简单的方法来从1-99做任何2位数字。
有什么方法可以扩展我当前的代码以允许3位数字并允许数字1-999以及它有多容易?
答案 0 :(得分:0)
您可以使用递归处理更大的数字。
使用此程序已经适用于2位数的事实,使用num = num / 100
解析数百位数并将余数r = num % 100
传递给您创建的函数。
通过一点点工作,您可以处理任何长度的数字。
答案 1 :(得分:0)
这是SQL中的算法(对不起)。 dbo.NumberWord
表格有两列:Number
和WordEn
。该表格包含数字1到20以及30,40,50,60,70,80和90的英文单词。(一,二,......二十,三十,四十,......,九十)。 / p>
这是一个递归T-SQL函数,用于将1到999,000之间的任何整数转换为英文单词:
CREATE FUNCTION [dbo].[fnNumberToEnglish] (@Number int)
RETURNS nvarchar(1024)
AS
BEGIN
DECLARE
@English nvarchar(1024) =
(SELECT CASE
WHEN @Number = 0 THEN ''
WHEN @Number BETWEEN 1 AND 19 THEN
(SELECT WordEn FROM dbo.NumberWord
WHERE Number = @Number)
WHEN @Number BETWEEN 20 AND 99 THEN
(SELECT WordEn FROM dbo.NumberWord
WHERE (Number / 10) = @Number / 10) + N'-' + dbo.fnNumberToEnglish(@Number%10)
WHEN @Number BETWEEN 100 AND 999 THEN
(dbo.fnNumberToEnglish(@Number / 100)) + N' Hundred ' + dbo.fnNumberToEnglish(@Number%100)
WHEN @Number BETWEEN 1000 AND 999999 THEN
(dbo.fnNumberToEnglish(@Number / 1000)) + N' Thousand ' + dbo.fnNumberToEnglish(@Number%1000)
ELSE
N' INVALID INPUT'
END);
SET @English = RTRIM(@English);
IF RIGHT(@English,1) = '-' BEGIN
SET @English = RTRIM(LEFT(@English, LEN(@English) - 1));
END
RETURN @English;
END
答案 2 :(得分:0)
这应该可以解决问题:
#include <string>
#include <iostream>
using namespace std;
string ones[] = { "",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen",
};
string tens[] = { "Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety",
};
string hundred = "Hundred";
string thousand = "Thousand";
string million = "Million";
string intToWord(int num)
{
if (num >= 1000000 && num < 1000000000)
{
int first = num / 1000000;
return intToWord(first) + " " + million + " " + intToWord(num - first * 1000000);
}
else if (num >= 1000)
{
int first = num / 1000;
return intToWord(first) + " " + thousand + " " + intToWord(num - first * 1000);
}
else if (num >= 100)
{
int first = num / 100;
return ones[first] + " " + hundred + " " + intToWord(num - first * 100);
}
else if (num >= 20)
{
int leftDigit = num / 10;
int rightDigit = num % 10;
return tens[leftDigit - 2] + " " + ones[rightDigit];
}
else if (num >= 0)
{
return ones[num];
}
return "number too large or smaller than 1";
}
int main()
{
int num;
cout << "Enter a Number ";
cin >> num;
cout << endl << intToWord(num) << endl;
system("PAUSE");
return 0;
}
此代码经过测试并正常运行。 如果您愿意,可以进一步扩展它。它使用递归,这使得这非常容易。
此代码一直有效至999999999。