Rational Class and Expression Evaluator
For this project, you will define a class for rational
numbers and implement a simple expression
evaluator for Rational expressions.
Define a class for rational numbers and call it Rational.
A rational number is a number that
can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2,
and so forth
are all rational numbers. (By 1/2 we mean a fraction.)
You should represent rational numbers as two values of
type int, one for the numerator and
one for the denominator.
Define constructors that allow the class to be
instantiated with two arguments, one being the
numerator and the other the denominator. Also define a constructor that has a
single
integer argument, wholeNumber, and initializes the class to wholeNumber/1.
Finally provide
a default constructor that initializes the class to 0/1.
Overload the input operator to read in rational numbers
from a stream. Note that the
numerator and/or the denominator could be negatives, so the input operator
should recognize
any of the following: 3/4, -3/4, 3/-4 and -3/-4.
Overload the output operator to produce output of a
Rational number.
Overload the following relational operators: !=, ==, <,
<=, >, >=. Note that:
•two rational numbers a/b and c/d are equal if a*d equals c*b.
•if b and d are positive rational numbers, then a/b is less than c/d provided
a*d is less
than c*b (Of course, your implementation should produce correct results in all
cases.)
Overload the following arithmetic operators: +, -, *, / as
member functions.
Define a private routine that allows a Rational number to
be normalized. After
normalization, the denominator is always positive and the numerator and
denominator are as
small as possible. For example, 4/-8 would be normalized as -1/2. Use this
routine every
time a value changes in a Rational number, that is call it from the
constructors, and from the
input parameter.
Controller
Define a controller that will allow you to enter
arithmetic expressions of rational numbers. The
expressions can contain any operator, arithmetic or relational. Your program
should read these
expressions from cin and print as an output the evaluation of the expression.
The expressions
follow the format:
<rational number> <space+> {<operator> <space+> <rational
number> }
<end of line>
where <operator> is one of +, -, *, /, !=, ==, <, <=, >, >=
This means that you can have an arbitrarily long
expression. There is always at least one
space between the rational numbers and the operators.
There is no order of precedence in this rational number
calculator. Evaluate the expressions
from left to right as they appear on the input. There will not be any
combination of arithmetic
and relational operators in one expression in the input.
The controller should also accept an exit command that
will terminate execution. Also,
reaching the end of file on the input stream should terminate the program.
Define your controller in such a way that it does not know
about the three different ways of
running your program. Your controller should have two aggregate objects that
represent the
input and output combinations. Call the controller in three different ways to
represent the three
different options of execution discussed below. The controller itself can be a
class or it can be
a function with the two aggregate objects being passed as parameters. The
objects
representing the input and output options are probably instances of a class you
create.
Program Execution
Assume that your program is called program2.exe. Your
program must accept some command
line arguments when it executes. However, the command arguments are optional.
Here are
the different combinations and what they mean.
% program2.exe
This version will run your program and read data from standard input (cin) and
produce
output to standard output (cout). In this condition, your program should print a
prompt to
let the user know it is waiting for input. The prompt can be anything you want,
but make
is a short string, much like the DOS or Unix prompt work.
% program2.exe <input-file>
This version will run your program and read data from the file provided in the
command
line and produce output to standard output (cout). In this condition, your
program should
produce to the standard output enough information to make it clear what the
system
was evaluating. Note that the input is coming from a file, so just printing the
results is
not enough. Printing something like this is the minimum you should produce:
expression: 1/2 + 3/4 = 5/4
% program2.exe <input-file> <log-file>
This version will run your program and read data from the file provided in the
command
line and produce output to the log file.
Testing
Your program should run as described above. This allows
you to test your program in many
different ways, from the terminal interactively or using test files prepared
ahead of time.
If your program is being run interactively, it should
produce an output somewhat like this:
Welcome to Rational calculator.
% 1/2 + 3/4
= 5/4
% 3/2 + 1/2
= 2/1
% 3/2 * 1/2 + 1/4 - 1/2
= 1/2
% -4/3 < 5/6
= true
% exit
Bye
Test Files
It is your responsibility to test your program for all
possible cases permitted by the description
here provided. If you program runs with your data but crashes with our data
during the demo of
the program, then it just means that you did not test your program thoroughly
and you will lose
points (lots of them).
Feel free to create interesting data files and share them
with others in class (THE DATA FILES
ONLY, do not share the output of your program nor your code nor your design).
The more
data files you create and use in your testing, the more certain you are that
your program works
correctly.
Programming Standards
The GTAs will be evaluating your source code on this
assignment for programming style, so
you should observe good practice (as usual). See the Programming Standards page
on the
course website for specific requirements that should be observed in this course.
Evaluation
Your program will be evaluated in a demo with a TA. You
will have to signup for a demo time
with a TA and demonstrate your program. The TA will have his/her own data files
for testing
and you should provide some of your own (see submitting your program below).
Your grade on the project is based on the following
distribution:
Design of the Rational Class |
25% |
Design of the input/output routines |
10% |
Design of the controller |
5% |
Internal documentation
and code organization |
15% |
Correctness of execution and
submission of test files |
35% |
Note that the evaluation of your project will depend
substantially on the quality of your code
and documentation.
Submitting your program
You must submit this program to the Curator System (read
the Student Guide), but it will not
be graded automatically. Instructions for submitting are contained in the
Student Guide.
You will be allowed up to five submissions for this
assignment. However, because the
assignment is not graded automatically there should be no need to submit more
than once,
unless you forgot to submit a file. Test your program thoroughly before
submitting it. Also,
write your documentation as you go, not after you have completed your work.
You should submit at least the following: a .cpp and .h
file for each class defined in your
project, the main.cpp file, and two test files. Call your test files test1.txt
and
test2.txt. You will lose points if you do not follow the instructions as
specified here.
Late Policy
Your program will be due at midnight on the due date. Late
submissions have a 10% penalty
per day that the project is late. After 5 days late, your project will be
accepted but will receive
a maximum of 50 points. You have to turn in your project, even if you turn it in
late, it is a
course requirement. Not turning it in will result in a zero for all your
programming assignments.
|