Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. The C++ Stack Class The sample code has a C++ program that contains a Stack s

ID: 640115 • Letter: 1

Question

1. The C++ Stack Class

The sample code has a C++ program that contains a Stack structure (called CPPStack) that holds items of ItemType, currently defined to be chars, and a program to test the stack. Convert this Stack structure into a Stack class (called Stack) and convert the program into a program that uses your class. When converting the structure to a class, consider what is the public interface for the stack and its private interface that supports encapsulation. Note that the stack passed to the general (or global) functions becomes the object making the call to the class member functions. Use a const int for SIZE and change IsEmpty() and IsFull() to return bool (not int). Add one more member function to your C++ Stack class called Clear(), which will clear all the contents off the stack so that calls to IsEmpty() after a call to Clear() will return true. Test your stack using your converted sample program.

// SAMPLE PROGRAM //

//
// C++ Struct Program: Character Stack Example using a struct
//
#include <iostream>
using namespace std;
const int SIZE = 8;
// Type Definitions
typedef char ItemType; // Current implementation uses a stack of char
struct CPPStack
{
ItemType stack[SIZE]; // holds the stack of items
int top; // index to top of stack and next available position
};
// Define functions to manipulate the stack using a pointer to the stack
void Init(CPPStack *s)
{
s->top = 0;
}
// Test if stack is empty.
int IsEmpty(const CPPStack *s)
{
return (s->top == 0);
}
// Test if stack is full.
int IsFull(const CPPStack *s)
{
return (s->top == SIZE);
}
// Push an item onto the stack
void Push(CPPStack *s, ItemType item)
{
if (IsFull(s))
{
cout << "Stack is full. No change to stack. ";
return;
}
s->stack[s->top] = item;
s->top++;
}
// Pop a character off the stack
ItemType Pop(CPPStack *s)
{
if (IsEmpty(s))
{
cout << "Error in Pop(): Stack is empty. Exiting program. ";
exit(1); /* exit on trying to pop empty stack error */
}
s->top--;
return s->stack[s->top];
}
// Clear the stack
// You need to add the code for this function for your version.

// Sample Program to test the stack operations.
int main()
{
CPPStack myStack; // create a char stack
int i; // loop index
cout << "struct version of the Character Stack Program." << endl;
// Initialize the stack.
Init( &myStack );
// Push some lower case ASCII characters onto the stack.
i = 0;
while (!IsFull( &myStack ))
{
Push( &myStack, static_cast<char>('a'+i) );
i++;
}
// Pop characters off the stack and display. */
cout << "Pop the contents of My Stack:" << endl;
while (!IsEmpty( &myStack ))
{
cout << Pop( &myStack ) << " ";
}
cout << endl << endl;
return 0;
}

// END SAMPLE PROGRAM //

2. The Position Class

Create a class called Position. A Position object represents a row and a column position on a two dimensional 10 x 10 grid. You should be able to set the position (row, column), get the row, get the column, print the position, and compare two positions (return true if the positions match, false otherwise). [Note: no 2D grid is needed, just the Position class where an object represents one position on the grid.]

3. Test the Position Class with the Stack Class

Modify your Stack from problem #1, so that you can push and pop Position objects onto and off your stack. If you used the typedef for ItemType in your implementation, you can simply switch from char to Position. Make sure you include your Position header file above the typedef statement. #include "Position.h" typedef Position ItemType; Create a short program to test your Position Stack. Declare two Position objects and one pointer to a Position object in main(). For your third object allocate memory for the Position object in main(). Ask the user to give you six pairs of numbers (rows and columns) that will represent three Positions on a 10 x 10 grid (row, column). Assume that for the user the rows and columns are numbered from 1 to 10. Input validation is needed when the user enters the data (before you set the position). Then set the values for the three different Positions and push each Position object onto the Stack (note that a copy of each object is placed on the Stack by the push function). Then, while the stack is not empty, pop the Position objects off the stack, print the position and report if this position matches the second Position.

Explanation / Answer

OUTPUTS
1.push 2.pop 3.display 4.exit
Enter ur choice2
stack under flow

1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element2
inserted2

1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element3
inserted3

1.push 2.pop 3.display 4.exit
Enter ur choice2
deleted3

1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element5
inserted5

1.push 2.pop 3.display 4.exit
Enter ur choice3
5 2