In: Statistics and Probability
Code needed in C++, make changes to the file provided (18-1, has 3 files)
Chapter 18 Stacks and Queues
-----------------------------------------------------------------------------------------------------
capacity is just 5
1. push 6 numbers on the stack
2. catch the overflow error in a catch block
3. pop one element, which means your capacity is now down to 4
4. push the element that was rejected earlier
5. verify your entire stack by popping to show the new numbers.
IntStack.h
#include <memory>
using namespace std;
class IntStack
{
unique_ptr<int[]>stackArray;
int capacity;
int top;
public:
// Constructor
IntStack(int capacity);
// Member functions
void push(int value);
void pop(int &value);
bool isEmpty() const;
// Stack Exceptions
class Overflow {};
class Underflow {};
};
IntStack.cpp
ZOOM
#include "intstack.h"
//************************************
// Constructor *
//************************************
IntStack::IntStack(int capacity)
{
stackArray = make_unique<int[]>(capacity);
this->capacity = capacity;
top = 0;
}
//***********************************
// Adds a value to the stack *
//***********************************
void IntStack::push(int value)
{
if (top == capacity) throw IntStack::Overflow();
stackArray[top] = value;
top++;
}
//****************************************
// Determines whether the stack is empty *
//****************************************
bool IntStack::isEmpty() const
{
return top == 0;
}
//************************************************
// Removes a value from the stack and returns it *
//************************************************
void IntStack::pop(int &value)
{
if (isEmpty()) throw IntStack::Underflow();
top--;
value = stackArray[top];
}
pr18-01.cpp
// This program illustrates the IntStack class.
#include "intstack.h"
#include <iostream>
using namespace std;
int main()
{
IntStack stack(5);
int values[] = { 5, 10, 15, 20, 25 };
int value;
cout << "Pushing...\n";
for (int k = 0; k < 5; k++)
{
cout << values[k] << " ";
stack.push(values[k]);
}
cout << "\nPopping...\n";
while (!stack.isEmpty())
{
stack.pop(value);
cout << value << " ";
}
cout << endl;
return 0;
}
/* note: use compiler std=c++14 */
/* intStack.h */
#include <memory>
using namespace std;
class IntStack
{
unique_ptr<int[]>stackArray;
int capacity;
int top;
public:
// Constructor
IntStack(int capacity);
// Member functions
void push(int value);
void pop(int &value);
bool isEmpty() const;
// Stack Exceptions
class Overflow {};
class Underflow {};
};
/* intStack.cpp */
#include "intstack.h"
//************************************
// Constructor *
//************************************
IntStack::IntStack(int capacity)
{
stackArray = make_unique<int[]>(capacity);
this->capacity = capacity;
top = 0;
}
//***********************************
// Adds a value to the stack *
//***********************************
void IntStack::push(int value)
{
if (top == capacity) throw IntStack::Overflow();
stackArray[top] = value;
top++;
}
//****************************************
// Determines whether the stack is empty *
//****************************************
bool IntStack::isEmpty() const
{
return top == 0;
}
//************************************************
// Removes a value from the stack and returns it *
//************************************************
void IntStack::pop(int &value)
{
if (isEmpty()) throw IntStack::Underflow();
top--;
value = stackArray[top];
}
/* main.cpp */
#include "intstack.h"
#include<iostream>
using namespace std;
int main()
{
// capacity is just 5
IntStack stack(5);
int values[] = { 5, 10, 15, 20, 25, 45 }; // make it
of 6
int value;
cout << "Pushing...\n";
try{
// 1. push 6 numbers on the
stack
for (int k = 0; k < 6;
k++)
{
cout <<
values[k] << " ";
stack.push(values[k]);
}
}catch(IntStack::Overflow s){
cout<<"\n"<<"Overflow"<<endl;
}catch(IntStack::Underflow s){
cout<<"\n"<<"Underflow"<<endl;
}
// 3. pop one element, which means your capacity is
now down to 4
stack.pop(value);
cout<<"\nPoped one element:
"<<value<<endl;
// 4. push the element that was rejected earlier
cout<<"\nPush element rejected earlier
"<<endl;
cout<<"\nPushing
"<<values[5]<<endl;
stack.push(values[5]); // values is 45 which was
rejeted earlier
cout << "\nPopping...\n";
//5. verify your entire stack by popping to show the
new numbers.
while (!stack.isEmpty())
{
stack.pop(value);
cout << value << "
";
}
cout << endl;
return 0;
}
/* OUTPUT */