-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroman_to_decimal.cpp
More file actions
70 lines (69 loc) · 1.3 KB
/
roman_to_decimal.cpp
File metadata and controls
70 lines (69 loc) · 1.3 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
#include<iostream>
using namespace std;
int value(char a)
{
if(a=='I')
{
return 1;
}
if(a=='V')
{
return 5;
}
if(a=='X')
{
return 10;
}
if(a=='L')
{
return 50;
}
if(a=='C')
{
return 100;
}
if(a=='D')
{
return 500;
}
if(a=='M')
{
return 1000;
}
}
/* IF s[i]<s[i+1] then '-'
/*III=1+1+1 IX=-1+10 XI=10+1*/
int romanToDecimal(string &str)
{
int n=str.size();
int k1,k2,ans=0;
if(n==1)
{
ans=value(str[0]);
cout<<"\n "<<ans;
return ans;
}
for(int i=0;i<n-1;i++)
{
k1=value(str[i]);
k2=value(str[i+1]);
if(k1<k2)
{
ans=ans-k1;
}
else if(k1>=k2)
{
ans=ans+k1;
}
}
ans=ans+value(str[n-1]);
cout<<"\n"<<ans;
return ans;
}
int main()
{
string str;
cout<<"\n enter the roman numeber:";
cin>>str;
romanToDecimal(str);
}