CSCE 2014 - Homework 4
Due Date - 10/22/2009 at 11:59 PM

1. Problem Statement:

The purpose of this programming project is to implement the "UltraMega 2014 Turbo" calculator. What makes this calculator unique is the fact that it processes postfix expressions using a stack. This calculator has 20 keys. Ten key for digits 0 through 9, and ten keys for the operators below:

   addition "+" 
   subtraction "-" 
   multiplication "*" 
   division "/"
   power "^" 
   absolute value "abs"
   square root "sqrt"
   natural logarithm "log"
   exponential function "exp"
   equals "="

When a user enters an expression of the form: integer1 integer2 operator the calculator will push integer1 and integer 2 onto the stack, then when it reads the operator, it pops two values off the stack, calculates the value of integer1 operator integer2 and pushes the result back on the stack.

This calculator also has several several unary operations of the form: integer1 operator. To calculate the result here, the calculator will push integer1 onto the stack, then when it reads the operator, it pops one value off the stack, calculates the result, and pushes this back on the stack for future use.

When a user enters a sequence of binary and unary operations, the calculator processes these commands in a left-to-right manner until the user enters the equals key "="/ When this happens the calculator will pop off the top value from the stack and print this as the answer to the calculation. For example, your program could print something like this:

   Enter postfix expression: 1 2 3 4 + - * =
   Result = -5

Your program should also include error checking and print error messages when the user enters expressions with the wrong syntax, or when they try operations that are not listed above. For example, your program could print something like this:

   Enter postfix expression: 1 2 3 + - * =
   Error: Not enough data for '*' operation

2. Design:

As noted above, your calculator must read postfix expressions from the user and use a stack to perform the specified operations. Hence your main program's primary task is to read and interpret user input, and call methods of the Stack class to store and retrieve the integers used in the calculation.

Since error checking is also an important part of this project, you need to come up with a list of potential errors, and devise some way to detect these errors in your program. It is not necessary to catch every possible error, but you do need to document which types of errors your program does detect, and which are not handled by your program.

3. Implementation:

There is a sample implementation of a Stack class in lab7 that you are welcome to use as part of your solution. This stack stores and retrieves integers, so for now your calculator should only read and produce integer results.

In order to call the math functions: "abs", "sqrt", "log" and "exp", you will need to include the < cmath > library. There are also a number of commands in the < cstdlib > library such as "atoi" that you may find to be useful for processing the user input. For example, you can convert the string "123" into the integer 123 using the following:

   string str = "123";
   int num = atoi(str.c_str());

As always, an incremental implementation is strongly advised. Start with one or two binary operations and make sure they work before going forward with the more complex operations. Once you have the basic program working, you can add the error checking you designed above into the program.

4. Testing:

Test your program to check that it operates correctly for all of the requirements listed above. Also check for the error handling capabilities of the code. Save your testing output in text files for submission on the program due date.

5. Documentation:

When you have completed your C++ program, write a short report (less than one page long) describing what the objectives were, what you did, and the status of the program. Does it work properly for all test cases? Are there any known problems? Save this report in a separate text file to be submitted electronically.

6. Project Submission:

In this class, we will be using electronic project submission to make sure that all students hand their programming projects and labs on time, and to perform automatic analysis of all programs that are submitted. When you have completed the tasks above go to the class web site to "submit" your documentation, C++ program, and testing files.

The dates on your electronic submission will be used to verify that you met the due date above. All late projects will receive reduced credit (50% off if less than 24 hours late, no credit if more than 24 hours late), so hand in your best effort on the due date.

You should also PRINT a copy of these files and hand them into your teaching assistant in your next lab. Include a title page which has your name and uaid, and attach your hand written design notes from above.