CS 144 Assignment 5. Roman Numerals solution




5/5 - (6 votes)

You will practice creating a class that has
operator overloading and friend functions,
and that uses separate compilation.
For a refresher on Roman numerals, see
Read up to but not including the section
“Alternate forms”.
Class RomanNumeral
Design and implement a C++ class RomanNumeral that reads, writes, and performs
arithmetic operations on Roman numerals. This class must have:
• Private member variables string roman and int decimal that store the
Roman numeral string (such as “MCMLXVIII”) and the corresponding decimal
value (such as 1968) of each RomanNumeral object.
• Private member functions to_roman and to_decimal that convert between the
string and decimal values of a RomanNumeral object and thereby set the values
of member variables roman and decimal.
• Public constructors, one that takes an integer parameter and another that takes a
string parameter. Each constructor must ensure that a RomanNumeral object’s
roman and decimal members are correct.
• Public getter functions that return a RomanNumeral object’s string and decimal
• Overloaded arithmetic operators + – * and / that enable direct arithmetic
operations with Roman numerals. Each operator must return a RomanNumeral
object and the operation must not change the value of either operand. Roman
numerals do integer division (drop the fraction part of a quotient).
• Overloaded equality operators == and != that enable direct comparisons of
RomanNumeral objects.
• Overloaded I/O stream operators >> and << o Input a Roman numeral value as a string, such as MCMLXVIII o Output a Roman numeral value in the form [decimal:string] such as [1968:MCMLXVIII] You may assume for this assignment that the values of the Roman numerals range from 1 through 3999. (Did the ancient Romans have a zero or negative numbers?) Tip: Do the operations using normal integer arithmetic on the decimal values and then convert to Roman strings. Separate compilation In CodeCheck, you will complete header file RomanNumeral.h and implementation file RomanNumeral.cpp. Reference and const Use reference parameters to minimize argument copying. Use const parameters and const member functions to maximize compile-time checking. Test the class Source file RomanNumeralTests.cpp contains two test functions. Function test1 performs arithmetic and equality tests on RomanNumeral objects. Function test2 inputs the text file RomanNumeral.txt: http://www.cs.sjsu.edu/~mak/CS144/assignments/5/RomanNumeral.txt The file contains simple two-operand arithmetic expressions with Roman numerals. The operator is either + – * or / and is surrounded on each side by one or more blanks. The function should read each expression, perform the operation, and print the expression and its result: You may assume that all the Roman numerals in the input are in upper case, and that there are no input errors. Therefore, for this assignment, you do not need to do input error checking. [1963:MCMLXIII] + [56:LVI] = [2019:MMXIX] [2001:MMI] – [33:XXXIII] = [1968:MCMLXVIII] [53:LIII] * [33:XXXIII] = [1749:MDCCXLIX] [2001:MMI] / [33:XXXIII] = [60:LX] MCMLXIII + LVI MMI – XXXIII LIII * XXXIII MMI / XXXIII 3 Submission into Canvas You can submit as many times as necessary to get satisfactory results, and the number of submissions will not affect your score. When you’re done with your program, click the “Download” link at the very bottom of the Report screen to download the signed zip file of your solution. Submit the signed zip file into Canvas: Assignment #5. Roman Numerals. Rubric Criteria Max points Correct program output (as determined by CodeCheck) • Correct output from Test1: o r1, r2, r3, and r4 o r1 + r2/r3 o r1 == r3 o r1 != r4 o r2 == r4 o r3 != r4 • Correct output from Test2: o + expression o – expression o * expression o / expression 60 • Test1 o 10 o 10 o 5 o 5 o 5 o 5 • Test2 o 5 o 5 o 5 o 5 Good class design • Constructor with string parameter. • Constructor with integer parameter. • Use of reference parameters and const. • Private member function to_roman. • Private member function to_decimal. • Overloaded + operator. • Overloaded – operator. • Overloaded * operator. • Overloaded / operator. • Overloaded == operator. • Overloaded != operator. • Overloaded >> operator.
• Overloaded << operator.
• 5
• 5
• 10
• 10
• 10
• 5
• 5
• 5
• 5
• 5
• 5
• 10
• 10
Good program style
• Function declarations with comments
before the main.
• Good names and internal comments.
• 5
• 5
It is possible …
… to perform arithmetic directly on Roman numeral strings:
Doing it on the decimal values is much easier!
Academic integrity
You may study together and discuss the assignments, but what you turn in must be
your individual work. Assignment submissions will be checked for plagiarism using
Moss (http://theory.stanford.edu/~aiken/moss/). Copying another student’s
program or sharing your program is a violation of academic integrity. Moss is
not fooled by renaming variables, reformatting source code, or re-ordering functions.
Violators of academic integrity will suffer severe sanctions, including academic
probation. Students who are on academic probation are not eligible for work as
instructional assistants in the university or for internships at local companies.