CS60 Homework 15 ---------------------- Due Fri Nov 21 Stack it up ----------------- The task of this assignment is to design a template stack data structure in C++. Also, we will be utilizing exception handling to manage any unexpected behaviour when using the stack. Note that for simplicity in the face of template coding, you can put all code for this assignment in one file (stack class def, member functions, main program). Stacks ----------------- Stacks are one of the simplest data structures that implement a FILO (first in last out) mecahnism. The stack must minimally implement the following functions: push(type) type pop() int size() void clear() The allowed size of the stack may be decided at stack instance creation time, using a constructor that accepts an integer as input. Internally, stacks contain an array of variables of type 'type' and a pointer to the current top of the stack. Every time the user calls 'push', the top pointer is incremented and the passed in value is added to the stack array. Every time the user calls pop the data at the current top of the array is returned and the top index moves down the stack. size() returns the currnet number of elements in the stack, clear resets the top pointer. Errors ----------------------------- There are a number of errors (exceptions) that can happen during the use of a stack. If the user tries to push a value and the stack is full, an exception should be thrown. If the user tries to pop a value from an empty stack, and exception should be thrown. Program Requirements ----------------------------- - build a template stack class that implements the required member functions/variables - build exception classes for push and pop errors that are thrown out of the push and pop functions respecitvely - build a main program that uses the stack object, for three data types (int, double, char), and generates some exceptions. The flow should be as follows: - initialize int stack - loop and get int input from user, push each input to stack - when push exception occurs, break out of loop - loop until stack is empty, pop each datum and print to screen - when pop exception occurs, break out of loop - repeat for double and char - use the following data as input to the stacks int: 1,2,3,4,5,6,7,8,9 double: 3.2, 2.2, 4.5, 6.5, 7.7 char: r, a, c, e, c, a, r - the char stack is a palindrome, a string which is the same word forward and backwards! Sample Ouput ---------------------- localhost:~/school/CS60/progies/sols/hw15 nurmi$ make clean rm -f *.o *~* hw15 localhost:~/school/CS60/progies/sols/hw15 nurmi$ make g++ -I./ -ansi -pedantic -g -c -o hw15.o hw15.cpp g++ hw15.o -o hw15 localhost:~/school/CS60/progies/sols/hw15 nurmi$ ./hw15 Enter an INT: 1 Enter an INT: 2 Enter an INT: 3 Enter an INT: 4 Enter an INT: 5 Enter an INT: 6 Enter an INT: 7 Enter an INT: 8 Enter an INT: 9 Enter an INT: 0 istack full, continuing 9 8 7 6 5 4 3 2 1 istack empty, continuing Enter an DOUBLE: 3.2 Enter an DOUBLE: 2.2 Enter an DOUBLE: 4.5 Enter an DOUBLE: 6.5 Enter an DOUBLE: 7.7 Enter an DOUBLE: 0.0 dstack full, continuing 7.7 6.5 4.5 2.2 3.2 dstack empty, continuing Enter an CHAR: r Enter an CHAR: a Enter an CHAR: c Enter an CHAR: e Enter an CHAR: c Enter an CHAR: a Enter an CHAR: r Enter an CHAR: f cstack full, continuing r a c e c a r cstack empty, continuing localhost:~/school/CS60/progies/sols/hw15 nurmi$ exit