Book: DATA STRUCTURES USING C++, 2nd EDITION by D.S. Malik Chapter 1, Page 57, P
ID: 3624753 • Letter: B
Question
Book: DATA STRUCTURES USING C++, 2nd EDITION by D.S. MalikChapter 1, Page 57, Problem 1
Write a program that converts a number to entered in Roman Numerals to decimal form. Your program should consist of a class, say romanType.
An object of romanType should do the following:
a) Store the number as a Roman Numeral.
b) Convert and store the number into decimal form.
c) Print the number as a Roman Numeral as requested by the user. (Write two separate functions – one to print the number as a Roman Numeral and the other to print the number as a decimal number.)
The decimal values of the Roman Numerals are:
M 1000
D 500
C 100
L 50
X 10
V 5
I 1
Remember; a larger number preceding a smaller number means addition, so LX is 60. A smaller numeral preceding a larger numeral means subtraction, so XL is 40. Any place in a decimal number, such as the 1s place, the 10s place, and so on. Requires from zero to four Roman Numerals.
d) Test your program using the following Roman Numerals: MCXIV, CCCLIX, and MDCLXVI.
Explanation / Answer
// romanType.h
#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 ofromanType
// -----------------------------------------------------
// implementationfile romanTypeImp.cpp
#include<iostream>
#include "romanType.h"
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 < len -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 subtract the dec. val
// according to the values of j and pos
if ( j == pos )
decimal += decimals[ pos ];
else
decimal -= decimals[ pos ];
}
} // endfor
// process the last numeralvalue
for ( j = 0; j < 7; j++ )
if (roman.at( len - 1 ) == romans[ j ] )
break;
//add the dec. val of roman letter tothe dec. number
decimal += decimals[ j ];
} // end functionconvertToDecimal
void romanType::printRoman()
{
cout << " The roman numeralis " << roman;
} // end function printRoman
void romanType::printDecimal()
{
cout << " The decimalequivalent of the "
<< "given roman numeral is " << decimal;
} // end function printDecimal
// -----------------------------------------------------
// Main program
#include<iostream>
#include "romanType.h"
using namespace std;
int main() // function main beginsprogram execution
{
// let the user know aboutthe program
cout << " Program thatconvert Roman Numeral"
<< " into decimal form.";
// instantiate object of typeromanType
romanType r;
string rns[ 3 ] = { "CCCLIX","MCXIV", "MDCLXVI" };
for ( int i = 0; i < 3; i++)
{
// set the roman numeral string
r.setRoman( rns[ i ] );
// convert the roman numeral into decimal form
r.convertToDecimal();
// print the roman numeral
r.printRoman();
// print the decimal form of numeral
r.printDecimal();
} // endfor
cout << " ";
return 0; // indicate program executedsuccessfully
} // end of function, main