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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
/**
* Computer Programming 2 (COMP2711, COMP8801)
* Practical 2: Roman Numbers
*/
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
using namespace std;
int main(int argc, char *argv[]) {
const string units[] = {"ZERO", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
const string tens[] = {"ZERO", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
const string hundreds[] = {"ZERO", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
const string thousands[] = {"ZERO", "M", "MM", "MMM"};
const string arabic[] = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9"};
string input;
while (cin >> input) {
bool isArabic = false;
for (int k = 0; k < (sizeof(arabic) / sizeof(arabic[0])) - 1; ++k) { // Find if first digit is an arabic numeral
if (input.find(arabic[k]) == 0) {
isArabic = true;
break;
}
}
if (isArabic);
{
string number;
for (int i = 0; i < (sizeof(arabic) / sizeof(arabic[0]) - 1); ++i) { //remove all prefixed arabic numerals and put into number variable
if (input.find(arabic[i]) == 0) {
cout << "arabic[i]" << arabic[i] << '\n';
number = number + arabic[i];
input.erase(0, (1));
i = 0;
}
}
//input = "";
cout << number << "test";
}
for (int i = 0; i < input.length(); ++i) { //Make input uppercase
input.at(i) = toupper(input.at(i));
}
if (
(!isArabic && input.find("I") == 0) ||
(!isArabic && input.find("V") == 0) ||
(!isArabic && input.find("X") == 0) ||
(!isArabic && input.find("L") == 0) ||
(!isArabic && input.find("C") == 0) ||
(!isArabic && input.find("D") == 0) ||
(!isArabic && input.find("M") == 0));
{
int result = 0;
for (int j = (sizeof(thousands) / sizeof(thousands[0])) - 1; j > 0; --j) {
if ((input.find(thousands[j]) ==
0)) { //if value of thousands index j is found at position 0 of input
result += (j * 1000);
input.erase(0, (thousands[j].length())); //Removes roman numerals from string
break;
}
}
for (int j = (sizeof(hundreds) / sizeof(hundreds[0])) - 1; j > 0; --j) {
if ((input.find(hundreds[j]) == 0)) {
result += (j * 100);
input.erase(0, (hundreds[j].length()));
break;
}
}
for (int j = (sizeof(tens) / sizeof(tens[0])) - 1; j > 0; --j) {
if ((input.find(tens[j]) == 0)) {
result += (j * 10);
input.erase(0, (tens[j].length()));
break;
}
}
for (int j = (sizeof(units) / sizeof(units[0])) - 1; j > 0; --j) {
if ((input.find(units[j]) == 0)) {
result += j;
input.erase(0, (units[j].length()));
break;
}
}
cout << result << "result"<<'\n';
}
}
}
|