-
Notifications
You must be signed in to change notification settings - Fork 0
/
0273 - Integer To English Words.cpp
81 lines (65 loc) · 2.04 KB
/
0273 - Integer To English Words.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
class Solution
{
// plain data
private:
std::string onekeys[9] = {
" One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine"
};
std::string tenkeys[9] = {
" Ten", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety"
};
std::string keywords[4] = {
" Thousand", " Million", " Billion"
};
std::string specialkeys[9] = {
" Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen"
};
// helper methods
private:
std::string digitToWord(const int digit)
{
return onekeys[digit - 1];
}
std::string tenDigitToWord(const int digit)
{
return tenkeys[digit - 1];
}
std::string specialNumber(int twodigits)
{
return specialkeys[twodigits - 11];
}
public:
string numberToWords(int num)
{
if (!num) return "Zero";
std::string res;
for (size_t i = 0; i < 4; ++i)
{
int tmp = num % 1000;
// add keyword if number is non-zero
if (tmp && i) res = keywords[i - 1] + res;
// compute first digit
int fd = tmp / 100;
tmp %= 100;
// check for any special two digit number (eg. twelve)
if (tmp > 10 && tmp < 20)
{
res = specialNumber(tmp) + res;
}
else
{
// no special case => compute second and third digit
int sd = tmp / 10;
tmp %= 10;
if (tmp) // tmp is now third digit
res = digitToWord(tmp) + res;
if (sd)
res = tenDigitToWord(sd) + res;
}
if (fd)
res = digitToWord(fd) + " Hundred" + res;
num /= 1000;
}
return res.substr(1, std::string::npos);
}
};