Programming Assignment 2

Computer Science 102

Please submit your programs to our e-tutor

Introduction
Your project is to design a program to implement a calculator. The calculator will take an infix expression convert it to a postfix expression and then evaluate it.

You should implement the calculator in two parts:

The textbook contains explanations of the algorithms you will need. See pages 84 – 86 for the algorithm you should use for the Calculator class and pages 86 - 89 for the Converter algorithm (Another explanation of the two algorithms can be found here). You can also use the ideas from the PostCalc class we looked at in lecture for your Calculator class. However, it will need to be modified to work for this assignment.

Both the Converter and Calculator classes, should use the StackLI class for the Stack.

The Calculator class

Basically this class will be the same as the Postcalc class shown in lecture. The main differences will be:

The Converter class

The converter class will use a stack in order to implement the algorithm in the book.

Sample input

Your output should show the converted postfix string and the result of the calculation. For instance, here is a sample run of the program (user input in italics):

type your infix expression
(4+8)*(6-5)/((3-2)*(2+2))

converted to postfix: 4 8 + 6 5 - * 3 2 - 2 2 + * /

answer is 3.0