Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. there is no function decimalToRoman 2. No overloading of insertion and extrac

ID: 3651429 • Letter: 1

Question

1. there is no function decimalToRoman
2. No overloading of insertion and extraction operator.

3. No derived class extRomanType

4. No overloaded arithmetic operators for derived class


20. In Programming Exercise 1 Chapter 1, we defined a class romanType to implement Roman numerals in a program. In that exercise, we also implemented a function, romanToDecimal, to convert a Roman numeral into its equivalent decimal number.
Modify the definition of the class romanType so that the data members are declared as protected. Use the class string to manipulate the strings. Furthermore, overload ther stream insertion and stream extraction operators for easy input and output. The stream insertion operator outputs the Roman numeral in the Roman format.
Also, inclue a member function, decimalToRoman, that converts the decimal number( the decimal nmber must be a positive integer) to an equivalent Roman numeral format. Write the definition of the member function decimalToRoman.
For simplicity, we assume that only the letter I can appear in front of another letter and that it appears only in front of the letters V and X. For example, 4 is represented as IV, 9 is represented as IX, 39 is represented as XXXIX, etc.
b. Derive a class extRomanType from the class romanType to do the following. In the class extRomanType, overload the arithmetic operators +, -, *, and / so that arithmetic operations can be performed on Roman numerals. Also, overload the pre- and postincrement and decrement operators as member functions of the class extRomanType.
To add(subtract, multiply, or divide) Roman numerals, add(subtract, multiply, or divide respectively) their decimal representations and then convert the result to Roman numeral format. For subtraction, if the first number is smaller than the second number, output a message saying that, "Because the first number is smaller than the second, the numbers can not be subtracted". Similarly, for division, the numerator must be larger than the denominator. Use similar conventions for the increment and decrement operators.
c. Write the definitions of the functions to overload the operators described in part b
d. Write a program to test your class extRomanType.


void romanType::convertNum( int& total )
{
const int SIZE = std::strlen(num);
int count[SIZE] = { 0 } ;

for( int i = 0 ; i < SIZE ; ++i )
{
switch( ::toupper( num[i] ) )
{
case 'M':
count[i] = 1000;
break;
case 'D':
count[i] = 500;
break;
case 'C':
count[i] = 100;
break;
case 'L':
count[i] = 50;
break;
case 'X':
count[i] = 10;
break;
case 'V':
count[i] = 5;
break;
case 'I':
count[i] = 1;
break;
}
}

total = 0;

for (int i = 1 ; i < SIZE ; ++i )
{
if( count[i] > count[i-1] )
total -= count[i-1] ;
else
total += count[i-1] ;
}

total += count[ SIZE - 1 ] ;
}

Explanation / Answer

Rules regarding Roman numerals sometimes state that a symbol representing 10^n may not precede any symbol larger than 10^(n+1). For example, C cannot be preceded by I or V, only by X (or, of course, by a symbol representing a value equal to or larger than C). Thus, one should represent the number ninety-nine as XCIX (using decimal places -- 90 (XC) then 9 (IX)), not as the "shortcut" IC. Also, that no symbol representing a number equal to 5*10n where n is an integer (such as V, L, or D) can precede a symbol larger than it. For example 45 would be written as XLV instead of VL. However, these rules are not universally followed. For example, you can see 1990 sometimes written as MXM (instead of MCMXC), and 1999 written as IMM or MIM (instead of MCMXCIX). Assuming that the simpler set of rules hold, and 1990 can be written simply as MIM, the algorithm for converting a number from Roman Numerals to Decimal is easy: Step 1. Set the value of the decimal value to zero. Step 2. Check the string containing the Roman characters from left to right, keeping track of the values of the current and previous characters (use a switch as in your code): a. if we reach the end of the string, add the value of the previous character to the decimal value. And we are done. b. if the value of the current character is greater than the value of the previous character, subtract the value of the previous character from the decimal value. b. if the value of the current character is less than or equal to the value of the previous character, add the value of the previous character from the decimal value. void romanType::convertNum( int& total ) { const int SIZE = std::strlen(num); int count[SIZE] = { 0 } ; for( int i = 0 ; i