Question

C++ Write a program that converts anumber entered in Roman numerals to decimal.

In C++ Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class, say,romanType. An object of typeromanTypeshould do the following:

a. Store the number as a Romannumeral.

b. Convert and store the number into decimalform.

c. Print the number as a Roman numeral ordecimal 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 followingRoman numerals: MCXIV, CCCLIX, MDCLXVI.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear,

Here is the code


//Class Declaration
#include<string>
using namespace std;
class romanType
{
public :
romanType( string = "" );
void setRoman( string );
void convertToDecimal();
void printRoman();
void printDecimal();

private:
string roman;
int decimal;
};//end class definition of romanType
romanType::romanType( string myRoman )
{
roman = myRoman;
decimal = 0;
}//end constructor romanType

void romanType::setRoman( string myRoman )
{
roman = myRoman;
decimal = 0;
}//end function setRoman

void romanType::convertToDecimal()
{
char romans[7] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I'};
int decimals[ 7 ] = { 1000, 500, 100, 50, 10, 5, 1 };
int j, pos;

size_t len = roman.length();

//process the numeral
for ( unsigned int i = 0; i < len - 1; i++ )
{
//find the roman letter
for ( pos = 0; pos < 7; pos++ )
if ( roman.at( i ) == romans[ pos ] )
break;

//check for validity of the roman letter
if ( pos < 7 )
{
//check the next roman letter's value
for ( j = 0; j < pos; j++ )
if ( roman.at( i + 1 ) == romans[ j ] )
break;

//add or subtract the dec. val
//according to the values of j and pos
if ( j == pos )
decimal += decimals[ pos ];
else
decimal -= decimals[ pos ];
}
}//end for

//process the last numeral value
for ( j = 0; j < 7; j++ )
if ( roman.at( len - 1 ) == romans[ j ] )
break;
//add the dec. val of roman letter to the dec. number
decimal += decimals[ j ];

}//end function convertToDecimal

void romanType::printRoman()
{
cout << "nntThe roman numeral is " << roman;
}//end function printRoman

void romanType::printDecimal()
{
cout << "ntThe decimal equivalent of the "
<< "given roman numeral is " << decimal;
}//end function printDecimal

/* Main method to test the class*/
int main()//function main begins program execution
{
//let the user know about the program
cout << "nntProgram that convert Roman Numeral"
<< " into decimal form.";

//instantiate object of type romanType
romanType r;

string rns[ 3 ] = { "CCCLIX", "MCXIV", "MDCLXVI" };

for ( int i = 0; i < 3; i++ )
{
//set the roman numeral string
r.setRoman( rns[ i ] );

//convert the roman numeral into decimal form
r.convertToDecimal();

//print the roman numeral
r.printRoman();

//print the decimal form of numeral
r.printDecimal();
}//end for

cout << "nnt";
system( "pause" );
return 0;// indicate program executed successfully
}// end of function, main

Hope this will help you..

answered by: rakeem
Add a comment
Know the answer?
Add Answer to:
C++ Write a program that converts anumber entered in Roman numerals to decimal.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the roman.cpp implementation file that converts a number entered in Roman numerals to a positive...

    Write the roman.cpp implementation file that converts a number entered in Roman numerals to a positive integer. Your program should consist of a class, say, romanType. An object of type romanType should do the following: Step a: Store the number as a Roman numeral. Step b: Convert and store the number as a positive integer. Step c: Print the number as a Roman numeral or positive integer as requested by the user. Step d: Test your program using the following...

  • c++ object oriented programming

    object oriented programming:Write a program that converts a number entered in decimal toRoman numerals. Your program should consist of a class, say, romanType. Anobject of type romanType should do the following:a. Store the number as a decimal.b. Convert and store the number into roman form.c. Print the number as a Roman numeral or decimal number as requestedby the user.The decimal values of the Roman numerals are:M 1000D 500C 100L 50X 10V 5I 1

  • (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers....

    (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers. Write a program that converts a positive integer into the Roman number system. The Roman number system has digits I 1 V 5 X 10 L 50 C 100 D 500 M 1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately....

  • Write a C program to convert a given integer to roman number.Roman numerals are represented by...

    Write a C program to convert a given integer to roman number.Roman numerals are represented by 7different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C 100D 500M 1000

  • Write up a detailed solution to the problem: Design a program to convert a Roman numeral...

    Write up a detailed solution to the problem: Design a program to convert a Roman numeral to a decimal number. The program should read a Roman numeral. You may read it as a string or one character at a time. Do the conversion and then output the decimal number. Here are the “letters” you need to know: Symbol =   Value I   = 1 V =   5 X =   10 L =   50 C =   100 D =   500 M   =...

  • .data # This data will serve as our map between the roman # and the decimal numbers. Here, each list will # serve as our...

    .data # This data will serve as our map between the roman # and the decimal numbers. Here, each list will # serve as our makeshift "array", where the conversion # from decimal to roman will be according to each indice, # i.e. decimalNumeral[i] will dictate romanNumeral[i]. # This also will be useful when converting roman to # decimal, as romanNumeral[i] will dictate decimalNumeral[i] decimalNumeral: .word 1000 100 50 10 5 1 # the decimal map romanNumeral: .asciiz "MDCLXVI" #...

  • I'm having trouble getting a certain output with my program Here's my output: Please input a...

    I'm having trouble getting a certain output with my program Here's my output: Please input a value in Roman numeral or EXIT or quit: MM MM = 2000 Please input a value in Roman numeral or EXIT or quit: mxvi Illegal Characters. Please input a value in Roman numeral or EXIT or quit: MXVI MXVI = 969 Please input a value in Roman numeral or EXIT or quit: EXIT Illegal Characters. Here's my desired output: Please input a value in...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

  • Write and test a function toDecimal() that converts a roman number such as MCMLXXVII to its...

    Write and test a function toDecimal() that converts a roman number such as MCMLXXVII to its decimal number representation. Write a main program to test the function. Your function should have two arguments - the roman number as a string, and an error processing function. Write a helper function that will return the numeric value of each of the letters used in roman numbers. Then convert the string argument as follows look at the first two characters. If the first...

  • Create a function in python called “num_roman” that converts integers between 1 and 3999 into Roman...

    Create a function in python called “num_roman” that converts integers between 1 and 3999 into Roman numerals. The input will be a scalar number and it should output a string that is the Roman numeral representation of the input number. The function should give a meaningful error message if the input is less than one. The function should truncate a fractional value to an integer. Different meaningful error messages should occur if you enter a value greater than 3999 or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT