Reverse polish notation in C
CSCI 281 LAB 5 - STACKS AND A
CALCULATOR
ASSIG NM ENT
Using 2 stacks to implement a calculator.
C A L C U L A T O R
1. User input of two types. Integer values and associated tokens (*, /, -, +)
2. As the user needs to enter the input in the following manner:
3 + 5 / 17 – 3 * 9 + 2 * 8 + 7
3. You should use a looping scanf to allow the user to enter digit, then a char, then a digit,
then a car etc until they press the enter key.
4. Each integer (evens) will be added to the integer stack.
5. Each token (odds) will be added to the character stack.
(Hint: counter%2 = 0 if even, counter%2 = 1 if odd.)
Example of full stacks:
6. After the stacks are full or the user is done entering numbers and tokens, complete the
following:
a. You will read the first number off the stack, then the first char, then the second
number.
b. You will need to use a switch statement to determine which token you have, and
then complete the operation. In the example above, you have a +, so you will set a
double variable called calculation equal to 7 + 8.
c. Next you will remove the next number from the number stack and the next token
from the char stack. You will complete the next operation ( * in this case) by
setting calculation equal to calculation * 2.
d. And so on for the rest of the stacks contents.
e. 3 + 5 / 17 – 3 * 9 + 2 * 8 + 7 will result in a value of -17.
f. See the following walkthrough:
Calculation = 8 + 7 = 15.000000
Calculation = 15 * 2 = 30.000000
Calculation = 30 + 9 = 39.000000
Calculation = 39 * 3 = 117.000000
Calculation = 17 - 117 = -100.000000
Calculation = -100 / 5 = -20.000000
Calculation = -20 + 3 = -17.000000
E R R O R C H E C K I N G
1. You only have 4 tokens, so make sure the user does not enter the wrong token. Tell them
to re-enter the entire thing again if they enter either an incorrect token, or they enter
2. If the user is done entering numbers and tokens and total number of numbers - total
number of tokens is not equal to 1, then their input is incorrect.
a. You need one more number than token for this to work.
3. Make sure the user is alternating numbers and tokens. If they try to enter 7 8 or * - they
are incorrect and you need to have them re-enter their information.
S E T U P S TE P S :
1. Create a new project. Do not name your main file main.c. Be descriptive and use your
last name. No spaces. Use camel case or underscores.
2. Delete all the code in the new main file.
3. Copy the stack code from your Stack project we made in class on Friday 3/22 into the
file.
4. Run it to make sure it works. See slides for expected output.
5. Remove the code in the main except the return exit success.
6. Change #define MAXSIZE 8 to two macros instead.
7. You need 2 top pointers. topInt and topChar too keep track of the top of each stack
since they are not the same.
8. Modify your functions as follows:
a. isEmpty, isFull, size – should not pass a struct, should pass an integer flag value.
The flag will be 0 if you are checking the integer stack and 1 if you are checking the
char stack.
b. Peek, push, pop – should make two of each of these functions. One for integers
and one for character tokens. The integer versions of the functions should deal with
integer values only. The char versions of the functions should deal with characters
only.
c. PrintStack – Modify the entire function to only print the values in the two stacks,
DO NOT REMOVE THE STACK ITEMS!! You will need a loop to iterate over
the arrays and print their values. MAKE SURE YOU INDICATE WHICH STACK
YOU ARE PRINTING IN THE OUTPUT.
9. Add code to produce the calculated values as shown above using the steps in the
calculator section. The user can enter any of the 4 tokens and any integer value. When
they are done with input they press enter.
10. Include all perinate error messages.
11. Output calculation at each step and then show the final result.
GRAD ING RUBRI C
Assignment Grading
Functional code – no errors present. If code does not run, then no points will be awarded. The grade of 0 will be given.
Cheating – If your code is identical to another students, both students will receive a grade of 0 for the assignment.
Grade Item Percent of Grade
Looping Input – let user choose one of two options, must use a switch statement.
10pts
Changes made to functions as specified in the instructions. 10pts
Error checking present as specified. 20pts
Calculation result reached using the above steps correctly. Correct calculator result.
50pts
Correct format for user input and code output 10pts
Deductions Points Deducted
Naming your main c file main.c. It has to be descriptive. -10pts
Not using a switch statement. -10pts
Not modifying the functions as specified. -20pts for each function
Incorrect int/char usage -5pts for each infraction
Using global variables for anything except top and the structure. -30pts per variable
Passing all addresses/variables into every function even if they are not needed in the function.
-30pts per function
1 character variable names, even in loops -5pts per variable
Uneven tabbing -31pts
Unreadable code (either through tabbing, spacing, variables names etc) -31pts
No/few comments/copied comments -31pts