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
|
#include<iostream>
#include<iomanip>
using namespace std;
class romanType
{
public:
void displayChoices(char numeral[15])
{
cout << "Enter a roman numeral.\n";
cout << "Roman Numerals are as follows:\n";
cout << " M = 1000\n" << " D = 500\n" << " C = 100\n" << " L = 50\n" << " X = 10\n" << " V = 5\n" << " I = 1\n";
while (numeral[0] != 'Q')
{
cout << "\nTo quit, press Q.\n";
cin >> numeral;
for (int x = 0; x < 15; x++)
{
numeral[x] = toupper(numeral[x]);
if (numeral[x] == 'Q')
{
cout << "Thank you for using my Roman Numeral Conversion Program.\n";
}
else
romansArray(numeral);
}
}
}
void romansArray(char numeral[15])
{
int RomanValues[7] = { 1000, 500, 100, 50, 10, 5, 1 };
char Roman[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
int number = 0, check[15] = { 0 }, numLen = 0, x = 0;
bool ok = false;
for (int x = 0; x < 15; x++)// x incriments to fill the array 'numeral' up to 15 blocks
{
if (numeral[x] != '\0')
numLen++; // Without this incriment, the value of the roman numeral will be set to 0.
else
x = 15;
}
for (int x = 0; x < numLen; x++) //Incriments to see how long the user input was.
{
ok = false;
}
for (int y = 0; y < 7; y++) //Incriments to store the roman numeral as an integer.
{
if (numeral[x] == Roman[y])
{
ok = true;
}
if (ok = false)
{
cout << "Not a valid input. Please try again.\n\n";
}
}
for (int x = 0; x < numLen; x++)
{
for (int y = 0; y < 7; y++)
{
if (numeral[x] == Roman[y])
{
check[x] = RomanValues[y];
}
}
}
for (int x = 0; x < numLen; x++)
{
if ((check[x + 1] > check[x]))
{
number += (check[x + 1] - check[x]);
x++;
}
else
number += check[x];
}
cout << number << endl << endl;
}
void setRoman(char roman)
{
roman = numeral;
}
char getRoman()
{
return numeral;
}
private:
char numeral;
};
int main()
{
char choice[15] = "";
char romans[15] = "";
char number = 0;
romanType romanObject;
romanObject.displayChoices(choice);
romanObject.romansArray(romans);
romanObject.setRoman(number);
cout << romanObject.getRoman();
return 0;
}
|