SlideShare a Scribd company logo
#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(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;
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(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 "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(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 <

More Related Content

PDF
Using the C++ programming language1. Implement the UnsortedList cl.pdf
PDF
This is the main file include itemh include itemList.pdf
PDF
Using C++I keep getting messagehead does not name a type.pdf
PDF
Write a class that implements the BagInterface. BagInterface should .pdf
PDF
Header file for an array-based implementation of the ADT bag. @f.pdf
PDF
fully comments for my program, thank you will thumb up#include io.pdf
PPTX
Stack and its applications
PDF
File yuan.h#include iostream#include fstream#include .pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdf
This is the main file include itemh include itemList.pdf
Using C++I keep getting messagehead does not name a type.pdf
Write a class that implements the BagInterface. BagInterface should .pdf
Header file for an array-based implementation of the ADT bag. @f.pdf
fully comments for my program, thank you will thumb up#include io.pdf
Stack and its applications
File yuan.h#include iostream#include fstream#include .pdf

Similar to #includeiostream#includestack#includestring #include .pdf (20)

PDF
C++ help finish my code Phase 1 - input phase. Main reads the fi.pdf
DOCX
Class array
PDF
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
PPT
Standard Template Library (STL) in Object Oriented Programming
DOCX
Program Specifications Develop an inventory management system for an e.docx
DOCX
Program Specifications in c++ Develop an inventory management syste.docx
DOCX
Program Specifications in c++ Develop an inventory management system f.docx
PPTX
Object Oriented Design and Programming Unit-05
PDF
show code and all classes with full implementation for these Program S.pdf
PDF
C++ help finish my code Topics class definitions, arrays of objec.pdf
PDF
DS & Algo 1 - C++ and STL Introduction
DOCX
Implementation File- -------------------------------------------------.docx
PDF
Please assist with this and use single line comment to explain each .pdf
PDF
1- The design of a singly-linked list below is a picture of the functi (1).pdf
PDF
#includeiostream #includefstreamusing namespace std; glo.pdf
PDF
BackgroundIn many applications, the composition of a collection o.pdf
DOCX
New microsoft word document
PDF
Pleae help me with this C++ task to the required result by edit or f.pdf
PPTX
Intro To C++ - Class #18: Vectors & Arrays
PDF
Data Structure
C++ help finish my code Phase 1 - input phase. Main reads the fi.pdf
Class array
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
Standard Template Library (STL) in Object Oriented Programming
Program Specifications Develop an inventory management system for an e.docx
Program Specifications in c++ Develop an inventory management syste.docx
Program Specifications in c++ Develop an inventory management system f.docx
Object Oriented Design and Programming Unit-05
show code and all classes with full implementation for these Program S.pdf
C++ help finish my code Topics class definitions, arrays of objec.pdf
DS & Algo 1 - C++ and STL Introduction
Implementation File- -------------------------------------------------.docx
Please assist with this and use single line comment to explain each .pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
#includeiostream #includefstreamusing namespace std; glo.pdf
BackgroundIn many applications, the composition of a collection o.pdf
New microsoft word document
Pleae help me with this C++ task to the required result by edit or f.pdf
Intro To C++ - Class #18: Vectors & Arrays
Data Structure
Ad

More from srinivas9922 (7)

PDF
they should clearly communicate what the model component represents. .pdf
PDF
1. How did ODO operationalize the definition of an adult with a disa.pdf
PDF
Write a program that do the following 1. Define one integer variable.pdf
PDF
Which of the following statements about location is FALSE LatLong.pdf
PDF
#include iostream#include iomanip#include fstreamusing n.pdf
PDF
1) Supongamos que los gobiernos federal, estatal y local de Estados .pdf
PDF
1) Create logical design for the following ERD (20pts) MEMBER Member.pdf
they should clearly communicate what the model component represents. .pdf
1. How did ODO operationalize the definition of an adult with a disa.pdf
Write a program that do the following 1. Define one integer variable.pdf
Which of the following statements about location is FALSE LatLong.pdf
#include iostream#include iomanip#include fstreamusing n.pdf
1) Supongamos que los gobiernos federal, estatal y local de Estados .pdf
1) Create logical design for the following ERD (20pts) MEMBER Member.pdf
Ad

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Classroom Observation Tools for Teachers
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Structure & Organelles in detailed.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Classroom Observation Tools for Teachers
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Anesthesia in Laparoscopic Surgery in India
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
VCE English Exam - Section C Student Revision Booklet
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Final Presentation General Medicine 03-08-2024.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Types and Its function , kingdom of life
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O5-L3 Freight Transport Ops (International) V1.pdf

#includeiostream#includestack#includestring #include .pdf

  • 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;
  • 5. 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{
  • 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;
  • 9. 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){
  • 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();
  • 12. 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 <