Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify your Towers of Hanoi program to overload the < < operator in the Peg class, replacing the Peg method displayPeg. Incorporate the new method

Modify your Towers of Hanoi program to overload the << operator in the Peg

class, replacing the Peg method displayPeg. Incorporate the new method into the

Peg class, and delete the Peg::displayPeg() method.

Do the same with your Stack and Node classes. In Stack, have the << operator

implement the stack from the bottom up (reverse order)

Modify your Hanoi() program to use the overloaded << method with cout. Your

code should look something like:

cout << peg1 << peg2 << peg3;

Your Peg class code should be able to say

cout << stack;

Your Stack class code should be able to say

cout << node;

The format for << in node and stack will be very similar to Peg (except in Peg you'll be outputting the stack (which you aren't doing)), in Stack you'll be outputting the nodes in reverse order, and in Node you'll be outputting the payload. I'm not sure where to go from where I am out, but I know I can't be for off. Looking for some direction and suggestions.

Below are my files for the project:

MAIN

// File Name: Hanoi.cpp // Author: Tanner Crane // Description: Program that recursively solves the Towers of Hanoi with a stack and node class.

#include #include #include "Peg.h"

using namespace std;

const int NUM_DISKS(7); // Number of disks to simulate

int hanoi(int numDisks, Peg& start, Peg& goal, Peg& temp); void moveDisk(Peg& from, Peg& to);

int main() { // Initialize variables Peg peg1("Peg1", NUM_DISKS), peg2("Peg2"), peg3("Peg3"); int numMoves(0);

// Introduce the program

cout << "Welcome to Tanner's Tower 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 << peg2 << peg3; cout << endl;

// Solve Tower 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); // Move disks from peg 1 to peg 3 using peg 2

// // Display the ending condition cout << endl << "Ending condition of the three pegs:" << endl; cout << peg1 << peg2 << peg3; cout << endl; cout << endl;

cout << endl << "A stack of " << NUM_DISKS << " disks can be transferred in " << numMoves << " moves." << endl << endl;

cout << "Thanks for using Tanner's Tower of Hanoi simulator! " << endl; system("PAUSE"); return 0; }

// 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); }

return(numMoves); } // Function to move a disk from one peg to another void moveDisk(Peg& from, Peg& to) { // Place the top disk from "from" onto "to" to.addDisk(from.topDisk()); // Remove the top disk from "from" from.removeDisk(); }

NODE.H

#pragma once class Node { private: // Private Attributes int payload; // Holds the dataNode Node* next; // Pointer to next public: // Constructor Node(int newPayload, Node* newNext); // Accessors int getPayload() const; Node* getNext() const; // Mutators void setNext(Node* next); void setPayload(int newPayload); friend ostream& operator <<(ostream& outStream, const Node& node); // Destructor ~Node(); };

NODE.CPP

#include "Node.h" //Definitions for Node

Node::Node(int newPayload, Node* newNext) // constructor { payload = newPayload; next = newNext; } ostream& operator << (ostream& outStream, Node& node); { } Node* Node::getNext() const { return next; } void Node::setNext(Node* newNext) // assigning new node via newNext { next = newNext; } void Node::setPayload(int newPayload) // updating the value in payload with newPayload (integer value) { payload = newPayload; }

Node::~Node() //Destructor {

} PEG.H

// Peg.h by Tanner Crane #pragma once #include #include "Stack.h" #include "Node.h"

using namespace std; class Peg { private: void loadDisk(int numDisks); Stack stack; // This is the structure we are using to hold the disks instead of vector string pegName; // The name of the peg public: Peg(string newName, int numDisks = 0); // Class Constructor int getSize() const; void setName(string newName); /*void printPeg() const;*/ // method to display what disks are on the peg string getName() const; // accessor for the peg name int topDisk() const; // method to return the value of the top disk // methods to add and remove disks from the pegs int removeDisk(); void addDisk(int const topDisk);; friend ostream& operator <<(ostream& outStream, const Peg& peg); // destructor ~Peg(); };

PEG.CPP

#include "Peg.h" #include #include #include #include "Stack.h" #include "Node.h" using namespace std; Peg::Peg(string newName, int numDisks) { setName(newName); loadDisk(numDisks); } void Peg::loadDisk(int numDisks) { // Load disks onto the pegs assert(numDisks >= 0); // Assume there is no negative disks for (int i = numDisks; i > 0; i--) { stack.push(i); }

} // accessors string Peg::getName() const { return pegName; }

int Peg::topDisk() const // return the value of the top disk on the peg { assert(stack.getNumNodes() > 0); return stack.readTop(); } int Peg::getSize() const { return stack.getNumNodes(); // size of the stack }

//Mutators

void Peg::setName(string newName) { pegName = newName; }

ostream& operator << (ostream & outStream, const Peg & peg) { outStream << peg.getName() << " has " << peg.getSize() << " disks: " << peg.getSize() << endl; return(outStream); }

void Peg::addDisk(int const topDisk) //& removed {

stack.push(topDisk); }

int Peg::removeDisk() { assert(stack.getNumNodes() > 0); int removedDisk = stack.readTop(); stack.pop();

return removedDisk;

}

//Peg destructor Peg::~Peg() { }

STACK.H

#pragma once #include "Node.h" class Stack { private: Node* top; // Pointer to the top node unsigned int numNodes; //void displayReverse(Node* temp); public: // Constructor Stack(); // Push-Pop methods void push(int newPayload); int pop(); // Top accessor int readTop() const; // Num Elements accessor unsigned int getNumNodes() const; // Display the stack void displayStackReverse(); void displayReverse(Node* temp); friend ostream& operator <<(ostream& outStream, const Stack& stack);

// Destructor ~Stack(); };

STACK.CPP

#include "Stack.h" #include "Node.h" #include #include using namespace std;

Stack::Stack() { top = nullptr; numNodes = 0; }

void Stack::push(int newPayload) { top = new Node(newPayload, top); numNodes++; }

int Stack::pop() { assert(top != nullptr); int tempPayload = top->getPayload(); Node* temp = top; top = top -> getNext(); delete temp; numNodes--; return tempPayload; } // //void Stack::displayStack() const //{ // // Node* crawler = top; // while (crawler != nullptr) // { // cout << crawler->getPayload(); // crawler = crawler->getNext(); // } // }

int Stack::readTop() const { assert(top != nullptr); return top->getPayload(); }

unsigned int Stack::getNumNodes() const { return numNodes; }

Stack::~Stack() // Stack destructor { while (top != nullptr) { pop(); } }

ostream& operator << (ostream& outStream, Stack& stack) {

outStream << stack.displayReverse(); return(outStream); }

void Stack::displayReverse(Node* temp) { if (temp != nullptr) {

displayReverse(temp->getNext()); cout << temp->getPayload(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions