//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
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

PLEASE COMMENT if there is any concern.
==============================
//stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what)...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
//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...
Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...
#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;
}
...
Can I get a C++ code and output for this program using classes instead of using struct. The following program implements a Last In First Out (LIFO) stack. ( I want to use class for function definitions too and if main need.) #include <iostream> using namespace std; const int MAX = 100; struct stack { int s[MAX]; // an array of integers int top; // the index of the last number }; void push(stack &, int); void pop(stack&,...
#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 { 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 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 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 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...