-
Notifications
You must be signed in to change notification settings - Fork 0
/
RomanConverter.cpp
95 lines (85 loc) · 2.38 KB
/
RomanConverter.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
#include "RomanConverter.h"
RomanConverter::RomanConverter()
{
initAllTables();
}
RomanConverter::~RomanConverter()
{
}
void RomanConverter::initAllTables()
{
// Initializing mCoversion Map.
mConversion['M'] = 1000;
mConversion['D'] = 500;
mConversion['C'] = 100;
mConversion['L'] = 50;
mConversion['X'] = 10;
mConversion['V'] = 5;
mConversion['I'] = 1;
// Initializing mPriority Map.
mPriority['I'] = 0;
mPriority['V'] = 1;
mPriority['X'] = 2;
mPriority['L'] = 3;
mPriority['C'] = 4;
mPriority['D'] = 5;
mPriority['M'] = 6;
// Initializing mRomanTable Vector.
mRomanTable.push_back(make_pair("M", 1000));
mRomanTable.push_back(make_pair("CM", 900));
mRomanTable.push_back(make_pair("D", 500));
mRomanTable.push_back(make_pair("CD", 400));
mRomanTable.push_back(make_pair("C", 100));
mRomanTable.push_back(make_pair("XC", 90));
mRomanTable.push_back(make_pair("L", 50));
mRomanTable.push_back(make_pair("XL", 40));
mRomanTable.push_back(make_pair("X", 10));
mRomanTable.push_back(make_pair("IX", 9));
mRomanTable.push_back(make_pair("V", 5));
mRomanTable.push_back(make_pair("IV", 4));
mRomanTable.push_back(make_pair("I", 1));
}
string RomanConverter::numberToRoman(int number)
{
string roman = "";
vector< pair<string, int> >::iterator it;
number = (number < 0) ? (-1*number) : number;
for(it = mRomanTable.begin(); it != mRomanTable.end(); it++)
{
while(number - it->second >= 0)
{
number = number - it->second;
roman += it->first;
}
}
return roman;
}
int RomanConverter::romanToNumber(string roman)
{
int number = 0;
int i= 0;
// Validating Roman number
for(int i=0; i<roman.length(); i++)
{
if(mConversion.find(roman[i]) == mConversion.end())
{
cout << "Pl enter a valid Roman number" << endl;
return -1;
}
}
while(roman[i] != '\0')
{
if( mPriority[roman[i]] < mPriority[roman[i+1]] )
{
int tmp = mConversion[roman[i+1]] - mConversion[roman[i]];
number += tmp;
i += 2;
}
else
{
number += mConversion[roman[i]];
i++;
}
}
return number;
}