-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq16.8.cpp
96 lines (75 loc) · 2.32 KB
/
q16.8.cpp
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
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef unsigned long MYINT;
map<int, string> singleDigits{
{0, "zero"}, {1, "one"}, {2, "two"}, {3, "three"}, {4, "four"},
{5, "five"}, {6, "six"}, {7, "seven"}, {8, "eight"}, {9, "nine"}};
map<int, string> twoDigitsSpecial{
{11, "eleven"}, {12, "twelve"}, {13, "thirteen"},
{14, "fourteen"}, {15, "fifteen"}, {16, "sixteen"},
{17, "seventeen"}, {18, "eighteen"}, {19, "nineteen"}};
map<int, string> multiplesTen{{1, "ten"}, {2, "twenty"}, {3, "thirty"},
{4, "forty"}, {5, "fifty"}, {6, "sixty"},
{7, "seventy"}, {8, "eighty"}, {9, "ninety"}};
map<int, string> prependMap{
{1, ""}, {2, "thousand"}, {3, "million"}, {4, "billion"}, {5, "trillion"}};
string genStr2Dig(int number) {
if (number == 0)
return ""; // 00
if (number / 10 == 0) { // 0x
return singleDigits[number];
}
// xy
// y=0
if (number % 10 == 0)
return multiplesTen[number / 10];
else if (number / 10 == 1) { // 1y
return twoDigitsSpecial[number];
}
else { // xy
return multiplesTen[number / 10] + ' ' + singleDigits[number % 10];
}
return "";
}
string genStr3Dig(int number, string appendString) {
if (number == 0) // 000
return "";
if (number / 100 == 0) { // 0xy
return genStr2Dig(number) + ' ' + appendString;
}
else { // xyz
return singleDigits[number / 100] + " hundred " + genStr2Dig(number % 100) +
' ' + appendString;
}
}
string genString(MYINT number) {
string print_str = "";
if (number / 10 == 0) {
return singleDigits[number];
}
int prependCounter = 1;
// int rem;
int divider = 1000;
while (number != 0) {
print_str = genStr3Dig(number % divider, prependMap[prependCounter]) +
", " + print_str;
prependCounter++;
number = number / divider;
}
return print_str;
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
MYINT number;
string print_str;
while (true) {
cout << "enter positive number or a zero to quit: ";
cin >> number;
if (number == 0)
break;
print_str = genString(number);
cout << print_str << '\n';
}
return 0;
}