forked from srinidh-007/Coding_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathromanNumeral.cpp
More file actions
59 lines (50 loc) · 1.18 KB
/
romanNumeral.cpp
File metadata and controls
59 lines (50 loc) · 1.18 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
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
/*
a function which returns false when a character belongs to Roman literals else returns true
*/
bool is_roman(char x){
return !(x=='I' || x=='V' || x=='X' || x=='L' || x=='C' || x=='D' || x=='M');
}
/*
Roman to Numerical convert function
*/
int main(int argc,char* argv[]){
//condition to check null input
if(argv[1]==NULL){
cerr<<"Usage: please provide a string of roman numerals"<<endl;
exit(0);
}
//storing the roman number in a string
string s;
s=argv[1];
//checking each character for any invalid input
for(char c:s){
if(is_roman(c)){
cerr<<"Error: invalid string of roman numerals"<<endl;
exit(0);
}
}
//map to store the values of Roman characters and their corresponding integer value
map<char,int> value;
value['I']=1;
value['V']=5;
value['X']=10;
value['L']=50;
value['C']=100;
value['D']=500;
value['M']=1000;
//variable to store the value
int num=0;
for(int i=s.size()-1;i>=0;i--){
//To handle cases like IV where it should be considered as 4
if(value[s[i]]>value[s[i-1]] && i>0){
num+=value[s[i]]-value[s[i-1]];
i--;
}else{
num+=value[s[i]];
}
}
cout<<num<<endl;
}