Question

In: Computer Science

Implementing a Stack Write a program that implements a stack of integers, and exercises the stack...

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.

Solutions

Expert Solution

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

*/

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


Related Solutions

Write a program that implements a stack of integers, and exercises the stack based on commands...
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")...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
write a c++ program to perform the following operations on stack of Integers (Array Implementation of...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of Stack with maximum size MAX) (i) Push an Element on to stack (ii) Pop an Element from stack (iii) Demonstrate how stack can be used to check Palindrome (iv) Display the status of stack (v) Exit
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race condition and is not appropriate for a concurrent environment. Using Pthreads mutex locks, fix the race condition. For reference, see Section 7.3.1 of SGG book.(Section 7.3.1 is about mutex and semaphores it does explain how to implement I'm just having a hard time finding the race condition within the code) /* * Stack containing race conditions */ #include #include #include typedef int value_t; //...
In an application write a method filterStack that takes a stack of integers as a parameter...
In an application write a method filterStack that takes a stack of integers as a parameter and filters its elements (in a new Stack) in a way that places the even elements at the bottom and the odd ones at the top. The original stack should remain unchanged. You should use a queue (only one queue) as a temporary storage. Use stack and queue operations only to solve this problem. No need to write the main method. For example, if...
/** * Write a recursive function that accepts a stack of integers and * replaces each...
/** * Write a recursive function that accepts a stack of integers and * replaces each int with two copies of that integer. For example, * calling repeatStack and passing in a stack of { 1, 2, 3} would change * the stack to hold { 1, 1, 2, 2, 3, 3}. Do not use any loops. Do not use * any data structures other than the stack passed in as a parameter. * @param stack */ public static void...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT