You are to design and implement a Roman numeral calculator. The subtractive Roma
ID: 3639856 • Letter: Y
Question
You are to design and implement a Roman numeral calculator. The subtractive Roman numeral notation commonly in use today was used only rarely during the time of the Roman Republic and Empire. For ease of calculation, the Romans most frequently used a purely additive notation in which a number was simply the sum of its digits (4 equals IIII in this notation, not IV). Each number starts with the digit of highest value and ends with the digit of smallest value. This is the notation you will use in this program.Your program inputs two Roman numbers and an arithmetic operator and prints out the result of the operation, also as a Roman number. The values of the Roman digits are as follows:
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Thus, the number MDCCCCLXXXXVI represents 1996, because 1996 really consists of:
1000 + 500 + 100 + 100 + 100 + 100 + 50 + 10 + 10 + 10 + 10 + 5 + 1.
M D C C C C L X X X X V I
The arithmetic operators that your program should recognize in the input are +, -, *, and /. These should perform the C++ operations of integer addition, subtraction, multiplication, and division.
One way of approaching this problem is to convert the Roman numbers into integers, perform the required operation, and then convert the result back into a Roman number for printing.
Assume that the input numbers are in purely additive form - that is, digits are followed only by digits of the same or lower value. Also assume that the letters are all legal, no need to check for errors in the input file. Also, assume that the answer to each calculation will be a positive number.
REQUIREMENTS: This program is to be done using functions. These functions must NOT reference global variables directly (use parameter lists). The prototypes and descriptions below are a suggested method of breaking up the problem into functions if you prefer to design your own functions, feel free to do so, but make sure you have both value-returning and void functions in your solution.
FUNCTION get_Data
int get_Data(ifstream&, char);
This function receives the input file, reads one series of chars representing a Roman numeral, and sends back the value of the numeral read in. This function can call the function convert_from_Roman_to_Decimal to do the conversion while it is reading each letter.
FUNCTION convert_from_Roman_to_Decimal
int convert_from_Roman_to_Decimal(char);
This function is to receive a char (e.g. an i Mi or a i Ci etc.) and return its corresponding integer value as an integer. Use a value-returning function. It can be called from the get_Data function.
FUNCTION get_Oper
char get_Oper(ifstream&);
This function receives the input file, reads the operator, and sends back the character read in.
FUNCTION calc_Romans
void calc_Romans(int, int, char, int&);
This function is given the two integers and a char (the operator) and returns the result of doing the required operation on the two integers, (using the reference 4th parameter.)
FUNCTION print_Roman_Result
void print_Roman_Result(int);
This void function receives the integer result of the calculation, and prints out each Roman letter while it is processing the integer into the Roman letters. It does not have to return anything to the calling program. It is expected that the output will go to the screen.
INPUT/OUTPUT: The input file will have a number of lines. Each line will have two Roman numbers followed by an operator, separated by blanks. Include a copy of the file with your program and output. The style of the data file looks as follows:
MCCXXVI CV +
MCCXXVI MCCXXVI /
...
etc.
Output Example:
The two lines above would produce the output:
The first number is MCCXXVI ( 1226 ).
The second number is CV ( 105 ).
The operator is +
The result is MCCCXXXI ( 1331 ).
************************************************
The first number is MCCXXVI ( 1226 ).
The second number is MCCXXVI ( 1226 ).
The operator is /
The result is I ( 1 ).
Hint: Write this program in stages, concentrating on one function at a time. E.g. write the Get_Data function and a main program to test it. Make sure that it works and can read a Roman numeral in the file correctly before working on any of the other functions. Your main program will be quite short with lots of function calls to do all the work.
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//PROTOTYPES
int get_Data(ifstream&);
char get_Oper(ifstream&);
void calc_Romans(int, int, char, int&);
void print_Roman_Result(int);
//LOCAL DECLARATIONS
int num1;
int num2;
char oper;
int result;
ifstream fin;
//PROCEDURES
fin.open("roman.txt");
if (!fin)
{
cout << "Error: cannot open data file. Program aborting. ";
exit(1);
}
while (num1 = get_Data(fin))
{
num2 = get_Data(fin);
oper = get_Oper(fin);
calc_Romans(num1, num2, oper, result);
cout << "The first number is ";
print_Roman_Result(num1);
cout << " ( " << num1 << " ). ";
cout << "The second number is ";
print_Roman_Result(num2);
cout << " ( " << num2 << " ). ";
cout << "The operator is " << oper << endl;
cout << "The result is ";
print_Roman_Result(result);
cout << " ( " << result << " ). ";
cout << endl;
}
fin.close();
cout << endl;
cin.get();
return 0;
}
//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
int convert_from_Roman_to_Decimal(char symbol)
{
if (toupper(symbol) == 'I')
return 1;
else if (toupper(symbol) == 'V')
return 5;
else if (toupper(symbol) == 'X')
return 10;
else if (toupper(symbol) == 'L')
return 50;
else if (toupper(symbol) == 'C')
return 100;
else if (toupper(symbol) == 'D')
return 500;
else if (toupper(symbol) == 'M')
return 1000;
return 0;
}
//---------------------------------------------------------
void calc_Romans(int lhs, int rhs, char oper, int &result)
{
if (oper == '+')
result = lhs + rhs;
else if (oper == '-')
result = lhs - rhs;
else if (oper == '*')
result = lhs * rhs;
else if (oper == '/')
result = lhs / rhs;
else
result = 0;
}
//---------------------------------------------------------
int get_Data(ifstream& fin) //why we need an extra "char" here in the parameter list??
{
char temp[29]; //maximum char could be 28
char *ptr = temp;
int romanNum = 0;
fin >> temp;
while (*ptr)
romanNum += convert_from_Roman_to_Decimal(*ptr++);
return romanNum;
}
//---------------------------------------------------------
char get_Oper(ifstream& fin)
{
char oper = ' ';
fin >> oper;
return oper;
}
//---------------------------------------------------------
void print_Roman_Result(int num)
{
while (num >= 1000)
{
cout << "M";
num -= 1000;
}
while (num >= 500)
{
cout << "D";
num -= 500;
}
while (num >= 100)
{
cout << "C";
num -= 100;
}
while (num >= 50)
{
cout << "L";
num -= 50;
}
while (num >= 10)
{
cout << "X";
num -= 10;
}
while (num >= 5)
{
cout << "V";
num -= 5;
}
while (num)
{
cout << "I";
num--;
}
}
Sample run:
Enter file name: roman.txt
The first number is MCCXXVI ( 1226 ).
The second number is CV ( 105 ).
The operator is +
The result is MCCCXXXI ( 1331 ).
The first number is MCCXXVI ( 1226 ).
The second number is MCCXXVI ( 1226 ).
The operator is /
The result is I ( 1 ).
The first number is MMC ( 2100 ).
The second number is XIII ( 13 ).
The operator is -
The result is MMLXXXVII ( 2087 ).
The first number is V ( 5 ).
The second number is CXX ( 120 ).
The operator is *
The result is DC ( 600 ).
Contents of roman.txt
MCCXXVI CV +
MCCXXVI MCCXXVI /
MMC XIII -
V CXX *
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.