1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
//To do:
//translate decimal numbers (http://www.basic-mathematics.com/writing-decimals-in-words.html)
//use the word "and" to indicate decimal location
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string ones[10] =
{
"zero ", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ", "nine "
};
string teens[10] =
{
"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ",
"fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "
};
string tens[8] =
{
"twenty", "thirty", "fourty", "fifty", "sixty",
"seventy", "eighty", "ninety"
};
string magnitude[10] =
{
"hundred ", "thousand ", "million ", "billion ", "trillion ",
"quadrillion ","quintillion ","sextillion ", "septillion ", "octillion "
};
//This function takes a given number and reverses it
//in order to retrieve the appropriate words(including "hundred")
//for each power of 1000.
int threeDigitChunk
(
int number, string ones[], string teens[],
string tens[], int reverseNumber, string magnitude[]
)
{
if ( number < 1000 )
{
int reverseNumber[3];
for ( int i = 0; i < 3; i++ )
{
reverseNumber[i] = (number % 10);
number = (number / 10);
}
if ( reverseNumber[2] != 0 )
{
cout << ones[reverseNumber[2]];
cout << magnitude[0];
}
//Check if hyphen is needed
if ( reverseNumber[1] > 1 && reverseNumber[0] == 0 )
{
cout << tens[reverseNumber[1] - 2] << ' ';
}
else if ( reverseNumber[1] > 1 && reverseNumber[0] != 0 )
{
cout << tens[reverseNumber[1] - 2] << '-';
}
if ( reverseNumber[1] < 2 && reverseNumber[1] != 0 )
{
cout << teens[reverseNumber[0]];
}
else if ( reverseNumber[0] != 0 )
{
cout << ones[reverseNumber[0]];
}
else if ( reverseNumber[0] == 0 && reverseNumber[1] == 0 && reverseNumber[2] == 0 )
{
cout << ones[reverseNumber[0]];
}
}
else
{
cout << "Derp";
}
}
int main()
{
cout << "This program will translate a number entered in digits to the equivalent" << endl;
cout << "in English. Commas may be used." << endl << endl;
while ( true )
{
string commaCheck;
unsigned long long number;
unsigned long long reverseNumber;
cout << "Enter number: ";
cin >> commaCheck;
commaCheck.erase( remove( commaCheck.begin(), commaCheck.end(), ',' ), commaCheck.end() );
number = atoi( commaCheck.c_str() );
cout << endl;
//Find the magnitude and corresponding index of the input number.
unsigned long long power_value = 1000;
int power_index = 1;
while ((number / power_value) != 0)
{
power_value *= 1000;
power_index += 1;
}
power_value /= 1000;
power_index -= 1;
//Isolate each power of 1000 and retrieve magnitude if appropriate.
int tempNumber = (number / power_value);
threeDigitChunk( tempNumber, ones, teens, tens, reverseNumber, magnitude );
if ( power_index != 0 )
{
cout << magnitude[power_index] << endl;
for ( int i = 1; i <= power_index; i++ )
{
tempNumber = (number % power_value) / ( power_value / 1000 );
if ( tempNumber != 0 )
{
threeDigitChunk( tempNumber, ones, teens, tens, reverseNumber, magnitude );
}
if ( power_index > i && tempNumber != 0 )
{
cout << magnitude[power_index - i] << endl;
}
power_value /= 1000;
}
number %= power_value;
if ( power_index > 1 && number != 0 )
{
threeDigitChunk( number, ones, teens, tens, reverseNumber, magnitude );
}
}
cout << endl << endl;
}
}
|