CSE 303, Spring 2008, Assignment 6

Something Rational for a Change

Due Wednesday, June 4 at 9 pm

Synopsis

In this assignment you will supply the implementation of a C++ type for Rational numbers and use it to build a calculator for rationals. You should do this assignment alone. We will supply you with the calculator code, including a header file that specifies type Rational. Your only job is to provide the implementation of Rational.

Assignment Goals

The main purpose of this assignment is to give you a taste of C++ by implementing a new C++ type. In the process you will also work out the mechanics of compiling and running a C++ program, which is basically the same as running one written in C.

The Calculator

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 using the operators +, -, *, and % (for divide); spaces may separate parts of expressions; and subexpressions can be surrounded by parentheses. 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 on attu). The calculator makes no effort to detect or report errors such as illegal input or rationals with a denominator of 0. If it is unable to make sense out of something, it usually returns the result 0/0 and goes on to the next input line.

Type Rational

The calculator is implemented as a set of C++ functions. Calculations are done with a user-defined type Rational that defines the representation of rational numbers, constructors for them, and basic arithmetic operations. This type is specified in a file rational.h; there is a corresponding implementation file rational.cpp that is empty. Your job is to provide an implementation of Rational in file rational.cpp.

You should download file rational.tar (right-click the link), which contains the starter code. File rational.h in the starter code contains descriptions of the operations of the type; these are not repeated here to save space. In addition to the source code for the calculator, the archive contains 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 program using type Rational. This does not constrain how Rationals are actually represented, just how they can be observed to behave.

One note about how Rational is used in the calculator code. A Rational is a very simple object with a numerator and denominator, some constructors, and some operations. The calculator creates instances of Rationals as ordinary variables and parameters. They are not allocated on the heap with new, or deleted manually later.

What to Turn In

You should create a tar archive containing the source files (including the Makefile) needed to build your completed calculator, including your Rational implementation Turn that in using the regular dropbox.