Question

The decimal values of the Roman numerals are: M D C L X V I 1000...

The decimal values of the Roman numerals are:

M D C L X V I
1000 500 100 50 10 5 1

Remember, a larger numeral preceding a smaller numeral means addition, so LX is 60. A smaller numeral preceding a larger numeral means subtraction, so XL is 40.

Assignment:

Begin by creating a new project in the IDE of your choice. Your project should contain the following:

Write a class romanType. An object of romanType should have the following attributes:

  • An integer value stored as a Roman numeral (for this problem, our numbers will only be up to two numerals long). Hint: consider using strings or an array of strings.
  • The number stored in decimal form (base-10)

The class should have methods that allow for the following:

  • an overloaded stream extraction operator to read in Roman numerals
  • an overloaded stream insertion operator for printing out the value in BOTH Roman numeral and decimal form (both will always be printed out at the same time)
  • a method (or methods) for converting and storing the number in decimal form
  • any other methods you deem necessary

Example Run and main function

  • I have provided the following main function (Links to an external site.) (in a separate .cpp file) that you MUST use, and several example runs (Links to an external site.) whose output you MUST match.
  • FINALLY, I have provided the implementation for a method I called, romanToDecimal (Links to an external site.). You may find this implementation useful, but it is not strictly required that you use it.

Compress your ENTIRE project folder into a single .zip file (the folder where your project and souce code files are) and submit the .zip file.

The main code


#include <iostream>
#include <string>
#include "Roman.h"

using namespace std;

int main()
{
romanType roman;

string romanString;

cout << "Enter a roman number: ";
cin >> roman;
cout << endl;

cout << roman;
cout << endl;

return 0;
}

The txt implementation

void romanType::romanToDecimal()
{
int sum = 0;
int length = romanNum.length();
int i;
  
   int previous = 1000;

for (i = 0; i < length; i++)
{
switch (romanNum[i])
{
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;
previous = 1;
}
}

decimalNum = sum;
}

The sample runs

Example Run #1:
=======
Enter a roman number: II

II is 2


=======

Example Run #2:
=======
Enter a roman number: IV

IV is 4


=======

Example Run #3:
=======
Enter a roman number: VI

VI is 6


=======

Example Run #4:
=======
Enter a roman number: CM

CM is 900

Homework Answers

Answer #1

Updated code here Main.cpp, Roman.cpp and Roman.h three separate file as per your request and also not calling romanToDecimal() from main.

Note: here .zip file is not upload here, so uploading direct .cpp and .h file here.

Explain al all code in comment section but If you still have any doubt, feel free to ask me or post the doubt here.

Roman.h

#include<iostream>
using namespace std;

class romanType
{
  private: 
          
  public: 
        string romanNum;
        int decimalNum;
         romanType(string r = "") 
        {  
           romanNum = r;   
           decimalNum = 0; 
        } 
        friend ostream & operator << (ostream &out, const romanType &r); 
        friend istream & operator >> (istream &in,  romanType &r); 
        friend void romanToDecimal(romanType &r);
};

Roman.cpp

#include <string>
#include "Roman.h"

ostream & operator << (ostream &out, const romanType &r) {             
    out << r.romanNum << " is " << r.decimalNum << endl; 
    return out; 
} 
  
istream & operator >> (istream &in,  romanType &r) { 
    in >> r.romanNum; 
    
    // here call romanToDecimal inside at the type of value entered
    romanToDecimal(r);
    return in; 
} 

// this function return decimal value according to their Roman symbol represent
int value(char r) { 
  switch(r){
      case 'I': return 1;
      case 'V': return 5;
      case 'X': return 10;
      case 'L': return 50;
      case 'C': return 100;
      case 'D': return 500;
      case 'M': return 1000;
      default : return -1;
  } 
} 

void romanToDecimal(romanType &r){
    int res = 0; 
    string str = r.romanNum;
  
    for (int i = 0; i < str.length(); i++) { 
    
    // Getting ith char value to s1 into ASCII FORMAT (char to int)
        int s1 = value(str[i]); 
  
        if (i + 1 < str.length()) { 
        
        // Storing (i+1)th char value to s1 into ASCII foramt
            int s2 = value(str[i + 1]); 
  
            // Comparing both s1 and s2 char value
            if (s1 >= s2) { 
                res = res + s1; 
            } 
            
            // Value of current char is less than the next char 
            else { 
                res = res + s2 - s1; 
                i++; 
            } 
        } 
        else { 
            res = res + s1; 
        } 
    } 
    
    // store in romanType class variable decimalNum (public type)
    r.decimalNum = res;
}

Main.cpp

#include "Roman.cpp"

int main(){
    romanType roman;
    string romanString;
    
    cout << "Enter a roman number: ";
    cin >> roman;
    cout << roman;
    cout << endl;
    return 0;
}
Know the answer?
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
Similar Questions
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...
python programming Question #4: # Years ago the Romans used a different system to represent numbers....
python programming Question #4: # Years ago the Romans used a different system to represent numbers. # Instead of using the digits (0, 1, 2, 3, 4, 5, 6, etc.), the Romans # formed numbers by joining combinations of the characters # (I, V, X, L, C, D, and M). # Roman Numeral characters and their integer values are: # I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and M...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...