-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman_to_Integer.cpp
97 lines (96 loc) · 2.16 KB
/
roman_to_Integer.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
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
enum RomanNumeral
{
I = 1,
V = 5,
X = 10,
L = 50,
C = 100,
D = 500,
M = 1000
};
class Solution
{
public:
int romanToInt(string s)
{
if (s.length() == 0)
return 0;
else
{
int sum = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == 'I')
{
if (s[i + 1] == 'V' || s[i + 1] == 'X')
{
sum -= I;
}
else
{
sum += I;
}
}
else if (s[i] == 'V')
{
sum += V;
}
else if (s[i] == 'X')
{
if (s[i + 1] == 'L' || s[i + 1] == 'C')
{
sum -= X;
}
else
{
sum += X;
}
}
else if (s[i] == 'L')
{
sum += L;
}
else if (s[i] == 'C')
{
if (s[i + 1] == 'D' || s[i + 1] == 'M')
{
sum -= C;
}
else
{
sum += C;
}
}
else if (s[i] == 'D')
{
sum += D;
}
else if (s[i] == 'M')
{
sum += M;
}
}
return sum;
}
}
};
int main()
{
string s;
cout << "Enter the Roman numeral: ";
cin >> s;
Solution sol;
// here I 1
// V 5
// X 10
// L 50
// C 100
// D 500
// M 1000
cout << "The integer value of the Roman numeral " << s << " is: " << sol.romanToInt(s) << endl;
return 0;
}