Write a program that converts a number entered in roman
numerals to decimal. Your program should
consist of a class, say, RomanType and
must have following methods:
a.
Store the number as a Roman numeral.
b.
Convert and store the number into decimal form.
d.
The decimal values of the roman numbers are:
M=100, D=500, C=100, L=50, X=10, V=5, I=1
e.
Test your program using the following Roman
numbers: MCXIV, CCCLIX, MIXLXVI.
SOLUTION:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class romanType
{
private:
char romanNum[6];
int sum;
public:
void printRoman(char romanNum[6]);
int convertRoman(char romanNum[6]);
int printDecimal(int sum);
};
void romanType::printRoman(char romanNum[6])
{
cout << "Here is your number in Roman Numeral form: " << romanNum << endl;
}
int romanType::convertRoman(char romanNum[6])
{
int total=0, sum;
int len = 0;
len = strlen(romanNum);
int count;
for(int i = 0; i < len; i++)
{
switch(romanNum[i])
{
case 'M':
count = 1000;
break;
case 'm':
count = 1000;
break;
case 'D':
count = 500;
break;
case 'd':
count = 500;
break;
case 'C':
count = 100;
break;
case 'c':
count = 100;
break;
case 'L':
count = 50;
break;
case 'l':
count = 50;
break;
case 'X':
count = 10;
break;
case 'x':
count = 10;
break;
case 'V':
count = 5;
break;
case 'v':
count = 5;
break;
case 'I':
count = 1;
break;
case 'i':
count = 1;
break;
}
total = total + count;
sum = total;
}
cout<<sum;
return sum;
}
/*int romanType::printDecimal(int sum)
{
cout << "Here is your number in Decimal form: " << sum << endl;
return sum;
}*/
int main()
{
romanType r;
char romanNum[6];
char choice;
int sum;
clrscr();
cout << " Please enter your Roman Numeral: " << endl;
cin >> romanNum;
cout << endl;
cout << "Do you want the Roman Numeral or the Decimal?" << endl;
cout << "Press [D] for Decimal!" << endl << "Press [R] for Roman Numeral!" << endl;
cin >> choice;
if (choice == 'D' || choice == 'd')
r.convertRoman(romanNum);
else if (choice == 'R' || choice == 'r')
r.printRoman(romanNum);
else
cout << "That wasn't the right button!" << endl;
getch();
return 0;
}
OUTPUT:
This solution does not produce accurate results.
ReplyDelete