-
Notifications
You must be signed in to change notification settings - Fork 160
/
Namenumbers.cpp
77 lines (59 loc) · 1.77 KB
/
Namenumbers.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
/*
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[6] = { "", "Thousand", "Million", "Billion", "Trillion", "Quadrillion" };
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;
}
string TranslateNumber(long long num) {
if (num == 0) {
return "Zero";
} else if ( num < 0) {
return "Negative " + TranslateNumber(-1 * num);
}
int index = 0;
string output = "";
while (num > 0) {
if (num % 1000 != 0) {
output = UnderHundred(num % 1000) + more[index] + " " + output;
}
num /= 1000;
++index;
}
return output;
}
int main() {
cout<<"Input the number: ";
long long num;
cin>>num;
cout<<TranslateNumber(num)<<endl;
return 0;
}