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
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++ 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...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
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...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the following CFG: 1. exps --> exp | exp NEWLINE exps 2. exp --> term {addop term} 3. addop --> + | - 4. term --> factor {mulop factor} 5. mulop --> * | / 6. factor --> ( exp ) | INT The 1st production defines exps as an individual expression, or a sequence expressions separated by NEWLINE token. The 2nd production describes an...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
C++. Write a program that draws a rocket shape on the screen based on user input...
C++. Write a program that draws a rocket shape on the screen based on user input of three values, height, width and stages. The type of box generated (i.e.. a hollow or filled-in) is based on a check for odd or even values input by the user for the box height (or number of rows). Here is the general specification given user input for the height of the box..... Draw a hollow box for each stage if the value for...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT