-
Notifications
You must be signed in to change notification settings - Fork 0
/
printNum.cpp
188 lines (158 loc) · 3.46 KB
/
printNum.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <string>
// Explore:
// - pragma once
// - internal linkage and external linkage
// - every .cpp file (called `source` file) creates an object file (.o file)
// - objects contain symbols definitions
// - when linker encounters an undefined symbol in one object file (say a.o), it searches the definition in
// other objects file AND shared library AND static library.. and wherever it finds it, it links!
// - all the symbols in an object file do NOT have same visibility.
// - some symbols are visible outside the object file (these are said to have `external` linkage)
// - other are not; they're internal to the object file (these are said to have `internal` linkage)
// - explore also `nm` command line
//
static std::string const Ones[] = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
static std::string const Tens[] = {"Eleven","Twelve","Thirteen","Forteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
static std::string const specialTens[] = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
static void print99999(int);
static void primitiveClassify(int);
static void print99(int);
static void print999(int);
static void printLakh(int);
static void printCrore(int);
static void printArab(int);
void printWords(int number) {
printArab(number);
}
void primitiveClassify(int number)
{
assert(number < 20);
if (number<10)
{
std::cout << Ones[number];
}
else if (number>10)
{
number-=11;
std::cout << Tens[number];
}
else
{
std::cout<<" Ten ";
}
}
void print99(int number)
{
assert(number<100);
if(number < 20)
{
primitiveClassify(number);
}
else
{
int tens = number / 10;
int ones = number % 10;
std::cout<<specialTens[tens-2];
if (number!=0)
{
std::cout <<Ones[ones];
}
}
}
void print999(int number)
{
assert(number<1000);
if (number < 20)
{
primitiveClassify(number);
}
else if(number > 19 and number <100)
{
print99(number);
}
else
{
int hundreds= number/100;
int rest= number%100;
std::cout<< Ones[hundreds];
std::cout<< " Hundred ";
if (rest)
{
printWords(rest);
}
}
}
void print99999(int number)
{
assert(number <100000 );
if (number<1000)
{
print999(number);
}
else
{
int thousands = number / 1000;
int rest = number %1000;
printWords(thousands);
std::cout<<" Thousand ";
if(rest)
{
printWords(rest);
}
}
}
void printLakh(int number)
{
if (number<100000)
{
print99999(number);
}
else
{
int lakhs = number/100000;
int rest = number%100000;
printWords(lakhs);
std::cout << " Lakh " ;
if (rest)
{
printWords(rest);
}
}
}
void printCrore(int number)
{
if (number<10000000)
{
printLakh(number);
}
else
{
int crores = number/10000000;
int rest = number%10000000;
printWords(crores);
std::cout<<" Crore ";
if (rest)
{
printWords(rest);
}
}
}
void printArab(int number)
{
if(number<1000000000)
{
printCrore(number);
}
else
{
int arabs = number / 1000000000;
int rest = number % 1000000000;
printWords(arabs);
std::cout << " Arabs ";
if(rest)
{
printWords(rest);
}
}
}