In: Computer Science
Implementing a Stack
Write a program that implements a stack of integers, and exercises the stack based on commands read from cin. To do this, write a class called Stack with exactly the following members: class Stack { public: bool isEmpty(); // returns true if stack has no elements stored int top(); // returns element from top of the stack // throws runtime_error("stack is empty") int pop(); // returns element from top of the stack and removes it // throws runtime_error("stack is empty") void push(int); // puts a new element on top of the stack private: vector elements; }; The program should read commands from cin until either end-of-file is reached or the command end is entered. (You can manually test for end-of-file by entering CTRL-D.) Each time the program expects a new command it should first print a prompt: "stack> " Your program should catch all errors that happen and continue execution until the end command or end-of-file. In case the command push is read, the program should read an integer value from cin and push it onto the stack. In case top is read, the program should print the top integer of the stack to cout. In case pop is read, the program should print the top integer of the stack to cout, and remove it from the stack. In case the command list is read, the program should print all values currently on the stack, without modifying the stack. (Exact format see below.) Your program should check whether a "number" to be pushed is actually a number. If not, print an error message (see below), and reset cin such that it will again accept commands. (See Section 7.6 of the zyBook.) Also, your program should ignore all characters behind a number to be pushed that are on the same input line (example see below). An example of a correct execution of this program is shown below: stack> push 5 stack> pop 5 stack> pop error: stack is empty stack> push 6 stack> push 4bb stack> push foo error: not a number stack> list [4,6] stack> list [4,6] stack> top 4 stack> hello error: invalid command stack> end You may want to use the compare() function (Links to an external site.) on a string to check which command has been entered. Use of arrays, a built-in stack class, or container classes from std:: other than vector, is not allowed.
//Stack.h
#pragma once
#include<iostream>
#include<vector>
using namespace std;
class Stack
{
public: bool isEmpty(); // returns true if stack has no elements
stored
int top(); // returns element from
top of the stack // throws runtime_error("stack is empty")
int pop(); // returns element from
top of the stack and removes it // throws runtime_error("stack is
empty")
void push(int);
// puts a new element on top of the
stack
private:
vector<int> elements;
};
========================
//Stack.cpp
#include"Stack.h"
bool Stack::isEmpty() // returns true if stack has no elements
stored
{
if (elements.size() == 0)
return true;
return false;
}
int Stack::top()// returns element from top of the stack //
{
try
{
if (isEmpty() == true)
{
throw
runtime_error("stack is empty");
}
else
return
elements[elements.size() - 1];
}
catch (exception e)
{
cout << e.what() <<
endl;
return -9999;
}
}
int Stack::pop() // returns element from top of the stack and
removes it // throws runtime_error("stack is empty")
{
try
{
if (isEmpty() == true)
{
throw
runtime_error("stack is empty");
}
else
{
int val =
elements[elements.size() - 1];
elements.pop_back();
return
val;
}
}
catch (exception e)
{
cout << e.what() <<
endl;
return -9999;
}
}
void Stack::push(int val)
{
elements.push_back(val);
}
========================================
//MainStack.cpp
#include <iostream>
#include<string>
#include"Stack.h"
int main() {
string input;
int val;
Stack st1;
while (!cin.eof())
{
cout << "stack> ";
cin >> input;
if (input.compare("push") ==
0)
{
cin >>
val;
st1.push(val);
}
else if (input.compare("pop") ==
0)
{
int val =
st1.pop();
if (val !=
-9999)
cout << val<<endl;
}
else if (input.compare("top") ==
0)
{
int val =
st1.top();
if(val !=
-9999)
cout << val << endl;
}
else if (input.compare("list") ==
0)
{
Stack st2 =
st1;
cout <<
"[";
while
(!st2.isEmpty())
{
cout << st2.pop();
if (!st2.isEmpty())
cout << ", ";
}
cout <<
"]" << endl;
}
else if (input.compare("end") ==
0)
{
break;
}
else
cout <<
endl;
}
}
/*Output
stack> push 1
stack> push 2
stack> push 3
stack> pop
3
stack> top
2
stack> list
[2, 1]
stack> pop
2
stack> pop
1
stack> pop
stack is empty
stack> end
*/
============================================