-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman.cpp
165 lines (149 loc) · 4.95 KB
/
roman.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
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
#include <iostream>
std::string romanUnit(int num) {
std::string roman = "";
if (num < 4) {
for (size_t i = 0; i < num; i++) {
roman += "I";
}
} else if (num == 4) {
roman += "IV";
} else if (num == 5) {
roman += "V";
} else if (num == 9) {
roman += "IX";
} else {
roman += "V";
for(size_t i = 0; i < num - 5; i++) {
roman += "I";
}
}
return roman;
}
std::string romanTens(int num) {
std::string roman = "";
if (num < 4) {
for (size_t i = 0; i < num; i++) {
roman += "X";
}
} else if (num == 4) {
roman += "XL";
} else if (num == 5) {
roman += "L";
} else if (num == 9) {
roman += "XC";
} else {
roman += "L";
for(size_t i = 0; i < num - 5; i++) {
roman += "X";
}
}
return roman;
}
std::string romanHundreds(int num) {
std::string roman = "";
if (num < 4) {
for (size_t i = 0; i < num; i++) {
roman += "C";
}
} else if (num == 4) {
roman += "CD";
} else if (num == 5) {
roman += "D";
} else if (num == 9) {
roman += "CM";
} else {
roman += "D";
for(size_t i = 0; i < num - 5; i++) {
roman += "C";
}
}
return roman;
}
std::string romanThousands(int num) {
std::string roman = "";
for(size_t i = 0; i < num; i++) {
roman += "M";
}
return roman;
}
std::string intToRoman(int num) {
if (num != 0) {
int numThousands = num/1000;
int numHundreds = (num - numThousands*1000)/100;
int numTens = (num - numThousands*1000 - numHundreds*100)/10;
int numUnit = num - numThousands*1000 - numHundreds*100 - numTens*10;
return (romanThousands(numThousands) + romanHundreds(numHundreds) + romanTens(numTens) + romanUnit(numUnit));
} else return "Error";
}
int romanToInt(std::string s) {
int finalNumber = 0;
for (size_t i = 0; i < s.size(); i++) {
switch(s[i]) {
case 'I':
if (s[i+1] == 'V' ) {
finalNumber += 4;
i++;
} else if (s[i+1] == 'X') {
finalNumber += 9;
i++;
} else finalNumber++;
break;
case 'V': finalNumber += 5;
break;
case 'X':
if (s[i+1] == 'L' ) {
finalNumber += 40;
i++;
} else if (s[i+1] == 'C') {
finalNumber += 90;
i++;
} else finalNumber += 10;
break;
case 'L': finalNumber += 50;
break;
case 'C':
if(s[i+1] == 'D') {
finalNumber += 400;
i++;
} else if(s[i+1] == 'M') {
finalNumber += 900;
i++;
} else finalNumber += 100;
break;
case 'D': finalNumber += 500;
break;
case 'M': finalNumber += 1000;
break;
}
}
return finalNumber;
}
int main() {
bool useAgain = true;
while (useAgain)
{
std::cout << std::endl << "What conversion do you want to make?" << std::endl
<< "1- Integer to Roman" << std::endl
<< "2- Roman to Integer" << std::endl
<< "3- Quit" << std::endl;
int option;
std::cin >> option;
int integer;
std::string roman;
switch (option)
{
case 1:
std::cout << std::endl << "Enter your integer: ";
std::cin >> integer;
std::cout << intToRoman(integer) << std::endl;
break;
case 2:
std::cout << std::endl << "Enter your roman: ";
std::cin >> roman;
std::cout << romanToInt(roman) << std::endl;
break;
case 3: useAgain = false;
break;
}
}
}