ICS 211 Homework 4

Infix calculator

Infix calculator

In this assignment, you will implement an infix calculator.

This infix calculator accepts input from a file specified as a command-line parameter. It reads each line of the file. For each line, it considers each character of the line, one at a time, taking the appropriate action for each character. A character is one of the following:

Any character other than space, a digit, or +-*/%, is illegal for this calculator, and should lead to your program reporting the problem and exiting.

There are only two operator precedence levels: + and - have low operator precendence, and *, / , and % have high operator precedence.

Your program must exit once the end of the file is reached.

At the end of each line, there should be exactly one number left on the stack, and nothing left in the operator stack. If that is the case, the number should be printed as the result of the operation. Otherwise, an error should be reported and the program should exit.

You should use this interface for your operand stack, as well as this implementation. Include these files when submitting the assignment. If you want to, you may add new methods to the the interface and the implementation, for example some people may want to add a peek() method.

You must also convert the integer stack code to store char instead of int, for the operator stack. Call your code CharStackInterface.java and CharArrayStack.java.html. Again you may add any methods you need (but do not remove any methods).

For some ideas on how to read lines in a file, see ReadFile. You may copy and adapt any code you wish from this file.

You may also test your program on this file. The expected result of the computations are 1, 2, 3, ... 10. You should also test your program on other files that you create.

You are welcome to consult the book (e.g. Section 5.4) in doing this project. However, the book does things in a different way than is required for this project, and you may not (and cannot and are not allowed to) directly use the code in the book. You MAY copy and use any bits and pieces (up to 4 lines of code per snippet) that are useful for your project. You MUST appropriately label (with a comment) any code that you copy or adapt from the book.

The TA is very familiar with the code in the book, and any wholesale copying of code from the book will get no credit for the assignment.

Hints

You can use Character.isSpaceChar to determine if a character is a space.

Likewise, you can use Character.isDigit to determine if a character is a digit, and Character.digit to convert a digit to an int (radix should simply be 10 in your code).

Turning in the Assignment

Email your assignment to the TA following the instructions here.