-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
259 lines (241 loc) · 8.56 KB
/
Copy pathmain.cpp
File metadata and controls
259 lines (241 loc) · 8.56 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//I took the full four hours to complete this code. I was planning on adding code to make sure a number between 1 and 3,999 was input into both parts of the code.
//Not having that condition allows the user to input an incorrect number and roman numeral which causes the wrong output.
//I would have also tried to fix up the functions in order to shorten the overall code.
#include <iostream>
#include <string>
using namespace std;
//declare variables needed for each function and main
string decNum;
int i;
//function to perform conversion for the ones place of decimal number
void OnesPlace () {
for (i = 0; i < decNum.size(); i++) {
if (decNum.at(decNum.size() - 1) == '1') {
decNum.replace(decNum.size() - 1,1, "I");
}
if (decNum.at(decNum.size() - 1) == '2') {
decNum.replace(decNum.size() - 1,1, "II");
}
if (decNum.at(decNum.size() - 1) == '3') {
decNum.replace(decNum.size() - 1,1, "III");
}
if (decNum.at(decNum.size() - 1) == '4') {
decNum.replace(decNum.size() - 1,1, "IV");
}
if (decNum.at(decNum.size() - 1) == '5') {
decNum.replace(decNum.size() - 1,1, "V");
}
if (decNum.at(decNum.size() - 1) == '6') {
decNum.replace(decNum.size() - 1,1, "VI");
}
if (decNum.at(decNum.size() - 1) == '7') {
decNum.replace(decNum.size() - 1,1, "VII");
}
if (decNum.at(decNum.size() - 1) == '8') {
decNum.replace(decNum.size() - 1,1, "VIII");
}
if (decNum.at(decNum.size() - 1) == '9') {
decNum.replace(decNum.size() - 1,1, "IX");
}
}
}
//function to perform conversion for the tens place decimal number
void TensPlace() {
for (i = 0; i < decNum.size(); i++) {
if (decNum.at(decNum.size() - 2) == '1') {
decNum.replace(decNum.size() - 2, 1, "X");
}
if (decNum.at(decNum.size() - 2) == '2') {
decNum.replace(decNum.size() - 2, 1, "XX");
}
if (decNum.at(decNum.size() - 2) == '3') {
decNum.replace(decNum.size() - 2, 1, "XXX");
}
if (decNum.at(decNum.size() - 2) == '4') {
decNum.replace(decNum.size() - 2, 1, "XL");
}
if (decNum.at(decNum.size() - 2) == '5') {
decNum.replace(decNum.size() - 2, 1, "L");
}
if (decNum.at(decNum.size() - 2) == '6') {
decNum.replace(decNum.size() - 2, 1, "LX");
}
if (decNum.at(decNum.size() - 2) == '7') {
decNum.replace(decNum.size() - 2, 1, "LXX");
}
if (decNum.at(decNum.size() - 2) == '8') {
decNum.replace(decNum.size() - 2, 1, "LXXX");
}
if (decNum.at(decNum.size() - 2) == '9') {
decNum.replace(decNum.size() - 2, 1, "XC");
}
if (decNum.at(decNum.size() - 1) == '0') {
decNum.erase(decNum.size() - 1, 1);
}
if (decNum.at(decNum.size() - 1) > 0) {
OnesPlace();
}
}
}
//function to perform conversion for the hundreds place of decimal number
void HundredsPlace() {
for (i = 0; i < decNum.size(); i++) {
if (decNum.at(decNum.size() - 3) == '1') {
decNum.replace(decNum.size() - 3, 1, "C");
}
if (decNum.at(decNum.size() - 3) == '2') {
decNum.replace(decNum.size() - 3, 1, "CC");
}
if (decNum.at(decNum.size() - 3) == '3') {
decNum.replace(decNum.size() - 3, 1, "CCC");
}
if (decNum.at(decNum.size() - 3) == '4') {
decNum.replace(decNum.size() - 3, 1, "CD");
}
if (decNum.at(decNum.size() - 3) == '5') {
decNum.replace(decNum.size() - 3, 1, "D");
}
if (decNum.at(decNum.size() - 3) == '6') {
decNum.replace(decNum.size() - 3, 1, "DC");
}
if (decNum.at(decNum.size() - 3) == '7') {
decNum.replace(decNum.size() - 3, 1, "DCC");
}
if (decNum.at(decNum.size() - 3) == '8') {
decNum.replace(decNum.size() - 3, 1, "DCCC");
}
if (decNum.at(decNum.size() - 3) == '9') {
decNum.replace(decNum.size() - 3, 1, "CM");
}
if (decNum.at(decNum.size() - 2) == '0') {
decNum.erase(decNum.size() - 2, 1);
}
if (decNum.at(decNum.size() - 2) > 0) {
TensPlace();
}
}
}
//function to perform conversion for the thousands place of decimal number
void ThousandsPlace() {
for (i = 0; i < decNum.size(); i++) {
if (decNum.at(decNum.size() - 4) == '1') {
decNum.replace(decNum.size() - 4, 1, "M");
}
if (decNum.at(decNum.size() - 4) == '2') {
decNum.replace(decNum.size() - 4, 1, "MM");
}
if (decNum.at(decNum.size() - 4) == '3') {
decNum.replace(decNum.size() - 4, 1, "MMM");
}
if (decNum.at(decNum.size() - 3) == '0') {
decNum.erase(decNum.size() - 3, 1);
}
if (decNum.at(decNum.size() - 2) > 0) {
HundredsPlace();
}
}
}
int main() {
//declare variables
string romNum;
string strIV = "IV";
string strIX = "IX";
string strXL = "XL";
string strXC = "XC";
string strCD = "CD";
string strCM = "CM";
int numeral = 0;
int I = 1;
int V = 5;
int X = 10;
int L = 50;
int C = 100;
int D = 500;
int M = 1000;
//ask for and receive roman numeral from user
cout << "What is the Roman numeral?";
cin >> romNum;
cout << endl;
//make user roman numeral uppercase in case they put lowercase letters to allow code to still work
for (i = 0; i < romNum.size(); i++) {
romNum.at(i) = toupper(romNum.at(i));
}
//find potential subtractive pairs and remove from string so individual numerals are not repeated in the total
//add the value of the pairs to the total if found
for (i = 0; i < romNum.size(); i++) {
if (romNum.find(strIV) != string::npos) {
romNum.erase(romNum.find(strIV), strIV.length());
numeral = numeral + (V - I);
}
if (romNum.find(strIX) != string::npos) {
romNum.erase(romNum.find(strIX), strIX.length());
numeral = numeral + (X - I);
}
if (romNum.find(strXL) != string::npos) {
romNum.erase(romNum.find(strXL), strXL.length());
numeral = numeral + (X - L);
}
if (romNum.find(strXC) != string::npos) {
romNum.erase(romNum.find(strXC), strXC.length());
numeral = numeral + (C - X);
}
if (romNum.find(strCD) != string::npos) {
romNum.erase(romNum.find(strCD), strCD.length());
numeral = numeral + (D - C);
}
if (romNum.find(strCM) != string::npos) {
romNum.erase(romNum.find(strCM), strCM.length());
numeral = numeral + (M - C);
}
}
//add value of individual numerals to the total
for (i = 0; i < romNum.size(); i++) {
if (romNum.at(i) == 'I') {
numeral = numeral + I;
}
if (romNum.at(i) == 'V') {
numeral = numeral + V;
}
if (romNum.at(i) == 'X') {
numeral = numeral + X;
}
if (romNum.at(i) == 'L') {
numeral = numeral + L;
}
if (romNum.at(i) == 'C') {
numeral = numeral + C;
}
if (romNum.at(i) == 'D') {
numeral = numeral + D;
}
if (romNum.at(i) == 'M') {
numeral = numeral + M;
}
}
//Print the decimal form total of the roman numeral
cout << "The Roman numeral as a decimal number is " << numeral << "." << endl << endl;
//check the conversion by asking user for decimal number
cout << "Let's check the conversion." << endl;
cout << "What is the decimal number?";
cin >> decNum;
cout << endl;
//convert one digit decimal numbers to roman numeral by calling function
if (decNum.size() == 1) {
OnesPlace();
}
//convert two digit decimal numbers to roman numeral by calling function
if (decNum.size() == 2) {
TensPlace();
}
//convert three digit decimal numbers to roman numeral by calling function
if (decNum.size() == 3) {
HundredsPlace();
}
//convert four digit decimal numbers to roman numeral by calling function
if (decNum.size() == 4) {
ThousandsPlace();
}
//output the converted decimal to roman numeral
cout << "The decimal number " << numeral << " as a Roman Numeral is " << decNum << "." << endl;
return 0;
}