Question
Please write stack.h and stack.cpp in c++ and test with main.cpp 1. display the stack 2. push 1,2,3,4 3. display the top element 4. pop
Please write stack.h and stack.cpp in c++ and test with main.cpp
1. display the stack 2. push 1,2,3,4 3. display the top element 4. pop 3. display the top element 5. display the stack 6. clear the stack 7. display the stack
stack.h
//INSTRUCTION: //You must complete the ** parts and then complete stack.cpp //EMACS HINT: // control-S does searches
// ======================================================= // Your name: ** // Compiler: putty g++ // File type: headher file stack.h //=======================================================
//----- Globally setting up the aliases ----------------
const int MAX = 10; // The MAX number of elements for the stack // MAX is unknown to the client
typedef ** el_t; // the el_t type is ** for now // el_t is unknown to the client //-------------------------------------------------------
class stack {
private: // to be hidden from the client
el_t el[MAX]; // el is an array of el_t's int top; // top is index to the top of stack
public: // available to the client
// Add exception handling classes here ** // prototypes to be used by the client // Note that no parameter variables are given
stack(); // constructor to create an object ~stack(); // destructor to destroy an object
// PURPOSE: if not full, enters an element at the top; // otherwise throws an exception - Overflow // PARAMETER: pass the element to be pushed void push(el_t);
// PURPOSE: if not empty, removes and gives back the top element; // otherwise throws an exception - Underflow // PARAMETER: provide variable to receive the popped element (by ref) void pop(el_t&);
// PURPOSE: if not empty, gives the top element without removing it; // otherwise, throws an exception - Underflow // PARAMETER: returns the last element added to the stack void topElem(el_t&);
// ** Must add good comments for each function - See Notes1B
//PURPOSE: bool isEmpty();
//PURPOSE: bool isFull();
//PURPOSE: void displayAll();
//PURPOSE: void clearIt();
};
stack.cpp
//INSTRUCTION: // Look for ** to complete the program (do control-S) // Update the comments using the HOW TO COMMENT file, // When done, compile stack.cpp to make sure there are no syntax errors. // NOTE: functions are called instead of repeating // the same lines of code.
//========================================================= //HW#: HW1P1 stack //Your name: //Complier: g++ //File type: stack implementation file stack.cpp //=========================================================
using namespace std; #include
//PURPOSE: Constructor must initialize top to be -1 to begin with. stack::stack() { top = -1; // indicate an empty stack }
//PURPOSE: Destructor does not have to do anything since this is a static array. stack::~stack() { // nothing }
//PURPOSE: checks top and returns true if empty, false otherwise. bool stack::isEmpty() { if ( top == -1) return true; else return false; )
//PURPOSE: checks top and returns true if full, false otherwise. bool stack::isFull() { if (top == MAX-1 ) return true; else return false; }
//PURPOSE: calls isFull and if true, throws an exception (Overflow) // Otherwise, adds an element to el after incrementing top. //PARAMETER: pass the element (elem) to be pushed void stack::push(el_t elem) { if (isFull()) throw runtime_error else { top++; el[top] = elem; } }
//PURPOSE: calls isEmpty and if true, throws an exception (Underflow) // Otherwise, removes an element from el and gives it back. //PARAMETER: provide a holder (elem) for the element popped (pass by ref) void stack::pop(el_t& elem) { if (isEmpty()) ** ; else { elem = el[top]; top--;} }
// PURPOSE: calls isEmpty and if true, throws an exception (underflow) // Otherwise, gives back the top element from el. //PARAMETER: provide a holder (elem) for the element (pass by ref) void stack::topElem(el_t& elem) { if (isEmpty()) ** ; else { ** } }
test with this:
main.cpp
using namespace std;
#include
int main () {
cout << "************** STACK Test Program *************" << endl; cout << "This program tests an integer stack ADT implemented as" << endl; cout << "an array. " << endl; cout << "It is a menu based program allowing you to test each method. " << endl; cout << "It displays the entire stack whenever it is modified." << endl; cout << "***********************************************" << endl;
stack S; // this is the stack object S
int selection; // this will indicate what the user wants to do
do { try { cout << endl << "MENU: These are your options: " << endl << endl; cout << " (1) Push an element " << endl; cout << " (2) Display the entire stack " << endl; cout << " (3) Pop the top element" << endl; cout << " (4) Display the top element" << endl; cout << " (5) Check to see if is it empty" << endl; cout << " (6) Check to see if is it full" << endl; cout << " (7) Clear the entire stack" << endl; cout << " Enter ( 0 ) to quit " << endl; cout << "===>";
cin >> selection;
int toadd; //this is the number the user wants to add to the stack int erased; // this is the number removed from the stack int looked; // this is the number at the top
switch (selection) {
case 1: // the Push choice cout << "Enter a positive integer to be added : ";
cin >> toadd; S.push(toadd); cout << "The updated contents of the stack are: " << endl; S.displayAll(); break;
case 2: // the Display choice cout << "The current contents are: " << endl; S.displayAll(); break;
case 3: // The Pop choice S.pop(erased); cout << erased << " has been removed." << endl; cout << "The updated contents of the stack are: "; S.displayAll(); break;
case 4: // the top element case S.topElem(looked); cout << looked << " is at the top." << endl; break;
case 5: // the check-empty case if (S.isEmpty()) cout << "Stack is empty right now." << endl; else cout << "Stack has some elements." << endl; break;
case 6: // the check-full case if (S.isFull()) cout << "Stack is full right now." << endl; else cout << "Stack has more room." << endl; break;
case 7: // the clear the stack case S.clearIt(); S.displayAll(); break;
} // end of switch statement
}// end of try - Now catch exceptions catch(stack::Overflow) {cerr << "Error: You have caused the stack to overflow." << endl;} catch(stack::Underflow) {cerr << "Error: You have caused the stack to underflow." << endl;}
} while(selection!=0);
cout << "Bye bye - ending the stack test program" << endl;
}// end of main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started