1. Write a program that converts a number entered in Roman numerals to decimal.
ID: 3644566 • Letter: 1
Question
1. Write a program that converts a number entered in Roman numerals todecimal. 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.
Explanation / Answer
// Roman Numerals To Decimal.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" // Used because I compile with MS Visual C++ 2008, and this is needed for it
#include <iostream>
#include <string>
using namespace std;
struct RomanNumber
{
public:
string get()
{
return romanNumber;
}
void show()
{
cout << "Roman Number is " << romanNumber << endl;
}
RomanNumber(const string &input)
{
romanNumber = input;
}
int convert()
{
int length = romanNumber.length();
int previous = 0;
bool error = false;
int nIndex = 0;
sum = 0;
while( (error == false) && (nIndex < length) )
{
switch(romanNumber[nIndex])
{
case 'M':
sum += 1000;
if(previous < 1000)
{
sum -= 2 * previous;
}
previous = 1000;
break;
case 'D':
sum += 500;
if(previous < 500)
{
sum -= 2 * previous;
}
previous = 500;
break;
case 'C':
sum += 100;
if(previous < 100)
{
sum -= 2 * previous;
}
previous = 100;
break;
case 'L':
sum += 50;
if(previous < 50)
{
sum -= 2 * previous;
}
previous = 50;
break;
case 'X':
sum += 10;
if(previous < 10)
{
sum -= 2 * previous;
}
previous = 10;
break;
case 'V':
sum += 5;
if(previous < 5)
{
sum -= 2 * previous;
}
previous = 5;
break;
case 'I':
sum += 1;
if(previous < 1)
{
sum -= 2 * previous;
}
previous = 1;
break;
default:
cout << romanNumber[nIndex] << " is not a Roman Numeral!" << endl;
error = true;
sum = 0;
} // switch
nIndex++;
} // while
return sum;
}
int length()
{
return romanNumber.length();
}
private:
string romanNumber;
int sum;
};
int main()
{
string myInput;
int value;
cout << "Please enter the Roman Numeral to convert : ";
cin >> myInput;
RomanNumber myRomanNumber(myInput);
value = myRomanNumber.convert();
cout << "Roman Number " << myInput << " equals " << value <<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.