-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpeakANo.cpp
More file actions
103 lines (86 loc) · 2.34 KB
/
Copy pathSpeakANo.cpp
File metadata and controls
103 lines (86 loc) · 2.34 KB
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
/*
Spell out an integer number in English.
For example:
Input: 1000234
Output: One Thousand, Two Hundred and Thirty Four
Note: support negative integer.
*/
/*
solution: process the number by three digits one time, add proper big unit between those digits.
For each three digits, consider different situations.
*/
#include <iostream>
#include <string>
using namespace std;
string lastdigit[10] = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
string allteen[10] = { "", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
string allten[10] = { "", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
string more[10] = { "","","","","Thousand ","Thousands " ,"Lac ", "Lacs ", "Crore ", "Crores "};
string UnderHundred(int num)
{
string output = "";
if (num >= 100)
{
output += lastdigit[num/100] + " Hundred ";
num %= 100;
}
if (num >= 11 && num <= 19)
{
output += allteen[num-10] + " ";
}
else if (num >= 20 || num == 10)
{
output += allten[num/10] + " ";
num %= 10;
}
if (num >= 1 && num <= 9)
{
output += lastdigit[num] + " ";
}
return output;
}
//Do not try to start from left side
//process less than 1000 element first and then remain process in pair of two.
string TranslateNumber(long long num)
{
string output = UnderHundred(num%1000);
num = num/1000;
int index = 4;
while (num > 0)
{
int d1 = num%10;
num = num/10;
int d2 = num%10;
num = num/10;
int d = d2*10 + d1;
if(d >= 11 && d <= 19)
{
index++;
output = allteen[d%10] + " " + more[index] + " " + output;
index++;
}
else if(d == 0)
{
index = index+2;
}
else if(d >= 1 && d <= 9)
{
output = lastdigit[d%10] + " " + more[index] + " " + output;
index = index + 2;
}
else
{
index++;
output = allten[d2] + " " + lastdigit[d1] + " " + more[index] + " " + output;
index++;
}
}
return output;
}
int main() {
cout<<"Input the number: ";
long long num;
cin>>num;
cout<<TranslateNumber(num)<<endl;
return 0;
}