CSE 374 23WI Homework 6

Due: Fri, Mar 10, 2023 at 11:59 pm

Important!

I will not be available Friday March 10 - Sunday March 12. Office hours on Friday March 10 is the last time that there will definitely be TA support, so please plan accordingly. The TAs may check in on edstem over the weekend, but there is no guarantee, and you should assume that there will be no support for this assignment after office hours March 10, so I strongly recommend doing it early Even if office hours are cancelled on Friday, or are too busy for the TA to answer your questions, there will be no extensions due to lack of staff support. This assignment is designed to be very manageable if you start early - time management is your responsibility, and the only extensions will be due to outside-of-class factors in your personal life, such as sickness.



Synopsis

In this assignment you will supply the implementation of a C++ type for fractional 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, as it is specified in Rational.h, and to complete a Makefile to build the system.

To get started download the file rational.zip . You will need to get these files to cancun whichever way you wish. The header file Rational.h contains the specification of the Rational class. The archive also contains the source code for a small calculator program that uses Rationals and a simple Makefile to build it. You will need to fill in some details of the Makefile in order to use it.

Type Rational

Rational is specified in file Rational.h. You will need to create a file called Rational.cpp, and implement the class as it is specifed in the header file.

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 coded, just how they can be observed to behave to someone calling the functions in the class.

You should implement type Rational as defined in Rational.h. You may not modify the contents of this header file or add or delete anything from it. (You should not modify any of the other source code files, either.) You are free, of course, to include additional helper functions and data in your Rational.cpp 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.

You should use good style in your code, just like in our C programs, including using to check for issues.

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 with the operators +, -, *, and % (for divide - the / isn't used to indicate division of two rationals since it 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 to compute -r, you must evaluate 0-r : you cannot type -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 (or 0/1) and goes on to process the next input line. Example input, showing the results of bad parsing on the last line:

  $ 3/4-5/13
  19/52
  $ 4/0
  1/0
  $ gh
  0/1
  $ 7*(0-1)/3
  -7/1
  

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.

Hints

The Makefile contains comments prompting you to fill in some portions. You will create a target to clean the directory, and also fill in the object file targets. If you want, you can use the g++ -MM flag to help you here. Do this part first so you can use your Makefile during the rest of the assignment.

One step budding fraction experts learn is to always reduce their fractions. You should make sure that your answers are always reported in their reduced format. A tip is to ensure that your parameterized constructor results in a fraction that has been entirely reduced. When this is done intermediate arithmetic calculations do not need to be reduced becuase the final fraction is reduced upon creation.

You can complete this assignment entirely on the stack; no dynamic memory needed.

What to Turn In

You should turn in only the files Rational.cpp containing your implementation of type Rational, and your modified Makefile, both via gradescope. There will be no manual grading.