|
|
maulk wrote: |
---|
#include <iostream> #include <string> using namespace std; int convertRomanToInt(string input); int romanCharValue(char r); int main() { cout << "Please enter a roman number or 'Q' to quit." << endl; } int convertRomanToInt(string input) { int total = 0; int value = 0; string romanNumerals = "IVXLCDM"; char numeral[7] = { 'I','V','X','L','C','D','M' }; for (int i = 0; i < input.length(); i++) { for (int j = 0; j < romanNumerals.length(); j++) { if (input.substr(i, 1) == romanNumerals.substr(j, 1)) { for (int k = 0; k < romanNumerals.length();k++) { if (input.substr((i + 1), 1) == romanNumerals.substr(k, 1)) { value = romanCharValue(numeral[k]); } } if (romanCharValue(numeral[j]) < value) { total -= romanCharValue(numeral[j]); } else if (romanCharValue(numeral[j]) >= value) { total += romanCharValue(numeral[j]); } } } } return total; } int romanCharValue(char r) { switch (r) { case 'I': {return 1; } case 'V': {return 5; } case 'X': {return 10; } case 'L': {return 50; } case 'C': {return 100; } case 'D': {return 500; } case 'M': {return 1000; } default: "error"; } } |