#include
#include
#include
#include "GroceryList.h"
using namespace std;
bool ParseIndices(std::string str, int& outIndex1, int& outIndex2);
int main(int argc, char *argv[]){
//initalize s new grocery list
GroceryList groceryList;
string command;
bool quit = false;
while(!quit){
getline (cin, command);
if(command == "print"){
groceryList.Print(cout);
}
else if(0== command.find("add")){
groceryList.AddWithUndo(command.substr(4));
}
else if(0 == command.find("removeat")){
int index = stoi(command.substr(9));
groceryList.RemoveWithUndo(index);
}
else if(0 == command.find("swap")){
int index1 = -1, index2 = -1;
if(ParseIndices(command.substr(5), index1,index2)){
groceryList.SwapWithUndo(index1, index2);
}
else if(command=="undo"){
if(0==grocerList.GetUndoStackSize()){
cout<< "Cannot execute undo because undo stack is empty"<< endl;
}
else{
groceryList.ExecuteUndo();
}
else if(command==" quit"){
quit == true;
}
else{
cout<<"Unknown command:<< command << endl;
}
}
return 0;
}
bool ParseIndices(std::string str, int& outIndex1, int& outIndex2){
auto spaceIndex = str.find(" ");
if(spaceIndex == strinh::npis){
return false;
}
outIndex1 = stoi(str);
outIndex2 = stoi(str.substr(spaceIndex +1));
return true;
}
the RemoveLastCommand class inherits from UndoCommand and is declared in
RemoveLastCommand.h When a RemoveLastCommand object executed, the strinh vectors last
element is removed. So when the user appends a new item in the grocery list a
RemoveLastCommand is pushed onto the stack of undo commands.Popping and executing the
RemoveLastCommand then removes the item most recently added by user.
RemoveLastCommands sourceVector member variable and constructir are already declared
sourceVector is a pointer to a GroceryList objects vector of strings.
The constructor takes a pointer to a vector of strings as a parameter, and assigns sourceVectors
witb the pointer.
implement RemoveLastCommandS Execute() member function to remove sourceVectors last
element.
c++ c++
class RemoveLastCommand : public UndoCommand{
private:
std::vector* sourceVector;
Public:
RemoveLastCommand(std::vector* vector){
sourceVector = vector;
}
void Execute() override{
// code here
};
#endif
implement GroceryLists ExecuteUndo() member function
to di following
pop an UndoCommand off the undo stack
Execute the popped undo command
Delete the undo command
File main.cpp had a code that reads in a list of commands one per line that allows for basic
testing of basic operations. So after implementing ExecuteUndo() run program with the
following input:
add bananas
add grapes
add strawberries
print
undo
print
undo
print
quit
verify that the ouput is 0. banana
1. grapes and so forth
class GroceryList{
protected:
std::vector listItems;
std::stackundostack;
public:
virtual voif AddWithUndo(std:: string newItemName({
listItems.push_back(newItemName);
//make an undo command that removes //the last item and push onto stack
undoStack.push(new RemoveLastCommand(&listItem));
}
virtual void RemoveAtWithUndo(i.
1. #include
#include
#include
#include "GroceryList.h"
using namespace std;
bool ParseIndices(std::string str, int& outIndex1, int& outIndex2);
int main(int argc, char *argv[]){
//initalize s new grocery list
GroceryList groceryList;
string command;
bool quit = false;
while(!quit){
getline (cin, command);
if(command == "print"){
groceryList.Print(cout);
}
else if(0== command.find("add")){
groceryList.AddWithUndo(command.substr(4));
}
else if(0 == command.find("removeat")){
int index = stoi(command.substr(9));
groceryList.RemoveWithUndo(index);
}
else if(0 == command.find("swap")){
int index1 = -1, index2 = -1;
if(ParseIndices(command.substr(5), index1,index2)){
groceryList.SwapWithUndo(index1, index2);
}
else if(command=="undo"){
if(0==grocerList.GetUndoStackSize()){
cout<< "Cannot execute undo because undo stack is empty"<< endl;
}
2. else{
groceryList.ExecuteUndo();
}
else if(command==" quit"){
quit == true;
}
else{
cout<<"Unknown command:<< command << endl;
}
}
return 0;
}
bool ParseIndices(std::string str, int& outIndex1, int& outIndex2){
auto spaceIndex = str.find(" ");
if(spaceIndex == strinh::npis){
return false;
}
outIndex1 = stoi(str);
outIndex2 = stoi(str.substr(spaceIndex +1));
return true;
}
the RemoveLastCommand class inherits from UndoCommand and is declared in
RemoveLastCommand.h When a RemoveLastCommand object executed, the strinh vectors last
element is removed. So when the user appends a new item in the grocery list a
RemoveLastCommand is pushed onto the stack of undo commands.Popping and executing the
RemoveLastCommand then removes the item most recently added by user.
RemoveLastCommands sourceVector member variable and constructir are already declared
sourceVector is a pointer to a GroceryList objects vector of strings.
The constructor takes a pointer to a vector of strings as a parameter, and assigns sourceVectors
witb the pointer.
implement RemoveLastCommandS Execute() member function to remove sourceVectors last
element.
c++ c++
3. class RemoveLastCommand : public UndoCommand{
private:
std::vector* sourceVector;
Public:
RemoveLastCommand(std::vector* vector){
sourceVector = vector;
}
void Execute() override{
// code here
};
#endif
implement GroceryLists ExecuteUndo() member function
to di following
pop an UndoCommand off the undo stack
Execute the popped undo command
Delete the undo command
File main.cpp had a code that reads in a list of commands one per line that allows for basic
testing of basic operations. So after implementing ExecuteUndo() run program with the
following input:
add bananas
add grapes
add strawberries
print
undo
print
undo
print
quit
verify that the ouput is 0. banana
1. grapes and so forth
class GroceryList{
protected:
std::vector listItems;
std::stackundostack;
4. public:
virtual voif AddWithUndo(std:: string newItemName({
listItems.push_back(newItemName);
//make an undo command that removes //the last item and push onto stack
undoStack.push(new RemoveLastCommand(&listItem));
}
virtual void RemoveAtWithUndo(int removalIndex){
//code here
}
void SwapWithUndo(int index1, int index2){
//code here
}
virtual void ExecuteUndo(){
if(!undoStack.empty()){
undoCommand* undoCommand = undoStack.top();
undoStack.pop();
undo->Execute();
delet undoCommand;
}
}
virtual int GetListSize() const{
return(int) listItems.size();
}
virtual int GetUndoStackSize() const{
return(int) undoStack.size();
}
virtual std:: vector GetVectorCopy() const{
return listItems;
}
virtuak void Print(std:: ostream& outputstream){
for(size_t i = 0; i < listItems.size(); i++){
outputstream <
#include
#include
#include "GroceryList.h"
using namespace std;
6. cout<<"Unknown command:<< command << endl;
}
}
return 0;
}
bool ParseIndices(std::string str, int& outIndex1, int& outIndex2){
auto spaceIndex = str.find(" ");
if(spaceIndex == strinh::npis){
return false;
}
outIndex1 = stoi(str);
outIndex2 = stoi(str.substr(spaceIndex +1));
return true;
}
the RemoveLastCommand class inherits from UndoCommand and is declared in
RemoveLastCommand.h When a RemoveLastCommand object executed, the strinh vectors last
element is removed. So when the user appends a new item in the grocery list a
RemoveLastCommand is pushed onto the stack of undo commands.Popping and executing the
RemoveLastCommand then removes the item most recently added by user.
RemoveLastCommands sourceVector member variable and constructir are already declared
sourceVector is a pointer to a GroceryList objects vector of strings.
The constructor takes a pointer to a vector of strings as a parameter, and assigns sourceVectors
witb the pointer.
implement RemoveLastCommandS Execute() member function to remove sourceVectors last
element.
c++ c++
class RemoveLastCommand : public UndoCommand{
private:
std::vector* sourceVector;
Public:
RemoveLastCommand(std::vector* vector){
sourceVector = vector;
}
7. void Execute() override{
// code here
};
#endif
implement GroceryLists ExecuteUndo() member function
to di following
pop an UndoCommand off the undo stack
Execute the popped undo command
Delete the undo command
File main.cpp had a code that reads in a list of commands one per line that allows for basic
testing of basic operations. So after implementing ExecuteUndo() run program with the
following input:
add bananas
add grapes
add strawberries
print
undo
print
undo
print
quit
verify that the ouput is 0. banana
1. grapes and so forth
class GroceryList{
protected:
std::vector listItems;
std::stackundostack;
public:
virtual voif AddWithUndo(std:: string newItemName({
listItems.push_back(newItemName);
//make an undo command that removes //the last item and push onto stack
undoStack.push(new RemoveLastCommand(&listItem));
}
virtual void RemoveAtWithUndo(int removalIndex){
8. //code here
}
void SwapWithUndo(int index1, int index2){
//code here
}
virtual void ExecuteUndo(){
if(!undoStack.empty()){
undoCommand* undoCommand = undoStack.top();
undoStack.pop();
undo->Execute();
delet undoCommand;
}
}
virtual int GetListSize() const{
return(int) listItems.size();
}
virtual int GetUndoStackSize() const{
return(int) undoStack.size();
}
virtual std:: vector GetVectorCopy() const{
return listItems;
}
virtuak void Print(std:: ostream& outputstream){
for(size_t i = 0; i < listItems.size(); i++){
outputstream <
#include
#include "GroceryList.h"
using namespace std;
bool ParseIndices(std::string str, int& outIndex1, int& outIndex2);
int main(int argc, char *argv[]){
//initalize s new grocery list
GroceryList groceryList;
string command;
bool quit = false;
10. return false;
}
outIndex1 = stoi(str);
outIndex2 = stoi(str.substr(spaceIndex +1));
return true;
}
the RemoveLastCommand class inherits from UndoCommand and is declared in
RemoveLastCommand.h When a RemoveLastCommand object executed, the strinh vectors last
element is removed. So when the user appends a new item in the grocery list a
RemoveLastCommand is pushed onto the stack of undo commands.Popping and executing the
RemoveLastCommand then removes the item most recently added by user.
RemoveLastCommands sourceVector member variable and constructir are already declared
sourceVector is a pointer to a GroceryList objects vector of strings.
The constructor takes a pointer to a vector of strings as a parameter, and assigns sourceVectors
witb the pointer.
implement RemoveLastCommandS Execute() member function to remove sourceVectors last
element.
c++ c++
class RemoveLastCommand : public UndoCommand{
private:
std::vector* sourceVector;
Public:
RemoveLastCommand(std::vector* vector){
sourceVector = vector;
}
void Execute() override{
// code here
};
#endif
implement GroceryLists ExecuteUndo() member function
to di following
11. pop an UndoCommand off the undo stack
Execute the popped undo command
Delete the undo command
File main.cpp had a code that reads in a list of commands one per line that allows for basic
testing of basic operations. So after implementing ExecuteUndo() run program with the
following input:
add bananas
add grapes
add strawberries
print
undo
print
undo
print
quit
verify that the ouput is 0. banana
1. grapes and so forth
class GroceryList{
protected:
std::vector listItems;
std::stackundostack;
public:
virtual voif AddWithUndo(std:: string newItemName({
listItems.push_back(newItemName);
//make an undo command that removes //the last item and push onto stack
undoStack.push(new RemoveLastCommand(&listItem));
}
virtual void RemoveAtWithUndo(int removalIndex){
//code here
}
void SwapWithUndo(int index1, int index2){
//code here
}
virtual void ExecuteUndo(){
if(!undoStack.empty()){
undoCommand* undoCommand = undoStack.top();