must be in c++ please a working code In Programming Exercise 1 in Chapter 1, we
ID: 3673099 • Letter: M
Question
must be in c++ please a working code
In Programming Exercise 1 in 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 the stream insertion and stream extraction operators for easy input and output. The stream insertion operator outputs the Roman numeral in the Roman format.
Also, include a member function, decimalToRoman, that converts the decimal number (the decimal number must be a positive integer) to an equivalent Roman numeral format. Write the definition ofthe 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 ofthe letters V and X. For example, 4 is represented as IV, 9 is represented as IX, 39 is represented as XXXIX, and 49 is represented as XXXXIX. Also, 40 will be represented as XXXX, 190 will be represented as CLXXXX, and so on.
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 the 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 cannot 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.
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int decimalNumber;
int j; // Counter Variable
int m; // Numerical Value 1000
int d; // Numerical Value 500
int c; // Numerical Value 100
int l; // Numerical Value 50
int x; // Numerical Value 10
int ix; // Numerical Value 9
int v; // Numerical Value 5
int iv; // Numerical Value 4
int i; // Numerical Value 1
cout << " C++ Program to Convert Decimal to Roman Numeral" << endl;
cout << " Enter Decimal Number: ";
cin >> decimalNumber;
if (decimalNumber <= 0)
{
cout << " Error! Invalid number." << endl;
cout << " Press enter to continue..." << endl;
cin.ignore();
cin.get();
return 0;
}
m = decimalNumber / 1000;
decimalNumber = decimalNumber % 1000;
d = decimalNumber / 500;
decimalNumber = decimalNumber % 500;
c = decimalNumber / 100;
decimalNumber = decimalNumber % 100;
l = decimalNumber / 50;
decimalNumber = decimalNumber % 50;
x = decimalNumber / 10;
decimalNumber = decimalNumber % 10;
ix = decimalNumber / 9;
decimalNumber = decimalNumber % 9;
v = decimalNumber / 5;
decimalNumber = decimalNumber % 5;
iv = decimalNumber / 4;
decimalNumber = decimalNumber % 4;
i = decimalNumber;
cout << " Roman Numeral = ";
for (j = 1; j <= m; j++)
cout << "M";
for (j = 1; j <= d; j++)
cout << "D";
for (j = 1; j <= c; j++)
cout << "C";
for (j = 1; j <= l; j++)
cout << "L";
for (j = 1; j <= x; j++)
cout << "X";
for (j = 1; j <= ix; j++)
cout << "IX";
for (j = 1; j <= v; j++)
cout << "V";
for (j = 1; j <= iv; j++)
cout << "IV";
for (j = 1; j <= i; j++)
cout << "I";
cout << endl;
cout << " Press enter to continue..." << endl;
cin.ignore();
cin.get();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.