forked from codetrotters/codingchallenge2015
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint_to_word.h
More file actions
145 lines (142 loc) · 2.43 KB
/
int_to_word.h
File metadata and controls
145 lines (142 loc) · 2.43 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
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
#pragma once
#include <string>
using namespace std;
string int_to_word(int n) {
int divisor = 100000000, dd = 0, temp = 0;
bool t, m;
t = false;
m = false;
string word;
if (n == 0)
word = "zero";
else
{
if (n < 0) {
n *= -1;
word += "negative ";
}
for (int i = 1; i <= 3; i++)
{
if ((n / 1000000) > 0 && i == 1)
m = true;
else if ((n / 1000) > 0 && i == 2)
t = true;
for (int j = 1; j <= 3; j++)
{
switch (n / divisor)
{
case 1:
if (j % 2 == 0)
{
dd = divisor / 10;
temp = n / dd;
if ((temp % 10) > 0)
{
switch (temp % 10)
{
case 1:
word += "eleven ";
break;
case 2:
word += "twelve ";
break;
case 3:
word += "thirteen ";
break;
case 4:
word += "fourteen ";
break;
case 5:
word += "fifteen ";
break;
case 6:
word += "sixteen ";
break;
case 7:
word += "seventeen ";
break;
case 8:
word += "eighteen ";
break;
case 9:
word += "nineteen ";
break;
}
}
else {
word += "ten ";
}
j++;
n %= divisor;
divisor /= 10;
}
else
word += "one ";
break;
case 2:
if (j % 2 == 0)
word += "twenty ";
else
word += "two ";
break;
case 3:
if (j % 2 == 0)
word += "thirty ";
else
word += "three ";
break;
case 4:
if (j % 2 == 0)
word += "fourty ";
else
word += "four ";
break;
case 5:
if (j % 2 == 0)
word += "fifty ";
else
word += "five ";
break;
case 6:
if (j % 2 == 0)
word += "sixty ";
else
word += "six ";
break;
case 7:
if (j % 2 == 0)
word += "seventy ";
else
word += "seven ";
break;
case 8:
if (j % 2 == 0)
word += "eighty ";
else
word += "eight ";
break;
case 9:
if (j % 2 == 0)
word += "ninety ";
else
word += "nine ";
break;
}//end switch
if (j == 1 && (n / divisor) != 0)
word += "hundred ";
n %= divisor;
divisor /= 10;
}//end j for
if (m && (i == 1)) {
word += "million ";
m = false;
}
if (t && (i == 2)) {
word += "thousand ";
t = false;
}
}//end i for
}
word.erase(word.end() - 1);
return word;
}