ok so in the question i am missing \"C\". just add it in here. I am not sure whe
ID: 3651425 • Letter: O
Question
ok so in the question i am missing "C". just add it in here. I am not sure where i missed it atWrite a program that converts a number entered in Roman numerals to
decimal. Your program should consist of a class, say, romanType. An
object of type 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 or decimal number as requested
by the user.
The decimal values of the Roman numerals are:
M 1000
D 500
C 100
L 50
X 10
V 5
I 1
d. Test your program using the following Roman numerals: MCXIV,
CCCLIX, MDCLXVI.
#include <iostream>
using namespace std;
class romanType{
public:
romanType(char&); // ** make constructor
int convert();
void print();
void get();
private:
int M, D, C, L, X, V, I;
char romanNumeral;
};
romanType::romanType(char &ch){
M = 1000;
D = 500;
C = 100;
L = 50;
X = 10;
V = 5;
I = 1;
cout << ch << endl;
romanNumeral=ch; // ** set up romanNumeral
}
int romanType::convert()
{
if (romanNumeral == 'M')
{
return 1000; // ** return value
}
else if(romanNumeral == 'D')
{
return 500;
}
else if(romanNumeral == 'C')
{
return 100;
}
else if(romanNumeral == 'L')
{
return 50;
}
else if(romanNumeral == 'X')
{
return 10;
}
else if(romanNumeral == 'V')
{
return 5;
}
else if(romanNumeral == 'I')
{
return 1;
}
return 0; // ** error -
}
void romanType::print()
{
cout << romanNumeral << endl;
}
void romanType::get()
{
}
int main(){
char romanNumeral;
cout << "Welcome to the Roman numeral to decimal converter! Please enter a number in Roman numerals to be converted: ";
cin >> romanNumeral;
romanType roman=romanType(romanNumeral); // ** set up value
cout << roman.convert() << endl;;
system("pause");
return 0;
Print The Roman Numeral
r.printRoman();
Print The Decimal Form Of Numeral
r.printDecimal();
}
Explanation / Answer
Please rate...
Program:
===============================================
#include<iostream>
#include<string.h>
using namespace std;
class romanType
{
private: char rn[100];
int dn;
public:romanType()
{
int i;
for(i=0;i<100;i++)
{
rn[i]='';
}
dn=0;
}
public:romanType(char n[])
{
strcpy(rn,n);
dn=0;
}
public: void romanToDecimal();
public: int toDecimal(char);
public: void printRoman();
public: void printDecimal();
};
void romanType::romanToDecimal()
{
int i=0;
int s=toDecimal(rn[i]);
while(rn[i])
{
if(s>=toDecimal(rn[i]))dn = dn+toDecimal(rn[i]);
if(s<toDecimal(rn[i]))dn = dn-s+(toDecimal(rn[i])-s);
s=toDecimal(rn[i]);
i++;
}
}
int romanType::toDecimal(char symbol)
{
if (toupper(symbol) == 'I')
return 1;
else if (toupper(symbol) == 'V')
return 5;
else if (toupper(symbol) == 'X')
return 10;
else if (toupper(symbol) == 'L')
return 50;
else if (toupper(symbol) == 'C')
return 100;
else if (toupper(symbol) == 'D')
return 500;
else if (toupper(symbol) == 'M')
return 1000;
return 0;
}
void romanType::printRoman()
{
cout<<" The roman number is: "<<rn;
}
void romanType::printDecimal()
{
cout<<" The decimal number is: "<<dn;
}
int main()
{
char a[100];
cout<<"Enter the roman number: ";
cin>>a;
romanType r1(a);
r1.romanToDecimal();
r1.printRoman();
r1.printDecimal();
}
===================================================
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.