Homework 7, A Fractional Calculator
For this program you will implement a postfix notation calculator for rational numbers.
Goals
- Implement several classes.
- Design a test plan for a given class.
- Test a class using a test plan.
- Build an application based on several classes.
- Build a UML diagram for the classes in an application.
Formal Description
Implement a postfix notation calculator which performs calculations on rational numbers. Postfix notation, which is also called Reverse Polish Notation or RPN, is a way to write expressions so no parentheses are required. Evaluating RPN expressions involves a stack.
The expression 3 4 + is equivalent to 3 + 4. This expression would be evaluated parsing left to right as follows:
Start with an empty stack.
Read the number 3
Push it on the stack
Read the number 4
Push it on the stack
Read the operator +
o2 = Pop()
o1 = Pop()
Push (o1+o2)
Wikipedia currently has the example : ((15 / (7 - (1 + 1))) × 3) - (2 + (1 + 1)) which is 15 7 1 1 + - / 3 × 2 1 1 + + - in RPN.
Note that the expression 3 6 / produces 1/2, and 3 6 - produces -3.
You are to implement a RPN calculator which performs calculations on rational numbers.
The algorithm for evaluating an expression in RPN is
for each token in the expression
if the token (op) is an operator (+,-,*,/)
arg2 = Pop()
arg1 = Pop()
result = arg1 op arg2
Push(result)
else if the token is a rational number
Push(token)
else
Print error
Exit algorithm
Prompt
Your program should prompt the user with:
- The number of items on the stack in parenthesis
- The percent sign.
- A space.
Initially the stack is empty so the prompt should be:
(0) %
If the command 3 2 - is executed, the stack will contain a single item so the prompt should be
(1) %
Commands
Your program should execute the following commands:
- off : exit the program.
- print : print the rational number at the top of the stack as a rational number.
- fprint : print the rational number at the top of the stack as a double.
- clear : remove all values from the stack.
- dump : print all values on the stack from bottom to top.
If no command is given, the calculator should assume the input is an expression and attempt to evaluate it. If there are no errors, the calculator should print a blank line and return.
The following is an example session using the calculator
[bennett@mirkwood hw7]$ Calculator
(0) % 3 6 /
(1) % print
1/2
(1) % fprint
0.5
(1) % 3 6 -
(2) % print
-3
(2) % 15 7 1 1 + - / 3 * 2 1 1 + + -
(3) % print
5
(3) % dump
1/2 -3 5
(3) % clear
(0) % off
[bennett@mirkwood hw7]$
Errors
You may encounter the following errors:
- An operation is attempted on a stack with less than two items
- Print the following: Error: Insufficient Stack for Xxxxx."
where Xxxxx is the name of the operation attempted (Add, Subtract, Multiply, Divide).
- Abandon the remaining computation.
- An attempt to add data to a full stack
- Print Error: Stack Overflow.
- Abandon the remaining computation.
- Any time invalid input is received:
- Print Error: Invalid Input xxx.
- xxx is the value that caused the stack error
- Abandon the remaining computation.
In any case, your calculator should continue to operate after an error. It should not exit.
[bennett@mirkwood hw7]$ Calculator
(0) % 3 +
Error: Insufficient Stack for Add.
(1) % 1 2 3 4 5 6 7 8 9 10
Error: Stack Overflow.
(10) % bob
Error: Invalid Input bob.
(10) % 1a/b
Error: Invalid Input 1a/b.
(10) %
Required Classes
You must implement the following classes:
- RationalT a rational number class
- Place this definition/implementation in rational.[h,C/cpp]
- Domain
- Rational numbers (ie fractions)
- Please observe the following restrictions
- Zero is the default and is represented as 0/1
- If the rational number is negative, the sign is kept with the numerator.
- -(3/2) has numerator -3 and denominator 2.
- The rational number is always kept as a reduced fraction
- Operations
- Constructors:
- The default constructor creates 0/1
- A single parameter constructor taking an integer n and creating the rational number n/1
- A two parameter constructor taking the integers n and d and creating the equivalent rational number n/d .
- If d is zero, the default value is created.
- RationalT Add(const RationalT & other) const
- Return this number + other.
- RationalT Sub(const RationalT & other) const
- Return this number - other.
- RationalT Mul(const RationalT & other) const
- Return this number * other.
- RationalT Div(const RationalT & other) const
- Return this number / other.
- int Num(void) const
- Return the numerator of the number.
- int Denom(void) const
- Return the denominator of the number.
- double Value(void) const
- Return the double equivalent of the number.
- StackT a stack class
- Place this definition/implementation in stack.[h,C/cpp]
- The domain consists of
- 0 to STACK_MAX (10) ItemT
- Please provide the following functions. You must implement these functions as given, you may not add any functions to this list.
- Constructors
- void Push(ItemT i);
- Add an item to the stack if possible.
- If the stack is full, silently fail.
- ItemT Pop(void);
- Remove the top item from the stack and remove it.
- If the stack is empty silently fail and return a default ItemT.
- ItemT Top(void) const;
- Return the top item on the stack.
- DO NOT remove the top item.
- If the stack is empty silently fail and return a default ItemT.
- int Size(void) const;
- Return the size of the stack.
- bool IsEmpty(void) const;
- Return true if the stack is empty, false otherwise.
- bool IsFull(void) const;
- Return true if the stack is full, false otherwise.
- You must consider creating a program which tests this type.
When implementing the rational number class, the Euclidean Algorithm might be useful. This algorithm will compute the greatest common divisor (GCD) of any two integers very efficiently. The proof of this algorithm is given in Discrete Math, but you can use it right now
GCD (a,b)
if a < b
return GCD(b,a)
if b is 0
return a
else
return GCD(b, a%b)
Testing
Discussion
Required Files
A tar file containing
- All source code required to build all programs in this project.
- A Makefile capable of building the programs required for this project. It should not contain executable or object files.
- A test plan using the Test Plan Document described here. Please call this document TestPlan.docx
- A UML diagram detailing the classes used in this project and their relationship. Please create this using UMLet or UMLetino Please call this Design.uml.
Submission
Send the tar file to your instructor as an attachment to an email message by the due date.