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
|
#include<string>
using namespace std;
class romanType
{
public:
romanType (string="");
void setRoman (string);
void convertToDecimal ();
void printRoman ();
void printDecimal ();
private:
string roman;
int decimal;
}; //End Class definition of romanType
#include <iostream>
using namespace std;
romanType::romanType (string myRoman)
{
roman=myRoman;
decimal=0;
} //End Constructor romanType
void romanType::setRoman (string myRoman)
{
roman=myRoman;
decimal=0;
} //End Function setRoman
void romanType::convertToDecimal ()
{
char romans[7]={'M', 'D', 'C', 'L', 'X', 'V', 'I'};
int decimals[7]={1000, 500, 100, 50, 10, 5, 1};
int j, pos;
size_t len=roman.length();
//Process the Numeral
for (unsigned int i=0; i<1; i++)
{
//Find the Roman Letter
for (pos=0; pos<7; pos++)
if (roman.at(i)==romans[pos])
break;
//Check for Validity of the Roman Letter
if (pos<7)
{
//Check the next Roman Letter's value
for (j=0; j<pos; j++)
if (roman.at(i+1)==romans[j])
break;
//add or substract the decimal value
//according to the values of j and pos
if (j==pos)
decimal+=decimals[pos];
else
decimal-=decimals[pos];
}
}//end for
//Process the last numeral value
for (j=0; j<7; j++)
if (roman.at(len-1)==romans[j])
break;
//Add the decimal value of Roman Letter to the Decimal number
decimal+=decimals[j];
}//End Function convertToDecimal
void romanType::printRoman()
{
cout<<"\n\n\tThe roman numeral is "<<roman;
} //End Function printRoman
void romanType::printDecimal()
{
cout<<"\n\tThe decimal equivalent of the given Roman numeral is "<<decimal;
}//End Function printDecimal
//Main Program
#include<iostream>
#include<string>
using namespace std;
int main () //Begins Program
{
string rnumeral;
romanType r;
//Information about the program
cout<<"\n\n\tProgram that converts Roman Numerals into Decimal form." ;
cout<<"Please enter a Roman Numeral"<<endl;
cin>>rnumeral;
for (int i=0; i<3; i++)
{
//Set the Roman Numeral String
r.setRoman (rnumeral);
//Convert the Roman Numeral into Decimal Form
r.convertToDecimal ();
//Print the Roman Numeral
r.printRoman ();
//Print the Decimal form of Numeral
r.printDecimal ();
} //End for
cout<<"\n\n\t";
system("pause");
return 0; //Indicates program executed successfully
}//end of function main
|