Ex1) Modify the Stack class to include appropriate error messages if invalid con
ID: 3880101 • Letter: E
Question
Ex1) Modify the Stack class to include appropriate error messages if invalid conditions occur—for example, trying to pop an item when the stack is empty.
Below is the code (main.cpp, stack.cpp, stack.h)
main.cpp
#include <iostream>
#include "stack.h"
using namespace std;
int main()
{
Stack s;
cout << "Insertion of 10 characters in s" << endl;
for (int i = 0; i < s.getSize(); i++)
{
char x = 32 + rand()%95;
cout << x << endl;
s.push(x);
}
cout << endl
<< "Displaying and deleting elements from s" << endl;
while(!s.isEmpty())
{
cout << "Item at the top: " << s.peek() << endl;
s.pop();
}
return 0;
}
stack.cpp
/*******************************
* Week 2 lesson: *
* a simple Stack class *
*******************************/
#include "Stack.h"
/*
* Constructor. Initializes the stack.
*/
Stack::Stack()
{
top = 0;
}
/*
* Determines whether the stack is empty.
*
* Returns true if the stack is empty, false otherwise.
*/
bool Stack::isEmpty()
{
return top == 0;
}
/*
* Adds an element to the top of the stack.
*
* x: element to be added to the stack.
*/
void Stack::push(char x)
{
list[top] = x;
top++;
}
/*
* Removes the element at the top of the stack.
*/
void Stack::pop()
{
top--;
}
/*
* Returns the element at the top of the stack. Does not remove it.
*/
char Stack::peek()
{
return list[top-1];
}
/*
* Returns the size of the stack.
*/
int Stack::getSize()
{
return SIZE;
}
stack.h
/*******************************
* Week 2 lesson: *
* a simple Stack class *
*******************************/
/*
* Class implementing a Stack ADT.
*/
class Stack
{
public:
Stack();
bool isEmpty();
void push(char);
void pop();
char peek();
int getSize();
private:
static const int SIZE = 10; //size of the queue array
char list[SIZE]; //array to store the stack items
int top; //amount of elements in the array
};
Explanation / Answer
Given below are the modified files which show error messages.
stack.cpp
========
/*******************************
* Week 2 lesson: *
* a simple Stack class *
*******************************/
#include "stack.h"
#include <iostream>
using namespace std;
/*
* Constructor. Initializes the stack.
*/
Stack::Stack()
{
top = 0;
}
/*
* Determines whether the stack is empty.
*
* Returns true if the stack is empty, false otherwise.
*/
bool Stack::isEmpty()
{
return top == 0;
}
/*
* Adds an element to the top of the stack.
*
* x: element to be added to the stack.
*/
void Stack::push(char x)
{
if(top == SIZE)
cout << "Stack is full. Can not push." << endl;
else
{
list[top] = x;
top++;
}
}
/*
* Removes the element at the top of the stack.
*/
void Stack::pop()
{
if(isEmpty())
cout << "Stack is empty! Can not pop." << endl;
else
top--;
}
/*
* Returns the element at the top of the stack. Does not remove it.
*/
char Stack::peek()
{
if(isEmpty())
cout << "Stack is empty. Can not peek" << endl;
return list[top-1];
}
/*
* Returns the size of the stack.
*/
int Stack::getSize()
{
return top;
}
main.cpp
========
#include <iostream>
#include "stack.h"
using namespace std;
int main()
{
Stack s;
cout << "Insertion of 12 characters in s" << endl;
for (int i = 0; i < 12; i++)
{
char x = 32 + rand()%95;
cout << "pushing '" << x << "'" << endl;
s.push(x);
}
cout << endl
<< "Displaying and deleting elements from s" << endl;
while(!s.isEmpty())
{
cout << "Item at the top: " << s.peek() << endl;
s.pop();
}
//try to pop() from empty stack, should show error message
s.pop();
return 0;
}
output
=======
Insertion of 12 characters in s
pushing 'w'
pushing '`'
pushing '('
pushing '}'
pushing 'u'
pushing '@'
pushing 'o'
pushing 'd'
pushing '('
pushing '~'
pushing 'C'
Stack is full. Can not push.
pushing 'C'
Stack is full. Can not push.
Displaying and deleting elements from s
Item at the top: ~
Item at the top: (
Item at the top: d
Item at the top: o
Item at the top: @
Item at the top: u
Item at the top: }
Item at the top: (
Item at the top: `
Item at the top: w
Stack is empty! Can not pop.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.