Assignment 1: Reverse-Polish Notation

Date: February 1st 2017
Deadline:February 15th 2017 23:59

Objectives

You must implement a stack API and a conversion program that converts between two notational systems for mathematical expressions.

Requirements

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.

Details on the input and output formats

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

Getting started

  1. Unpack the provided source code archive; then run make.
  2. Try out the generated infix2rpn and familiarize yourself with its interface.
  3. Read the file stack.h and understand the interface.
  4. Implement the data structure in stack.c.
  5. Write a bunch of valid and invalid expressions to serve as test input for your program.
  6. Implement the conversion algorithm in infix2rpn.c. Use your tests to check your work.

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!

Grading

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

See also