Question

//stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what)...

//stack_exception.h

#ifndef STACK_EXCEPTION
#define STACK_EXCEPTION

#include <string>

using namespace std;

class Stack_Exception {
public:
Stack_Exception(string what) : what(what) {}
string getWhat() {return what;}
private:
string what;
};

#endif

//stack_test_app.cpp

#include <iostream>
#include <sstream>
#include <string>

#include "stack.h"

using namespace std;


/*********************************************
* The 'contains' function template goes here *
*********************************************/

int main() {
   cout << boolalpha;

   cout << "--- stack of int" << endl;

   Stack<int> si;
   cout << "si intially " << si << endl;

   cout << "Pushing 0 .. 9 onto stack" << endl;
   for (int i = 0; i < 10; i++) {
       si.push(i);
       cout << "si after push: " << si << endl;
   }

   cout << "si: " << si << endl;

   cout << "A successful contains call followed by an unsuccessful contains" << endl;
   //cout << "si contains 5: " << contains(si, 5) << endl;
   cout << "si after 'successful contains: " << si << endl;
   //cout << "si contains 10: " << contains(si, 10) << endl;
   cout << "si after 'unsuccessful contains: " << si << endl;
   cout << si << endl;

   cout << "Emptying the stack, printing as we go" << endl;
   while (!si.isEmpty()) {
       si.pop();
       cout << "si after pop: " << si << endl;
   }

   cout << endl;
   cout << "--- stack of double" << endl;
   Stack<double> sd;
   cout << "sd intially " << sd << endl;

   cout << "Pushing 10.5 ... 14.5 onto the stack" << endl;
   for (int i = 10; i < 15; i++) {
       sd.push(i+.5);
       cout << "sd after push: " << sd << endl;
   }
   cout << sd << endl;

   cout << "A successful contains call followed by an unsuccessful contains" << endl;
   //cout << "sd contains 10.5: " << contains(sd, 10.5) << endl;
   cout << sd << endl;
   //cout << "sd contains 9.5: " << contains(sd, 9.5) << endl;
   cout << sd << endl;

   cout << "Emptying the stack, printing as we go" << endl;
   while (!sd.isEmpty()) {
       sd.pop();
       cout << "sd after pop: " << sd << endl;
   }


   cout << endl;
   cout << "--- stack of string" << endl;
   Stack <string> ss;
   cout << "ss intially " << ss << endl;

   cout << "Pushing S#100 ... S#115 onto stack (use ostringstream an in example presented in class)" << endl;
   for (int i = 100; i < 107; i++) {
       ostringstream oss;
       oss << "S#" << i;
       ss.push(oss.str());
       cout << "ss after push: " << ss << endl;
   }

   cout << ss << endl;

   cout << "A successful contains call followed by an unsuccessful contains" << endl;
   //cout << "ss contains S#105: " << contains(ss, string("S#105")) << endl;
   cout << ss << endl;
   //cout << "ss contains S#108: " << contains(ss, string("S#108")) << endl;
   cout << ss << endl;

   cout << "Emptying the stack, printing as we go" << endl;
   while (!ss.isEmpty()) {
       ss.pop();
       cout << "ss after pop: " << ss << endl;
   }

   cout << endl;
   cout << "Checking the exception handling facilities of the stack" << endl;

   try {
       si.pop();
       cout << "*** Error empty stack pop exception not caught" << endl;
   } catch (Stack_Exception e) {
       cout << "Expected exception caught: " << e.getWhat() << endl;
   }

   try {
       si.peek();
       cout << "*** Error empty stack peek exception not caught" << endl;
   } catch (Stack_Exception e) {
       cout << "Expected exception caught: " << e.getWhat() << endl;
   }

   for (int i = 1; i <= si.getCapacity(); i++)
       si.push(i);
   try {
       si.push(0);
       cout << "*** Error full stack push exception not caught" << endl;
   } catch (Stack_Exception e) {
       cout << "Expected exception caught: " << e.getWhat() << endl;
   }

   return 0;
}

//stack.h

#ifndef STACK_H
#define STACK_H

#include<iostream>
#include"stack_exception.h"

template<class T>

class Stack
{
T *top;
int capacity;
int size = 0;
//default constructor

public:
Stack();
void push(T val);
T pop();
T peek();
int getCapacity();
bool isEmpty();
friend ostream& operator<<(ostream &out, Stack<T> obj)
{
out << "[";
for (int i = obj.size-1; i >=0; i--)
{
if (i == obj.size - 1)
{
out << obj.top[i] ;
continue;
}
if (i >= 0)
out <<", "<< obj.top[i];
}
cout << "]";
return out;
}
};
template<class T>
Stack<T>::Stack()
{
//allocate memory for stack with capacity 100
top = new T[100];
capacity = 100;
size = 0;
}
template<class T>
void Stack<T>::push(T val)
{
try
{
if (size == capacity)
{
throw Stack_Exception("push attempted on full stack");
}
else
top[size++] = val;
}
catch (Stack_Exception e)
{
cout << e.getWhat();
}
}
template<class T>
T Stack<T>::pop()
{
try
{
if (size == 0)
{
throw Stack_Exception("pop attempted on empty stack");
}
else
{
T val = top[--size];
return val;
}
}catch (Stack_Exception &e)
{
cout << e.getWhat();
}
return NULL;
}
template<class T>
T Stack<T>::peek()
{
try
{
if (size == 0)
{
throw Stack_Exception("peek attempted on empty stack");
}
else
{
return top[size-1];
}
}
catch (Stack_Exception &e)
{
cout << e.getWhat();
}
return NULL;
}
template<class T>
int Stack<T>::getCapacity()
{
return capacity;
}
template<class T>
bool Stack<T>::isEmpty()
{
if (size == 0)
return true;
return false;
}

#endif

I need the expected output to look like this :

--- stack of int
si intially []
Pushing 0 .. 9 onto stack
si after push: [0]
si after push: [1, 0]
si after push: [2, 1, 0]
si after push: [3, 2, 1, 0]
si after push: [4, 3, 2, 1, 0]
si after push: [5, 4, 3, 2, 1, 0]
si after push: [6, 5, 4, 3, 2, 1, 0]
si after push: [7, 6, 5, 4, 3, 2, 1, 0]
si after push: [8, 7, 6, 5, 4, 3, 2, 1, 0]
si after push: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
si: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
A successful contains call followed by an unsuccessful contains
si contains 5: true
si after 'successful contains: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
si contains 10: false
si after 'unsuccessful contains: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Emptying the stack, printing as we go
si after pop: [8, 7, 6, 5, 4, 3, 2, 1, 0]
si after pop: [7, 6, 5, 4, 3, 2, 1, 0]
si after pop: [6, 5, 4, 3, 2, 1, 0]
si after pop: [5, 4, 3, 2, 1, 0]
si after pop: [4, 3, 2, 1, 0]
si after pop: [3, 2, 1, 0]
si after pop: [2, 1, 0]
si after pop: [1, 0]
si after pop: [0]
si after pop: []

--- stack of double
sd intially []
Pushing 10.5 ... 14.5 onto the stack
sd after push: [10.5]
sd after push: [11.5, 10.5]
sd after push: [12.5, 11.5, 10.5]
sd after push: [13.5, 12.5, 11.5, 10.5]
sd after push: [14.5, 13.5, 12.5, 11.5, 10.5]
[14.5, 13.5, 12.5, 11.5, 10.5]
A successful contains call followed by an unsuccessful contains
sd contains 10.5: true
[14.5, 13.5, 12.5, 11.5, 10.5]
sd contains 9.5: false
[14.5, 13.5, 12.5, 11.5, 10.5]
Emptying the stack, printing as we go
sd after pop: [13.5, 12.5, 11.5, 10.5]
sd after pop: [12.5, 11.5, 10.5]
sd after pop: [11.5, 10.5]
sd after pop: [10.5]
sd after pop: []

--- stack of string
ss intially []
Pushing S#100 ... S#115 onto stack (use ostringstream an in example presented in class)
ss after push: [S#100]
ss after push: [S#101, S#100]
ss after push: [S#102, S#101, S#100]
ss after push: [S#103, S#102, S#101, S#100]
ss after push: [S#104, S#103, S#102, S#101, S#100]
ss after push: [S#105, S#104, S#103, S#102, S#101, S#100]
ss after push: [S#106, S#105, S#104, S#103, S#102, S#101, S#100]
[S#106, S#105, S#104, S#103, S#102, S#101, S#100]
A successful contains call followed by an unsuccessful contains
ss contains S#105: true
[S#106, S#105, S#104, S#103, S#102, S#101, S#100]
ss contains S#108: false
[S#106, S#105, S#104, S#103, S#102, S#101, S#100]
Emptying the stack, printing as we go
ss after pop: [S#105, S#104, S#103, S#102, S#101, S#100]
ss after pop: [S#104, S#103, S#102, S#101, S#100]
ss after pop: [S#103, S#102, S#101, S#100]
ss after pop: [S#102, S#101, S#100]
ss after pop: [S#101, S#100]
ss after pop: [S#100]
ss after pop: []

Checking the exception handling facilities of the stack
Expected exception caught: pop attempted on empty stack
Expected exception caught: peek attempted on empty stack
Expected exception caught: push attempted on full stack

But for the exceptions (last 3 lines) it keeps printing this:

pop attempted on empty stack
*** Error empty stack pop exception not caught
peek attempted on empty stack
*** Error empty stack peek exception not caught
push attempted on full stack
*** Error full stack push exception not caught

I am only allowed to edit the stack.h file, can someone please help me add the necessary code so that it produces the correct output

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Only below file has changed, The change is exception will be caught by called you don't have to write try catch inside pop, push, peek. Just throw an exception, Thanks, PLEASE UPVOTE

===============================================================================

//stack.h

#ifndef STACK_H

#define STACK_H

#include<iostream>

#include<cstdlib>

#include"stack_exception.h"

template<class T>

class Stack

{

T *top;

int capacity;

int size = 0;

//default constructor

public:

Stack();

void push(T val);

T pop();

T peek();

int getCapacity();

bool isEmpty();

friend ostream& operator<<(ostream &out, Stack<T> obj)

{

out << "[";

for (int i = obj.size-1; i >=0; i--)

{

if (i == obj.size - 1)

{

out << obj.top[i] ;

continue;

}

if (i >= 0)

out <<", "<< obj.top[i];

}

cout << "]";

return out;

}

};

template<class T>

Stack<T>::Stack()

{

//allocate memory for stack with capacity 100

top = new T[100];

capacity = 100;

size = 0;

}

template<class T>

void Stack<T>::push(T val)

{

if (size == capacity)

{

throw Stack_Exception("push attempted on full stack");

}

else

top[size++] = val;

}

template<class T>

T Stack<T>::pop()

{

if (size == 0)

{

throw Stack_Exception("pop attempted on empty stack");

}

else

{

T val = top[--size];

return val;

}

return NULL;

}

template<class T>

T Stack<T>::peek()

{

if (size == 0)

{

throw Stack_Exception("peek attempted on empty stack");

}

else

{

return top[size-1];

}

return NULL;

}

template<class T>

int Stack<T>::getCapacity()

{

return capacity;

}

template<class T>

bool Stack<T>::isEmpty()

{

if (size == 0)

return true;

return false;

}

#endif

==================================================
SEE OUTPUT

Files https://EducatedFlimsyState.rahulkumar29.repl.run G main.cpp stack_exception.h stack.h saved 53 { 54 if (size == capaci

PLEASE COMMENT if there is any concern.

==============================

Add a comment
Know the answer?
Add Answer to:
//stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //trendtracker.h #ifndef TRENDTRACKER_H #define TRENDTRACKER_H #include <vector> #include <string> using namespace std; class Trendtracker { public:...

    //trendtracker.h #ifndef TRENDTRACKER_H #define TRENDTRACKER_H #include <vector> #include <string> using namespace std; class Trendtracker { public:    Trendtracker();    void insert(string ht);    int size();       void tweeted(string ht);    int popularity(string name);    string top_trend();    void top_three_trends(vector<string> &T);    void top_k_trends(vector<string> &T, int k); private:    class Entry    {    public:        string hashtag;        int pop;    }; vector<Entry> E; }; #endif //trendtracker.cpp #include"trendtracker.h" using namespace std; // Creates a new Trendtracker tracking...

  • #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:       ...

    #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:        int data;        Node* nextNodePtr;           public:        Node(){}               void setData(int d){            data = d;        }               int getData(){            return data;        }               void setNextNodePtr(Node* nodePtr){                nextNodePtr = nodePtr;        }                ...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const...

    stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...

  • In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

    In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

  • I need to modify my C++ code so it can get the min value of the...

    I need to modify my C++ code so it can get the min value of the stack code is as follows: #include <iostream> using namespace std; #define MAX_SIZE 100 class Stack { private: int S[MAX_SIZE]; int top; public: Stack() {   top = -1; } void push(int x) {   if (top == MAX_SIZE - 1) {    cout << "Stack is Full." << endl;    return;   }   S[++top] = x; } void pop() {   if (top == -1) {    cout << "Stack is...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std;...

    Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std; int is_palindrome(string word){ int len = word.size(); for(int i=0; i<len/2; i++){ if(toupper(word[i])!=toupper(word[len-i-1])) return 0; } return 1; } int have_vowels3(string word){ int cnt = 0; for(int i=0; i<word.size(); i++){ if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u') cnt++; } if(cnt>=3) return 1; else return 0; } int have_consecutives(string word){ for(int i=0; i<word.size()-1; i++){ if(tolower(word[i])=='o' && tolower(word[i+1]=='o')) return 1; } return...

  • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

    template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT