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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include<iostream>
#include<iomanip>
using namespace std;
const int index = 20;
const int romInd = 7;
//class for converting the numerals
class romanConvert
{
public:
char romLet[romInd] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
int getRoman(int decInteger)
{
return decInteger;
}
void setRoman(char romLet)
{
romLet = romNum;
}
romanConvert(char romNum[index],int decInteger);
private:
char romNum;
int decInteger;
};
int main()
{
//index to hold the user entered values
char numeral[index];
int decInteger = 0;
//explains the program to user
cout << "This program will take a Roman numeral then, " << endl
<< "return it's value as a decimal integer." << endl << endl;
cout << "The value of Roman numerals:" << endl;
cout << "M = 1000" << endl
<< "D = 500" << endl
<< "C = 100" << endl
<< "L = 50" << endl
<< "X = 10" << endl
<< "V = 5" << endl
<< "I = 1" << endl << endl;
//user input prompt
cout << "To exit, type E as your first entry." << endl;
cin >> numeral;
//loops to read each character input individually
for (int i = 0; i < index; i++)
{
//ensures all input is upper case when read
numeral[i] = toupper(numeral[i]);
//gives the user the option to exit early
if (numeral[0] == 'E')
{
cout << "Exiting program." << endl;
//don't forget to delete this
cin.ignore(2);
return '\0';
}
}
//setting constructer as the user input values
romanConvert converter(numeral, decInteger);
//constructor call
converter;
converter.getRoman();
//don't forget to delete this
cin.ignore(2);
return 0;
}
//constructor definition
romanConvert::romanConvert(char romNum[index], int decInteger)
{
int romVal[romInd] = { 1000, 500, 100, 50, 10, 5, 1 };
char romLet[romInd] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
int romLen = 0, check[index] = { 0 };
bool good = false;
//used to populate romNum
for (int i = 0; i < index; i++)
{
if (romNum[i] != '\0')
romLen++;
else
i = index;
}
//finds the number of characters input by the user
for (int i = 0; i < romLen; i++)
{
good = false;
//converts the roman number to an integer
for (int x = 0; x < romInd; x++)
{
if (romNum[index] == romLet[i])
{
good = true;
}
for (int i = 0; i < romLen; i++)
{
for (int x = 0; x < romInd; x++)
{
if (romNum[i] == romLet[x])
{
check[i] = romVal[x];
}
}
}
for (int i = 0; i < romLen; i++)
{
if ((check[i + 1] > check[i]))
{
decInteger += (check[i + 1] - check[i]);
i++;
}
else
decInteger += check[i];
}
}
}
cout << "Test output: " << decInteger << endl;
}
|