Date: | February 1st 2017 |
---|---|
Deadline: | February 15th 2017 23:59 |
You must implement a stack API and a conversion program that converts between two notational systems for mathematical expressions.
You should implement the stack API described in the stack.h file. This datastructure has not been covered in the lectures yet, but it is easy enough to understand. Note that for this assignment the size of the stack is limited to a fixed number, which is defined in the stack.c file. If you are unfamiliar with stacks, check out the reference links at the end of the assignment.
Your conversion program must be named infix2rpn and must accept a single expression using infix notation on the command-line, and output the following:
The program must terminate with exit code 1 if it encounters an invalid input, and exit code 0 when it succeeds.
You must submit your work as a tarball [1]. Next to the source code, your archive must contain a text file file named “AUTHORS” containing your name and Student ID.
[1] | make tarball will create the tarball for you. |
Input infix expressions are formed using the following rules:
Your program must support proper precedence: 3*1+2 and 3*(1+2) are different!
Output RPN expressions are a space-delimited sequence of operators and non-operators.
On the standard error, the program must print the word "stats" followed by three numbers separated by spaces, in this order: - the total number of stack "push" operations; - the total number of stack "pop" operations; - the maximum size of the stack during the conversion.
Example:
$ ./infix2rpn "3+2" 3 2 + stats 1 1 1 # Exit code is 0 in case of success. $ ./infix2rpn "(3+2)/3"; echo $? 3 2 + 3 / stats 3 3 2 0 # Stats go to stderr, result to stdout. $ ./infix2rpn "(3+2)/3" 2> /dev/null 3 2 + 3 / $ ./infix2rpn "(3+2)/3" > /dev/null stats 3 3 2 # Checking that the exit status is correct in case of error $ ./infix2rpn "blabla" > /dev/null 2>&1; echo $? 1
Hint
You will not need to analyze numbers and determine the value of each number on the input. (Of course, you can do it, but it is not needed to achieve a correct solution. The simple algorithm can look at digits individually and then forget about them.) Check the links referenced at the end of the assignment!
Your grade starts from 0, and the following tests determine your grade:
The following extra features will be tested to obtain higher grades, but only if you have obtained a minimum of 5 points on the list above already:
Summary of desired precedence levels:
Level | Operator |
---|---|
1 | Function application |
2 | Negation |
3 | Exponentiation |
4 | Division and multiplication |
5 | Addition and substraction |