I have a large piece of code :
string num_to_text[] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string tens_to_text[] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
string power_to_text[] = { "", "thousand", "million", "billion" };
string padIfNeeded (string ans)
{
if ( ans == "" )
{
return "";
}
return " " + ans;
}
string translateHundred (int hundred_chunk)
{
// handle special cases in the teens
if ( hundred_chunk < 20 ) {
return num_to_text[ hundred_chunk ];
}
int tens = hundred_chunk / 10;
int ones = hundred_chunk % 10;
return tens_to_text[ tens ] + padIfNeeded( num_to_text[ ones ] );
}
string translateThousand (int thousand_chunk)
{
if ( thousand_chunk < 100 )
{
return translateHundred( thousand_chunk );
}
else
{
int hundreds = thousand_chunk / 100;
int hundred_chunk = thousand_chunk % 100;
return num_to_text[ hundreds ] + " hundred" + padIfNeeded( translateHundred( hundred_chunk ) );
}
}
int main()
{
int n;
cin >> n;
string number;
bool is_negative = false;
if ( n < 0 )
{
is_negative = true;
n *= -1;
}
int chunk_count = 0;
while ( n > 0 )
{
if ( n % 1000 != 0 ) {
number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
}
n /= 1000;
chunk_count++;
}
if ( number == "" )
{
number = "zero";
}
if ( is_negative )
{
number = "negative " + number;
}
cout << number << endl;
the code is very simple and easy but there is only something in the following structure that confuses me a lot :
while ( n > 0 )
{
if ( n % 1000 != 0 ) {
number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
}
n /= 1000;
chunk_count++;
the code give the name of a number in words.
the first part is divided into three parts
1) it ensures white spaces between words when the number is given in words
2) it gives numbers from 1 to 99
3) it gives numbers from 1 to 999
the second is where the program begins and where the first part is called by the main function
what I could not understand is:
number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
the firt "translateThousand
( n % 1000 )
" gives any number between 1 and 999
power_to_text[ chunk_count ] gives (billion or million or thousand) but the problem is with (number) because how comes that :
**number= something + number when considering that the (+) is for concatenation between strings and not for arthmetic operation?????!!!!! **
in other words why it is written (number) at the end of :
number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );?????
2)what is the role of
(n/=1000)
in this context????
3) and consequently how the whole piece of code works???? I mean:
while ( n > 0 )
{
if ( n % 1000 != 0 ) {
number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
}
n /= 1000;
chunk_count++;