need help on c++ programming please Driver.cpp Stack.cpp: stack.h Lab Assignment
ID: 3724029 • Letter: N
Question
need help on c++ programming please
Driver.cpp
Stack.cpp:
stack.h
Lab Assignment 1 As promised, here is a fun guessing game: a program chooses a container for you, but you don't know what it is. A container can be a stack (Last In First Out), a queue (First In First Out), a priority queue (Most "Im- portant" One Out), and a mob (Random One Out). The new container types are the queue, the priority queue and the mob For a priority queue, some characteristic of the stored data determines the priority. For example, for a numeric priority queue, the magnitude of the number may determine its priority (hence we can have a Max priority quene or a Min priority queue) In a mob container the number that comes ou is selected at random. A mob is also sometimes called a bag, which means that you can reach into the bag and grab one of the stored items at random The object of the game is to gucss what container the program sclected for you, by just looking at the input and output data. You do not need to submit anything for this task of the lab, so just play and have some fun, but make sure you understand how the four containers work. Get the following files for the game code: RanGen.h. RanGen.cpp, Container·h, and GameDriver.cpp. These can be found in the github repo 2C+developers have been very nice to you. Inside the STL there is a class called a stack. We are going to use this stack to reverse the characters in a string. For this task create a function called string stringReversall (string input)Explanation / Answer
main.cpp
#include <iostream>
#include "Lab5MyStack.h"
using std::cout;
using std::cin;
int main()
{
string input;
// Test for stringReversal1.
cout << "Please enter a string which you would like reversed: ";
cin >> input;
cout << "Original string: " << input << " ";
cout << "Reversed string: " << stringReversal1(input) << " ";
// Test for stringReversal2.
cout << "Please enter a string which you would like reversed: ";
cin >> input;
cout << "Original string: " << input << " ";
cout << "Reversed string: " << stringReversal2(input) << " ";
// Test for stringReversal3.
cout << "Please enter a string which you would like reversed: ";
cin >> input;
cout << "Original string: " << input << " ";
cout << "Reversed string: " << stringReversal3(input) << " ";
// Test for stringReversal4.
cout << "Please enter a string which you would like reversed: ";
cin >> input;
cout << "Original string: " << input << " ";
cout << "Reversed string: " << stringReversal4(input) << " ";
return 0;
}
Lab5MyStack.h
#pragma once
#ifndef LAB5MYSTACK_H
#define LAB5MYSTACK_H
#include <string>
#include <vector>
using std::string;
using std::vector;
// Reverses a string using the STL stack implementation.
string stringReversal1(string input);
// Reverses a string using the vector implementation.
string stringReversal2(string input);
// Reverses a string using the list implementation.
string stringReversal3(string input);
// Reverses a string using the MyStack implementation.
string stringReversal4(string input);
// Interface of the MyStack class.
class MyStack
{
public:
// Holds the elements of the stack in a vector.
vector<char> stack;
// Holds the elements of a stack in a list.
// list<char> stack;
// Checks if the stack is empty.
bool isEmpty() const;
// Pushes a new character to the stack.
void push(char &c);
// Keeps track of the top of the stack and pops the top of the stack.
char pull();
};
#endif
Lab5MyStack.cpp
#include "Lab5MyStack.h"
#include <stack>
#include <vector>
#include <list>
using std::stack;
using std::vector;
using std::list;
string stringReversal1(string input)
{
// Initiate the stack in which to hold the characters of the string.
stack<char> newStack;
// Add each character of the string to the stack.
for (int i = 0; i < input.length(); i++)
{
newStack.push(input.at(i));
}
// Initiate a new string which will be the reversed string.
string reverseString;
// Reverse the input string by adding the top of the stack
// to the new string and then popping the top of the stack
// until the stack is empty.
while (!newStack.empty())
{
reverseString.push_back(newStack.top());
newStack.pop();
}
return reverseString;
}
string stringReversal2(string input)
{
// Initiate the vector in which to hold the characters of the string.
vector<char> newVector;
// Add each character of the string to the vector.
for (int i = 0; i < input.length(); i++)
{
newVector.push_back(input.at(i));
}
// Initiate a new string which will be the reversed string.
string reverseString;
// Reverse the input string by adding the last character in
// the vector to the new string and deleting the last element
// until the vector is empty.
while (!newVector.empty())
{
reverseString.push_back(newVector.back());
newVector.pop_back();
}
return reverseString;
}
string stringReversal3(string input)
{
// Initiate the new list in which to hold the characters of the string.
list<char> newList;
// Add each character of the string to the list.
for (int i = 0; i < input.length(); i++)
{
newList.push_back(input.at(i));
}
// Initiate a new string which will be the reversed string;
string reversedString;
// Reverse the input string by adding the last element of the list
// to the new string and deleting the last element until the list
// is empty.
while (!newList.empty())
{
reversedString.push_back(newList.back());
newList.pop_back();
}
return reversedString;
}
string stringReversal4(string input)
{
// Initiate the stack in which to hold the characters of the string.
MyStack newStack;
// Add each character of the string to the stack.
for (int i = 0; i < input.length(); i++)
{
newStack.push(input.at(i));
}
// Initiate a new string which will be the reversed string.
string reverseString;
// Reverse the input string by adding the top of the stack
// to the new string and then popping the top of the stack
// until the stack is empty.
while (!newStack.isEmpty())
{
reverseString.push_back(newStack.pull());
}
return reverseString;
}
bool MyStack::isEmpty() const
{
if (stack.empty())
{
return true;
}
else
{
return false;
}
}
void MyStack::push(char & c)
{
stack.push_back(c);
}
char MyStack::pull()
{
// Store the last element in the stack to allow for deletion.
char last = stack.back();
// Delete the last element.
stack.pop_back();
return last;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.