#include #include #include using namespace std; const int TENS = 20; char words[30][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; char *Digit (int num) { if (num > 0) return words[num-1]; else return ""; } void GetDigits (int number, int &thousands, int &hundreds, int &tens, int &ones) { thousands = number/1000; number -= (thousands*1000); hundreds = number/100; number -= (hundreds*100); tens = number/10; number -= (tens*10); ones = number; if (tens == 1) // teens are considered part of the one's place { ones = tens*10+ones; tens = 0; } } char *Phrase (int thousands, int hundreds, int tens, int ones) { char *text = new char[100]; *text = '\0'; // initialize the string to empty if (thousands) { strcat (text, words[thousands]); strcat (text, " thousand "); } if (hundreds) { strcat (text, words[hundreds]); strcat (text, " hundred "); } if (tens) { strcat (text, words[tens+TENS]); strcat (text, " "); } if (ones) { strcat (text, words[ones]); } return text; } int main() { char *first, *second, *sum; int number1, number2, add, thousands, hundreds, tens, ones; while (1) { cout << "Enter two numbers (no commas or $): "; cin >> number1 >> number2; GetDigits (number1, thousands, hundreds, tens, ones); first = Phrase(thousands, hundreds, tens, ones); GetDigits (number2, thousands, hundreds, tens, ones); second = Phrase(thousands, hundreds, tens, ones); add = number1 + number2; GetDigits (add, thousands, hundreds, tens, ones); sum = Phrase (thousands, hundreds, tens, ones); cout << first << endl; cout << "PLUS \n"; cout << second << endl; cout << "EQUALS \n"; cout << sum << endl; } }