-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
IntegerToEnglishWords.cpp
131 lines (116 loc) · 3.43 KB
/
IntegerToEnglishWords.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Source : https://leetcode.com/problems/integer-to-english-words/
// Author : Hao Chen
// Date : 2015-10-22
/***************************************************************************************
*
* Convert a non-negative integer to its english words representation. Given input is
* guaranteed to be less than 231 - 1.
*
* For example,
*
* 123 -> "One Hundred Twenty Three"
* 12345 -> "Twelve Thousand Three Hundred Forty Five"
* 1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
*
* Did you see a pattern in dividing the number into chunk of words? For example, 123
* and 123000.
*
* Group the number by thousands (3 digits). You can write a helper function that
* takes a number less than 1000 and convert just that chunk to words.
*
* There are many edge cases. What are some good test cases? Does your code work with
* input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)
*
***************************************************************************************/
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
static string dict1[] ={"Zero","One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"};
static string dict2[]={"","", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
static string dict3[]={"Hundred", "Thousand", "Million", "Billion" };
// This function only converts the number which less than 1000
string numberLess1000ToWords(int num) {
//char n[3] = {0, 0, 0};
string result;
if (num == 0) {
return result;
}else if (num < 20) {
return dict1[num];
} else if (num < 100) {
result = dict2[num/10];
if (num%10 > 0) {
result += " " + dict1[num%10];
}
}else {
result = dict1[num/100] + " " + dict3[0];
if ( num % 100 > 0 ) {
result += " " + numberLess1000ToWords( num % 100 );
}
}
return result;
}
string numberToWords(int num) {
//edge case
if (num ==0 ) return dict1[num];
vector<string> ret;
for( ;num > 0; num/=1000 ) {
ret.push_back( numberLess1000ToWords(num % 1000) );
}
string result=ret[0];
for (int i=1; i<ret.size(); i++){
if (ret[i].size() > 0 ){
if ( result.size() > 0 ) {
result = ret[i] + " " + dict3[i] + " " + result;
} else {
result = ret[i] + " " + dict3[i];
}
}
}
return result;
}
#define TEST(num) cout << num << " -> \"" << numberToWords(num) << "\"" << endl
int main(int argc, char** argv)
{
int num = 123;
if (argc >1){
num = atoi(argv[1]);
}
TEST(num);
TEST(0);
TEST(1);
TEST(10);
TEST(11);
TEST(18);
TEST(20);
TEST(22);
TEST(30);
TEST(99);
TEST(100);
TEST(101);
TEST(110);
TEST(120);
TEST(256);
TEST(1000);
TEST(1001);
TEST(1010);
TEST(1110);
TEST(1111);
TEST(10000);
TEST(10001);
TEST(100000);
TEST(100001);
TEST(1000000);
TEST(1000001);
TEST(10000000);
TEST(10000001);
TEST(100000000);
TEST(100000001);
TEST(1000000000);
TEST(1000000001);
TEST(2147483647);
return 0;
}