Question

Write a program in C++ that converts a number entered in Roman numerals to a positive...

  1. Write a program in C++ 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:

    1. Store the number as a Roman numeral.

    2. Convert and store the number as a positive integer.

    3. Print the number as a Roman numeral or positive integer as requested by the user.

      The integer 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

using namespace std;

//romanType class

class romanType{

                private:

                //string field to store roman numeral

                string roman;

                public:

                //constructor to initialize roman numeral, assuming input is valid

                romanType(string roman_num){

                                roman=roman_num;

                }

                //method to return the integer value of roman numeral

                int integer_value() const{

                                int value=0;

                                //looping through each character

                                for(int i=0;i<roman.length();i++){

                                               //adding the value of current roman numeral to value

                                               if(roman[i]=='M'){

                                                               value+=1000;

                                               }else if(roman[i]=='D'){

                                                               value+=500;

                                               }else if(roman[i]=='C'){

                                                               value+=100;

                                                }else if(roman[i]=='L'){

                                                               value+=50;

                                               }else if(roman[i]=='X'){

                                                               value+=10;

                                               }else if(roman[i]=='V'){

                                                               value+=5;

                                               }else if(roman[i]=='I'){

                                                               //if I is coming before X, adding 9 instead of 1

                                                               if(i+1<roman.length() && roman[i+1]=='X'){

                                                                               value+=9;

                                                                               i+=1; //skipping next X

                                                               }

                                                               //if I is coming before V, adding 4 instead of 1

                                                               else if(i+1<roman.length() && roman[i+1]=='V'){

                                                                               value+=4;

                                                                               i+=1; //skipping next V

                                                               }

                                                               else{

                                                                               //otherwise, just adding 1

                                                                               value+=1;

                                                               }                                                             

                                               }

                                }

                                return value;

                }

                //returns the roman numeral

                string roman_value() const{

                                return roman;

                }

};

int main(){

                //testing

                romanType r("MCXIV");

                cout<<"Roman: "<<r.roman_value()<<", Integer: "<<r.integer_value()<<endl;

                r=romanType("CCCLIX");

                cout<<"Roman: "<<r.roman_value()<<", Integer: "<<r.integer_value()<<endl;

                r=romanType("MDCLXVI");

                cout<<"Roman: "<<r.roman_value()<<", Integer: "<<r.integer_value()<<endl;

                return 0;

}

/*OUTPUT*/

Roman: MCXIV, Integer: 1114

Roman: CCCLIX, Integer: 359

Roman: MDCLXVI, Integer: 1666

Add a comment
Know the answer?
Add Answer to:
Write a program in C++ that converts a number entered in Roman numerals to a positive...
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++ 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 1000D 500C 100L 50X 10V 5I 1d. Test your program using the followingRoman numerals: MCXIV, CCCLIX,...

  • 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....

  • This lab will exercise your understanding of some of the concepts covered in Chapter 10: classes,...

    This lab will exercise your understanding of some of the concepts covered in Chapter 10: classes, default constructors, overloaded constructors, arrays of classes    A Roman numeral represents an integer using letters. Examples are XVII to represent 17, MCMLIII for 1953, and MMMCCCIII for 3303. By contrast, ordinary numbers such as 17 or 1953 are called Arabic numerals. The following table shows the Arabic equivalent of all the single-letter Roman numerals: M 1000 X 10 D 500 V 5 C...

  • Write a function that converts an integer number in the range [1, 2000] into Roman Numerals....

    Write a function that converts an integer number in the range [1, 2000] into Roman Numerals. void IntToRoman(int n, char *roman); Write a main() program to test and demonstrate your function

  • *KEEP IT SIMPLE PLEASE* Write a program that allows the user to convert any integer number...

    *KEEP IT SIMPLE PLEASE* Write a program that allows the user to convert any integer number to Roman numeral and the other way around. - You should create and print a menu that allows the user to select either Roman to Int or Int to Roman. - If one option is selected then you should promote the user to enter the number and then you print the equivalence in the other system. - Validate input, i.e Roman numerals should be...

  • 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

  • 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...

  • 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