Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

algorithm for roman numeral converter #112

Merged
merged 1 commit into from
Oct 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}