Created
February 26, 2017 05:43
-
-
Save shubham100795/dd10c061bb27c26d40363e281a1d09d9 to your computer and use it in GitHub Desktop.
Roman Numerals to Integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<string> | |
using namespace std; | |
int getvalue(char r) | |
{ | |
if (r == 'I') | |
return 1; | |
if (r == 'V') | |
return 5; | |
if (r == 'X') | |
return 10; | |
if (r == 'L') | |
return 50; | |
if (r == 'C') | |
return 100; | |
if (r == 'D') | |
return 500; | |
if (r == 'M') | |
return 1000; | |
return -1; | |
} | |
int main() | |
{ | |
int sum=0; | |
string str; | |
string:: iterator it; | |
cin>>str; | |
for(it=str.begin();it!=str.end();it++) | |
{ | |
int v1=getvalue(*it); | |
int v2=getvalue(*(it+1)); | |
if(v1<v2) | |
{ | |
sum=sum-v1; | |
sum=sum+v2; | |
it++; | |
} | |
else | |
sum=sum+v1; | |
} | |
cout<<endl<<sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment