01: #include <iostream>
02: #include <string>
03: using namespace std;
04: /**
05: Turn a digit into its English name
06: @param n an integer between 1 and 9
07: @return the name of n ("one" . . . "nine")
08: */
09: string digit_name(int n)
10: { if (n == 1) return "one";
11: if (n == 2) return "two";
12: if (n == 3) return "three";
13: if (n == 4) return "four";
14: if (n == 5) return "five";
15: if (n == 6) return "six";
16: if (n == 7) return "seven";
17: if (n == 8) return "eight";
18: if (n == 9) return "nine";
19: return "";
20: }
21: /**
22: Turn a number between 10 and 19 into its English name
23: @param n an integer between 10 and 19
24: @return the name of n ("ten" . . . "nineteen")
25: */
26: string teen_name(int n)
27: { if (n == 10) return "ten";
28: if (n == 11) return "eleven";
29: if (n == 12) return "twelve";
30: if (n == 13) return "thirteen";
31: if (n == 14) return "fourteen";
32: if (n == 15) return "fifteen";
33: if (n == 16) return "sixteen";
34: if (n == 17) return "seventeen";
35: if (n == 18) return "eighteen";
36: if (n == 19) return "nineteen";
37: return "";
38: }
39: /**
40: Give the English name of a multiple of 10
41: @param n an integer between 2 and 9
42: @return the name of 10 * n ("twenty" . . . "ninety")
43: */
44: string tens_name(int n)
45: { if (n == 2) return "twenty";
46: if (n == 3) return "thirty";
47: if (n == 4) return "forty";
48: if (n == 5) return "fifty";
49: if (n == 6) return "sixty";
50: if (n == 7) return "seventy";
51: if (n == 8) return "eighty";
52: if (n == 9) return "ninety";
53: return "";
54: }
55: /**
56: Turn a number into its English name
57: @param n a positive integer < 1,000,000
58: @return the name of n (e.g. "two hundred seventy four")
59: */
60: string int_name(int n)
61: { int c = n; /* the part that still needs to be converted */
62: string r; /* the return value */
63:
64: if (c >= 1000)
65: { r = int_name(c / 1000) + " thousand";
66: c = c % 1000;
67: }
68: if (c >= 100)
69: { r = r + " " + digit_name(c / 100) + " hundred";
70: c = c % 100;
71: }
72: if (c >= 20)
73: { r = r + " " + tens_name(c / 10);
74: c = c % 10;
75: }
76: if (c >= 10)
77: { r = r + " " + teen_name(c);
78: c = 0;
79: }
80: if (c > 0)
81: r = r + " " + digit_name(c);
82:
83: return r;
84: }
85:
86: int main()
87: { int n;
88: cout << "Please enter a positive integer: ";
89: cin >> n;
90: cout << int_name(n);
91: return 0;
92: }