-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
232 lines (191 loc) · 5.74 KB
/
Copy pathmain.cpp
File metadata and controls
232 lines (191 loc) · 5.74 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//To do:
//translate decimal numbers (http://www.basic-mathematics.com/writing-decimals-in-words.html)
//use the word "and" to indicate decimal location
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string decimalMagnitude[10] =
{
"", "tenth", "hundredth", "thousandth", "ten thousandth",
"hundred thousandth", "millionth", "ten millionth",
"hundred millionth"
};
string ones[10] =
{
"zero ", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ", "nine "
};
string teens[10] =
{
"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ",
"fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "
};
string tens[10] =
{
"", "", "twenty", "thirty", "fourty",
"fifty", "sixty", "seventy", "eighty", "ninety"
};
string magnitude[10] =
{
"", "thousand ", "million ", "billion ", "trillion ",
"quadrillion ","quintillion ","sextillion ", "septillion ", "octillion "
};
//This function takes a given number and reverses it
//in order to retrieve the appropriate words(including "hundred")
//for each power of 1000.
int threeDigitChunk( int number );
//Find power of 1000
long long powerValue ( long long number );
//Find corresponding index
int powerIndex ( long long number, long long power_value);
//Isolate powers of 1000 and print them
void printNumber ( long long number, long long power_value, int power_index );
int main()
{
cout << "This program will translate a number entered in digits to the equivalent" << endl;
cout << "in English.\n\n";
cout << "\tCommas may be used.\n\n";
cout << "\tDecimal number capable.\n\n";
cout << "\tNegative number capable.\n\n";
while ( true )
{
string puncCheck;
int decimalLength;
long long number;
long long decimalNumber;
cout << "Enter number: ";
cin >> puncCheck;
//Remove commas
puncCheck.erase( remove( puncCheck.begin(), puncCheck.end(), ',' ), puncCheck.end() );
//Find position of decimal and remove it.
int decimalPosition = puncCheck.find( '.', 0 );
puncCheck.erase( remove( puncCheck.begin(), puncCheck.end(), '.' ), puncCheck.end() );
if ( decimalPosition == -1 )
{
number = atoi( puncCheck.c_str() );
}
else
{
number = atoi( puncCheck.substr( 0, decimalPosition ).c_str() );
decimalLength = puncCheck.substr( decimalPosition, '\n' ).length();
decimalNumber = atoi( puncCheck.substr( decimalPosition, '\n' ).c_str() );
}
//Check zero
if ( puncCheck == "0" )
{
cout << "zero\n";
continue;
}
//Validate number
if (!number)
{
cin.clear();
cin.ignore( 10000, '\n' );
cout << "Invalid number. Try again.\n";
continue;
}
cout << endl;
//Check negative (turn negative number into positive)
if ( number < 0 )
{
cout << "negative ";
number = -number;
}
//Find the magnitude and corresponding index of the input number.
long long power_value = powerValue ( number );
int power_index = powerIndex ( number, power_value);
//Isolate each power of 1000 and retrieve magnitude if appropriate.
printNumber ( number, power_value, power_index );
if ( decimalPosition != -1 )
{
cout << "\nand\n";
//Find the magnitude and corresponding index of the input decimal number.
power_value = powerValue ( decimalNumber );
power_index = powerIndex ( decimalNumber, power_value);
//Isolate each power of 1000 and retrieve magnitude if appropriate.
printNumber ( decimalNumber, power_value, power_index );
cout << decimalMagnitude[decimalLength];
if ( decimalNumber != 1 )
{
cout << "s";
}
}
cout << endl << endl;
}
}
int threeDigitChunk( int number )
{
int digits[3];
for ( int i = 0; i < 3; i++ )
{
digits[i] = (number % 10);
number = (number / 10);
}
if ( digits[2] != 0 )
{
cout << ones[digits[2]] << "hundred ";
}
//Check if hyphen is needed
if ( digits[1] == 1 )
{
cout << teens[digits[0]];
}
else
{
if ( digits[1] != 0 && digits[0] != 0)
{
cout << tens[digits[1]] << '-';
}
else if ( digits[1] != 0 )
{
cout << tens[digits[1]] << ' ';
}
if ( digits[0] != 0 )
{
cout << ones[digits[0]];
}
}
}
long long powerValue ( long long number )
{
long long power_value = 1000;
while ((number / power_value) != 0)
{
power_value *= 1000;
}
power_value /= 1000;
return power_value;
}
int powerIndex ( long long number, long long power_value)
{
power_value = 1000;
int power_index = 1;
while ((number / power_value) != 0)
{
power_value *= 1000;
power_index += 1;
}
power_value /= 1000;
power_index -= 1;
return power_index;
}
void printNumber ( long long number, long long power_value, int power_index )
{
do
{
int tempNumber = (number / power_value);
if ( tempNumber != 0 )
{
threeDigitChunk( tempNumber );
if ( (power_index > 0) && (tempNumber != 0) )
{
cout << magnitude[power_index];
cout << endl;
}
}
power_value /= 1000;
power_index -= 1;
}
while ( power_index >= 0 );
}