Write a program that converts a number entered in Roman numerals to decimal form
ID: 3767564 • Letter: W
Question
Write a program that converts a number entered in Roman numerals to decimal form.
Your program should consist of a class, say romanType.
An object of 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.
(Write two separate functions—one to print the number as a Roman numeral and the other
to print the number as a decimal number.)
The decimal values of the Roman numerals are:
M 1000
D 500
C 100
L 50
X 10
V 5
I 1
Remember, a larger numeral preceding a smaller numeral means addition, so LX is 60.
A smaller numeral preceding a larger numeral means subtrac- tion, so XL is 40.
Any place in a decimal number, such as the 1s place, the 10s place, and so on,
requires from zero to four Roman numerals.
d. Test your program using the following Roman numerals: MCXIV, CCCLIX, and MDCLXVI.
Also is it possible to not get a copy of the code from any of these sites or elsewhere online.
http://www.csassignmentassist.com/qaboard.php?qid=291
http://www.dreamincode.net/forums/topic/22135-roman-numeral
http://www.cplusplus.com/forum/general/141571
https://www.daniweb.com/programming/software-development/threads/41064/homework-help-with-classes-plz-
Just looking for unique code that can't be found online at a bunch of sites already
Thanks much
Explanation / Answer
Code : Header file
Code: .cpp file
Program:
#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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.