Skip to content

Commit

Permalink
Merge pull request #112 from dariodsa/master
Browse files Browse the repository at this point in the history
algorithm for roman numeral converter
  • Loading branch information
ambujraj authored Oct 1, 2018
2 parents 3b5a8b0 + a9b0c96 commit 110abad
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions roman/roman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
using namespace std;

string HELLO_MESSAGE = "Please enter roman number: ";
string RESULT = "Result is";
string ERROR = "There were an error in the input.";

string input;

map<char,int>mapa;

int roman_number(string S) {
int result = 0;

int last_digit = -1;
for(int i = 0, len = S.length();i < len; ++i) {
if(mapa[S[i]] == 0) return -1;

int digit_value = mapa[S[i]];
if(last_digit == -1) {
result = digit_value;
last_digit = digit_value;
} else if(digit_value <= last_digit) {
result += digit_value;
last_digit = digit_value;
} else { //IX CM
result -= last_digit;
result += (digit_value - last_digit);
last_digit = digit_value;
}
}

return result;
}

void init_map() {
mapa['I'] = 1;
mapa['V'] = 5;
mapa['X'] = 10;
mapa['L'] = 50;
mapa['C'] = 100;
mapa['D'] = 500;
mapa['M'] = 1000;
}

int main() {
cout << HELLO_MESSAGE;
cin >> input;
init_map();
int result = roman_number(input);
if(result == -1) {
cout << ERROR;
} else {
cout << RESULT << " " << result << endl;
}
return 0;
}

0 comments on commit 110abad

Please sign in to comment.