Question: PLEASE HELP! Topic: Computer Science | C++ Programming IN C++ (TOWERS OF HANOI) Modify the Peg class to overload the < < operator, replacing the

PLEASE HELP!

Topic: Computer Science | C++ Programming IN C++ (TOWERS OF HANOI) Modify the Peg class to overload the << operator, replacing the Peg method displayPeg. Incorporate the new method into the Peg class, and delete the Peg::displayPeg() method. Do the same with the Stack and Node classes. In Stack, have the << operator output the stack from the oldest element to the newest. Modify the Hanoi() program to use the overloaded << method with cout. Your code should look something like: cout << peg1 << peg2 << peg3; The Peg class << overload method should include the line ostream << stack; The Stack class << overload method should include the line ostream << node; The Peg, Stack, and Node program files should #include instead of The following test code will be added at the beginning of main() during the grading process: // Node test code { const Node cliff(123, (Node *) 0x123456); cout << "Node cout (should be 123): " << cliff << endl; } // Stack test code { Stack cliff; cliff.push(123); cliff.push(456); cout << "Stack cout (should be 123 456): " << cliff << endl; const Stack cliff1; cout << "Stack cout (should be empty): " << cliff1 << endl; } // Peg test code { const Peg cliff("Cliff", 4); cout << "Peg cout (named Cliff, 4 disks): " << cliff << endl; } HERE IS MY CURRENT CODE: // Hanoi Peg Class.cpp : Defines the entry point for the console application. // Joyce Martinez // Author: Cliff Snyder (csnyder@everett.edu) // Description: Program that recursively solves the Towers of Hanoi // // Top Down Design // Initialize the simulation // Recursively solve Hanoi // Output the results #include "stdafx.h" #include #include #include "Peg.h" using namespace std; // Global types // Global consts const int NUM_DISKS(7); // Number of disks to simulate // Global function declarations // int hanoi(int numDisks, Peg &start, Peg &goal, Peg &temp); void moveDisk(Peg &from, Peg &to); int main() { // Node test code { const Node cliff(123, (Node *)0x123456); cout << "Node cout (should be 123): " << cliff << endl; } // Stack test code { Stack cliff; cliff.push(123); cliff.push(456); cout << "Stack cout (should be 123 456): " << cliff << endl; const Stack cliff1; cout << "Stack cout (should be empty): " << cliff1 << endl; } // Peg test code { const Peg cliff("Cliff", 4); cout << "Peg cout (named Cliff, 4 disks): " << cliff << endl; } Peg peg1("Peg1", NUM_DISKS), peg2("Peg2", 0), peg3("Peg3", 0); int numMoves(0); // Introduce the program cout << "Welcome to Cliff's Towers of Hanoi simulator...this simulation will be running with " << NUM_DISKS << " disks." << endl << endl; // Display the starting condition cout << "Starting condition of the three pegs:" << endl; cout << peg1 << endl; cout << peg2 << endl; cout << peg3 << endl; cout << endl; // Solve Towers of Hanoi - move the disks from peg 1 to peg 3, using peg2 temporarily, getting the number of required moves returned cout << "Moves required to move " << NUM_DISKS << " disks from " << peg1.getName() << " to " << peg3.getName() << ":" << endl; numMoves = hanoi(NUM_DISKS, peg1, peg3, peg2); // Display the ending condition cout << endl << "Ending condition of the three pegs:" << endl; cout << peg1 << endl; cout << peg2 << endl; cout << peg3 << endl; cout << endl; cout << endl << "A stack of " << NUM_DISKS << " disks can be transferred in " << numMoves << " moves." << endl << endl; cout << "Thanks for using Cliff's Towers of Hanoi simulator!" << endl; system("pause"); return 0; } //Functions // Hanoi - recursive solution to the tower of hanoi problem int hanoi(int numDisks, Peg &start, Peg &goal, Peg &temp) { int numMoves(0); // Only if there are disks to be moved if (numDisks > 0) { // First, move n-1 disks to the temporary peg, capturing the number of moves required numMoves = hanoi(numDisks - 1, start, temp, goal); // Next, move the remaining disk to the goal peg; increment the move count cout << "Move disk " << start.topDisk() << " from " << start.getName() << " to " << goal.getName() << endl; moveDisk(start, goal); numMoves++; // Finally, move n-1 disks to the goal peg, capturing the number of moves required numMoves += hanoi(numDisks - 1, temp, goal, start); } // Let the caller know how many moves were made return(numMoves); } //Function to move a disk from one peg to another void moveDisk(Peg &from, Peg &to) { assert(from.getNumDisks() > 0); // Verify assumption that the "from" peg has at least one disk on it if (to.getNumDisks() > 0) // Verify assumption that the diskon "from" is smaller than disk on "to" (only if there is a disk on "to") { assert(from.topDisk() < to.topDisk()); } // Place the top disk from "from" onto "to" to.addDisk(from.removeDisk()); } STACK.H #pragma once #include "Node.h" #include using namespace std; class Stack { private: Node *top; // Pointer to the top element unsigned int numNodes; void displayNodesHelper(Node *current) const; public: //Constructor Stack(); // push method void push(int newPayload); // pop method int pop(); // ACCESSORS // top accesor int readTop() const; // retrieve numbers of elements unsigned int getNumElements() const; // make sure theres no disks void clear(); // Display stack void reversePrint() const; // output stack friend ostream &operator << (ostream &output, const Stack &displayStack); // Deconstructor ~Stack(); }; STACK.CPP #include "stdafx.h" #include "Stack.h" #include #include using namespace std; // Constructor Stack::Stack() { top = nullptr; numNodes = 0; } void Stack::push(int newPayload) { top = new Node(newPayload, top); numNodes++; } int Stack::pop() { assert(top != nullptr); // Make sure stack isnt empty Node *tempPtr = top; int tempPayload = top->getPayload(); top = top->getNext(); delete tempPtr; numNodes--; return tempPayload; } ostream &operator << (ostream &output, const Stack &displayStack) { Node *current; current = displayStack.top; while (current != nullptr) { output << current->getPayload(); current = current->getNext(); } return output; } // Clear Stack void Stack::clear() { while (top != nullptr) { pop(); } } int Stack::readTop() const { assert(top != nullptr); return (top->getPayload()); } // retrieve numbers of elements unsigned int Stack::getNumElements() const { return (numNodes); } void Stack::displayNodesHelper(Node *current) const { if (current != nullptr) { displayNodesHelper(current->getNext()); const Node *node; } } void Stack::reversePrint() const { displayNodesHelper(top); } // Destructor Stack::~Stack() { clear(); } PEG.H // File Name: Peg.h // Author: Cliff Snyder (csnyder@everett.edu) // Joyce Martinez // Description: Declarations for the class Peg #pragma once #include #include #include "Stack.h" using namespace std; class Peg { private: static int numCreatedPegs; // Number of active peg objects string name; // Name of the peg Stack stack; // Structure to hold the disks // Private method to load numDisks disks onto a Peg void loadDisks(int numDisks); public: // Constructor (note the lack of a default constructor to prevent usage errors) Peg(string pegName, int numDisks); // Method to return the number of disks on the Peg unsigned int getNumDisks() const; // Accessor for the peg name string getName() const; // Method to return the value of the top disk int topDisk() const; // Methods to add and remove disks from the peg void addDisk(int diskNum); int removeDisk(); friend ostream &operator << (ostream &output, const Peg &displayPeg); // Destructor ~Peg(); }; PEG.CPP // File Name: Peg.cpp // Author: Cliff Snyder (csnyder@everett.edu) // Description: Definitions for the class Peg methods // #include "stdafx.h" #include #include #include "Peg.h" using namespace std; // Static Variables int Peg::numCreatedPegs = 0; // Constructors - default, with peg name, with # of disks, with peg name number of disks // Constructor - note that there is no default constructor to prevent mis-use of the Peg during initialization Peg::Peg(string inputName, int numDisks) { name = inputName; loadDisks(numDisks); numCreatedPegs++; } // Accessors / Mutators // Function to load disks onto the peg void Peg::loadDisks(int numDisks) { assert(numDisks >= 0); // no negative number of disks! stack.clear(); // Remove any existing disks for (int i = numDisks; i > 0; i--) // Add disks in inverse order (biggest to smallest) { addDisk(i); } } //Function to return the number of disks on the peg unsigned int Peg::getNumDisks() const { return stack.getNumElements(); } // Ostream friend operator ostream &operator << (ostream &output, const Peg &peg) { output << peg.name << " has " << peg.stack.getNumElements() << " disks: " << peg.stack << endl; return output; } // Accessor for the peg name (intentionally no mutator) string Peg::getName() const { return(name); } // Function to return the value of the top disk int Peg::topDisk() const { assert(getNumDisks() > 0); // Verify assumption that the Peg has a disk on it return (stack.readTop()); } // Functions to add and remove disks from the peg void Peg::addDisk(int diskNum) { stack.push(diskNum); } int Peg::removeDisk() { assert(getNumDisks() > 0); // Verify assumption that the Peg has a disk on it return stack.pop(); } // Destructor Peg::~Peg() { numCreatedPegs--; } NODE.H #pragma once #include using namespace std; class Node { private: static unsigned int numActiveNodes; int payload; Node *next; public: // Construtor Node(int newPayload, Node *newNext); //Accessors int getPayload() const; Node *getNext () const; // Mutators void setPayload(int newPayload); void setNext(Node *next); // Friend function friend ostream &operator << (ostream &output, const Node &node); // Deconstructor ~Node(); }; NODE.CPP // Joyce Martinez #include "stdafx.h" #include "Node.h" using namespace std; unsigned int Node::numActiveNodes = 0; // Constructors Node::Node(int newPayload, Node *newNext) { setPayload(newPayload); setNext(newNext); numActiveNodes++; } ostream &operator << (ostream &output, const Node &node) { output << node.payload; return output; } //Accessors int Node::getPayload() const { return (payload); } Node *Node::getNext() const { return (next); } // Mutators void Node::setPayload(int newPayload) { payload = newPayload; } void Node::setNext(Node *newNext) { next = newNext; } // Destructor Node::~Node() { numActiveNodes--; }

I'M ALMOST FINISHED I JUST TO: - IMPLEMENT THE COUT IN THE HANOI FUNCTION - FIGURE OUT WHY I USE THE STACK TEST CODE IT OUTPUTS THE NUMBERS BACKWARDS

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!