Emphasis on: functions, loops, reading from a text file
Write a C++ Roman numeral calculator. The subtractive Roman numeral notation commonly in use today (such as IV for 4) was apparently 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 is IIII in this notation). Each number starts with the digit of highest value and ends with the digit of lowest value.
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 Roman number MDCCCCLXXXXVIIII represents 1999 and MMXV represents 2015.
Your program should read from a file a series of Roman numeral arithmetic problems. Each problem consists of a Roman number, an arithmetic operator, and another Roman number. Convert each Roman number into its decimal equivalent, perform the indicated arithmetic with the decimal equivalents to get a decimal result. Then convert the decimal result back to a Roman number. The arithmetic operators that your program should recognize in the input are +, -, *, and /. These should perform the C++ standard operations of addition, subtraction, multiplication, and integer division.
Input: text file ROMAN.TXT contains an unknown number of lines (so read until end of file) with each line in this format (with each Roman number terminated with a dollar sign):
MCCXXVI$ + LXVIIII$
L$ + DCV$
MMMCCXI$ – CCCLXVII$
All input numbers and results will be positive values. You can assume that the Roman digits are valid and that a Roman number is correctly formed (no digits out of order, like MCDIL ). All arithmetic operators will be valid.
Your output (either to the screen or to a text file) should show a copy of the input problem, the integer equivalents of the Roman numbers, the integer result, and the Roman result, like this
(space in between output values to enhance readability – don’t squish everything together):
MCCXXVI + LXVIIII = 1226 + 69 = 1295 = MCCLXXXXV
L + DCV = 50 + 605 = 655 = DCLV
MMMCCXI – CCCLXVII = 3211 – 367 = 2844 = MMDCCCXXXXIIII
- You must use at least 4 user-defined functions in addition to main.
- Copy the main function from the back of this page and write the four functions that are used. The function names are shown in bold print, and the comments following them explain what they should do.
- If you can’t get the EOF loop to work, use a for loop instead. There are 10 problems in the file.
using namespace std;
// your 4 function prototypes go here
ifstream inFile ("roman.txt");
cout << "can't open input file" << endl;
else cout << "input file is open" << endl;
while ( inFile.peek() != EOF )
leftOperand = GetRoman (); // read the first Roman number
// The GetRoman function reads a roman character from a file, displays a copy of it, and
// calculates and returns the decimal equivalent of the input roman number. It should
// call another to get the integer equivalent for any single character roman digit:
// thisDigit = RomanToInt (digit); or sum = sum + RomanToInt (digit);
// The same function is called again to read the second roman number, display a copy of it,
// and calculate and return its decimal equivalent.
inFile >> opChar; // read the operator
cout << " " << opChar << " ";
rightOperand = GetRoman (); // read the second Roman number
cout << " = " << leftOperand << ' ' << opChar << ' ' << rightOperand;
inFile.ignore(1, '\n'); // skip over the end-of-line marker
// If you’re using a counted loop, you won’t need this.
result = DoTheMath (leftOperand, opChar, rightOperand);
cout << " = " << result << endl;
// DoTheMath compares the input operator character to see which arithmetic operation is
// called for, does the calculation, and returns the result back to main.
cout << " = " ;
// The ConvertIntToRoman function takes the integer result, converts it back into roman
// numerals, and displays the resulting roman number.
- This solution has not purchased yet.
- Submitted On 25 Apr, 2015 06:34:15