Due: Thursday, June 2, at 11 pm
The main goal of this assignment is to learn how to use a C++ class to create a new data abstraction (type). In the process we will explore some of the underlying similarities and differences in how classes and objects are used in C++ compared to other languages like Java.
In this assignment you will supply the implementation of a C++ type
for Rational numbers. You should do this assignment alone. We will
supply you with a small calculator program that evaluates arithmetic
expressions using your code. You can use this to experiment with and
test your implementation. But your only job is to provide an
implementation of type Rational
.
Rational
is specified in file rational.h
;
there is a corresponding implementation file rational.cc
in the
starter code that is empty. Your job is to supply an implementation of
Rational
in file rational.cc
.
You should download the archive rational.zip
(right-click the link), which contains the starter code. File
rational.h
in the starter code contains specifications of the
operations of the type; to save space these are not repeated here. The
archive also contains the source code for a small calculator program
that uses Rational
s and a simple Makefile
to
build it.
Your implementation of Rationals
should return values in
lowest (i.e., factored) terms. In other words, the greatest common
divisor of the numerator and denominator of a Rational
should be 1, as far as can be detected by a program using type
Rational
. Furthermore, as far as an observer can tell, the
denominator of a Rational
should always be non-negative;
the numerator may be positive or negative as appropriate. This does not
constrain how Rationals
are actually represented in the
implementation, just how they can be observed to behave.
You should implement type Rational
as specified in
rational.h
. You may not modify the contents of this header
file or add or delete anything from it. You are free, of course, to
include additional helper functions and data in your
rational.cc
implementation file if these are useful.
You do not need to check for or deal with rationals that have a denominator
of 0 or similar issues. If a client of type Rational
creates a
Rational
with a denominator of 0, or if a calculation produces
such a result, simply create that result and return it.
Similarly, you do not need to handle the possibility of integer
overflow in calculations.
However, the code in your implementation, including algorithms that compute
common factors when reducing fractions to lowest terms, should
not crash or loop indefinitely even if some numerators or denominators
of fractions are negative or zero.
The calculator program (supplied) is a simple line-oriented calculator for expressions involving rational numbers. Rationals can be entered as either a positive integer n, meaning n/1, or as a pair of positive integers separated by a slash: n/d. Numbers can be combined in expressions in the usual way with the operators +, -, *, and % . Operator % is used for division instead of / because / is used to enter a single rational value. Spaces may separate parts of expressions, and subexpressions can be surrounded by parentheses. One limitation is that the calculator only implements binary operators; in particular it doesn't support unary minus. To compute -r, you can evaluate 0-r. All expressions must be entered on a single line, and the calculator evaluates the expression on each input line and prints the result. The end of input is indicated by typing the usual end-of-file character (ctrl-D in Linux shell windows). The calculator makes no effort to detect or report errors such as illegal input, additional characters on the input line following a complete expression, or rationals with a denominator of 0. If it is unable to make sense out of something, it usually generates the result 0/0 and goes on to process the next input line.
Remember that your job in this assignment is only to implement type
Rational
. The calculator program is intended as a way of
testing and demonstrating that your Rational
type works
properly, but you are not responsible for any of its quirks or for
fixing any odd behavior that you may discover.
Your solutions should be:
cancun
or the current CSE Linux virtual
machine).valgrind
to help check for possible problems, but there shouldn't be any
because the Rational
class probably does not need
any dynamic memory allocation in its implementation.You should turn in only the file rational.cc
containing your
implementation of type Rational
. Be sure to include your name
at the top of the file. Use gradescope to submit the file.
Gradescope will allow you to turn in your homework up to two days late, which you can use if you have any individual late days remaining (max 2 for this assignment, as for all other assignments this quarter).