Please help.. Write a script/function that converts a Roman numeral to its decim
ID: 3545455 • Letter: P
Question
Please help..
Write a script/function that converts a Roman numeral to its decimal equivalent. There are two distinct situations that you might design your program for: The "old" style where the order of the symbols does not matter. In this case, IX and XI both mean 10 + 1 or 11. You should be able to handle the following conversion table: The "new" style where the order of the symbols does matter. For example, IX is 9 (10 -1), XC is 90 (100 -10). The conversion table given above still holds and you may assume for this case that the only instances of "order" you will encounter are IV (4), IX (9), XL (40), XC (90), CD (400) and CM (900)The function input will be useful here. The format » str = input('Roman numeral: ','s') will provide a way to get the Roman number into your program as a string. It would be a good idea to try case a. first. Write a function that will do the inverse of the previous problem - convert a decimal number into a Roman number.Explanation / Answer
1
a.
#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;
}