This homework is intended to verify your familiarity with stacks and give you more practice with C’s I/O functions. The assignment is to implement a Reverse Polish Notation (RPN) calculator. This is also known as postfix notation, as the operators follow their operands. As a programming assignment, this is a fairly standard one. The only complication beyond the norm lies in using C’s functions to handle inputs.
Calculations in RPN are simple:
All numbers will be single digit values, so you can read them with
fgetc (see lecture 7). Do not forget that numbers will be
in ascii character values. There is also an ascii chart in lecture
7.
Some examples:
Operators always work on the top two operands on the stack.
Walking through one final example, step by step:
Parsing of equations in this notation is simplified and parenthesis are not necessary. For many years this was a preferred style for calculations.
Coding
A makefile and a test script are provided, along with a version of
the stack from class that works on integers. You will provide a file
named hw03.c. Your program should accept the name of a
file. The file will have a single line that contains an equation in RPN
notation. Your parsing code should ignore any tokens that are not
operators or numbers.
Compile your code with make hw03. Test your code with
make test. You can also manually test with the provided
test .txt files.
Return Values
If no errors occur, your main function should return the result of the RPN calculations.
There are 4 possible errors to handle:
We will check for memory leaks with valgrind as well.
Testing
Download either the hw3_files.zip or hw3_files.tar (both have the same contents and are provided in case one format is unfamiliar).
When you extract the archive, you should see a makefile, a testing script named hw3_tests.sh, and several .txt files with test inputs.
Run make test to compile and test your program.
Submission
Submit your hw03.c file through Canvas.