Data Structures, Algorithms, and Generic Programming

Teaching assistants

Zhiqian Hu (zhiqiahu@cs.fsu.edu) Nobuyasu Fukuhara (nff0150@garnet.acns.fsu.edu)

Office: CS Majors Lab Office: CS Majors Lab

Office hours: T 3:00pm 5:00pm Office hours: W 1:00pm 3:00pm

** **

**Educational
Objectives**: Apply the STL stack data structure and the queue
data structure to the fundamental concept of computing. Become familiarized with the postfix
notation as the prelude for the tree
data structure.

**Assignment: **Write a program that takes a human
readable mathematical equation, converts it into a postfix equation, and
evaluates the postfix equation.

**Deliverables:** Turn in makefile,
and all header and cpp files, using project4submit.sh
(due 10/31). At each lecture, you need to turn in a
hardcopy of your cumulative
development log. For each day that you
worked, please list the features you implemented, bugs you fixed, and the help
you obtained from others.

**Requirements:**

1. Create a subdirectory called proj4. Make sure that your code distribution directories are up to date by invoking your update command.

2. Your makefile, header files, and source files should be placed in the proj4 directory.

3.
You **must** use the **Standard Template Library's**
queue and stack classes for this assignment.

4. Your program should handle digits, (, ), and the following operators: +, -, *, /.

5. Whitespaces should be ignored.

6. Remember that ( and ) should have the highest precedence. For example, (1 + 2) * 3 should return 1 2 + 3 * as the postfix notation and 9 as the answer.

7. Remember that * and / should be evaluated before + and -. For example, 1 + 2 * 3 should return 1 2 3 * + as the postfix notation and 7 as the answer.

8. You dont need to handle broken or invalid equations (e.g., missing close parentheses).

9. Bonus (5pts): handle negative numbers. For example, 1 + (-2) should return 1 -2 + as the postfix notation and -1 as the answer. For -2 * 3, you program should return -2 3 * as the postfix notation and -6 as the answer.

10. The name of the executable is polish.

**Sample Session:**

Unix prompt> polish

Enter the human readable equation: (1 + 2) * 3

Postfix notation: 1 2 + 3 *

Answer: 9

Enter the human readable equation: 1 + 2 * 3

Postfix notation: 1 2 3 * +

Answer: 7

Enter the human readable equation: (4 + 2 * 5) / (1 + 3 * 2)

Postfix notation: 4 2 5 * + 1 3 2 * + /

Answer: 2

Enter the human readable equation: (2 * 5 + 4) / (3 * 2 + 1)

Postfix notation: 2 5 * 4 + 3 2 * 1 + /

Answer: 2

**Bonus (5pts):**

Write a program, prefix, that randomly generates valid prefix equations. Write a second program, human, that converts a prefix equation into a human readable equation.