Created a Infix Calculator

This is an infix calculator that uses ArrayStacks to keep track of tokens (ArrayStack and StackADT not included)



Computer Science II
CS102-Ames Fall 2013
HW3—Infix Expression Evaluation
20 points
Assignment: Write a GUI program that evaluates an infix expression.  This is primarily an 
exercise using Stacks.
Due: Friday 2/1, 5PM
Turn In: Submit a copy of your Java sources to BBV.  
First, get a copy of the StackADT.java and ArrayStack.java files from the HW3 area of 
BBV.  Finish them by completing the peek(), isEmpty(), and size() methods.  Continue 
using the generic type—don’t make the stack specific to String’s or Double’s or any other 
particular type.
Then, write a GUI program (third .java file) to evaluate infix expressions.  We’ll use an 
algorithm by Dave Matuszek of U. Penn, it’s on the back of this page.  Use double’s for all of 
your values.  I’ll show you how to use the StringTokenizer in class.
Accept the operators + - * / ^ where ^ raises a number to a power.
By adding an ActionListener to the expression
JTextField, your actionPerformed() method will 
be called whenever the user alters the infix field and 
presses the Enter key.
A Few Test Cases:
(feel free to try others)
Input Expected Result
(2+3)*(4+5) 45.0
3.0*1.7 5.1
10/3 3.333333333333333
(3+222) 225.0
(1+2)^3*2 54.0
Suggestions:
Use the debugger !!! It’s essential for debugging.  Put a breakpoint at the top of your method 
that executes the algorithm on the next page.  Step one line at a time, checking your variables 
to make sure it’s working as expected.
Error Checking:
When writing your code, there will be times when you’ll notice error conditions.  When they 
arise, throw a new ArithmeticException(“message”) with an appropriate message.  In your 
ActionPerformed() method, use try/catch to see if any exceptions occur in your expression 
evaluator.  If they do, put the exception message into the result field of the frame.
You don’t have to catch every possible error, but let’s handle reasonably those that you do 
detect.The Algorithm
You need two stacks, a value stack (to hold the numbers), and an operator stack (to hold the 
operators). Numbers will be double values, operators will be String values.
The algorithm is as follows. Note that no error checking is mentioned explicitly; you should 
throw an ArithmeticException if you notice something go wrong.
1. Take the original infix expression, and add parentheses around it.  Then create a 
StringTokenizer to read tokens from that parenthesized expression.
2. While there are still tokens to be read in,
1. Get the next token.
2. If the token is:
1. A number: push it onto the value stack.
2. A left parenthesis: push it onto the operator stack.
3. A right parenthesis:
1. While the thing on top of the operator stack (use peek()) is not a 
left parenthesis,
1. Pop the operator from the operator stack.
2. Pop the value stack twice, getting two operands.
3. Apply the operator to the operands, in the correct order.
4. Push the result onto the value stack.
2. Pop the left parenthesis from the operator stack, and discard it.
4. An operator:
1. While the operator stack is not empty, and the top thing on the 
operator stack has the same or greater precedence as the token,
1. Pop the operator from the operator stack.
2. Pop the value stack twice, getting two operands.
3. Apply the operator to the operands, in the correct order.
4. Push the result onto the value stack.
2. Push the token onto the operator stack.
3. At this point the operator stack should be empty, and the value stack should have only 
one value in it, which is the final result.
Notes:
There are some obvious opportunities for helper methods here. For example, you should 
write a method that, given an operator, returns its precedence.
Use a StringTokenizer to break the input expression up into operators + - * / ^ ( ) and 
operands (numbers and variables). StringTokenizer is defined in standard library; it has a good
explanation at the top of the API description, and it's really useful for this kind of job. Hint: 
you want the three-argument constructor, since you want to use the operators as delimiters.