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

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

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;
}``````