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
|
#include <iostream>
#include <string>
using namespace std;
const string onesAndTeens[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};
const string tens[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
int number_to_text (int num, int divisor, const string & name)
{ int digits = num / divisor;
if (digits == 0)
return num % divisor;
if (digits > 100)
digits = number_to_text (digits, 100, "hundred");
if (digits >= 20)
{ cout << tens[(digits/10)-2] << " ";
digits -= digits/10*10;
}
if (digits)
cout << onesAndTeens[digits] << " ";
cout << name << " ";
return num % divisor;
}
int main()
{ int num;
while (true)
{ cout << "Please enter a number: (Type -0 to exit) " << endl;
cin >> num;
if(num == -0)
break;
if (num < 0)
{ num = num * -1;
cout << "negative ";
}
num = number_to_text (num, 1000000, "million");
num = number_to_text (num, 1000, "thousand");
num = number_to_text (num, 100, "hundred");
num = number_to_text (num, 1, "");
cout << endl;
}
system ("pause");
return 0;
}
|