Question

In: Computer Science

Tower of Hanoi problem complete the problems please use this code #pragma once #include <iostream> #include...

Tower of Hanoi problem
complete the problems
please use this code


#pragma once

#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class TowersOfHanoi
{
friend ostream& operator<<(ostream& sink, const TowersOfHanoi& towers);
public:
//constructor
TowersOfHanoi();
//custom methods
unsigned move(unsigned new_n_disks = 6, ostream& new_sink = cout);
private:
//custom methods
void move_aux(ostream& sink, unsigned n_disks, unsigned srce, unsigned dest, unsigned aux);
//temporary variable
unsigned _n_moves;
//data
const unsigned _n_towers;
stack<unsigned>* _tower;
};


#include "TowersOfHanoi.h"
//
ostream& operator<<(ostream& sink, const TowersOfHanoi& hanoi)
{
for (unsigned index = 0; index < hanoi._n_towers; index++)
{
sink << index + 1 << ": ";
stack<unsigned> temp;
while (!hanoi._tower[index].empty())
{
unsigned top = hanoi._tower[index].top();
hanoi._tower[index].pop();
temp.push(top);
}
while (!temp.empty())
{
unsigned top = temp.top();
temp.pop();
hanoi._tower[index].push(top);
sink << top << ' ';
}
sink << endl;
}
sink << "---" << endl;
return sink;
}
TowersOfHanoi::TowersOfHanoi(void)
:_n_towers(3)
{
_tower = NULL;
_n_moves = 0;
}
unsigned TowersOfHanoi::move(unsigned n_disks, ostream& sink)
{
if(n_disks < 1)
throw string("TowersOfHanoi:move - new-n-disks is 0 or negative");

_tower = new stack<unsigned>[_n_towers];
for(unsigned disk = n_disks; disk > 0; disk--)
_tower[0].push(disk);
  
_n_moves = 0;

sink << *this << endl;
move_aux(sink, n_disks, 0, 2, 1);
delete [] _tower;
return _n_moves;
}
void TowersOfHanoi::move_aux(ostream& sink, unsigned n_disks, unsigned srce, unsigned dest, unsigned aux)
{
if(n_disks > 1)
{
//move n -1 disks from source to auxilliary
//move_ 1 disk from source to destination
//mov n -1 disks from axuilliary to destination
}
else
{
unsigned top = _tower[srce].top();
_tower[srce].pop();
_tower[dest].push(top);
_n_moves++;
sink << *this << endl;
}
}


// testTowersOfHanoi.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <string>
#include "towersofhanoi.h"
using namespace std;

int main()
{
try
{
TowersOfHanoi towers;
for (unsigned n_disks = 1; n_disks <= 10; n_disks++)
{
cout << towers.move(n_disks) << " moves " << endl;
system("pause");
cout << endl << endl;
}
}
catch(string message)
{
cout << message << endl;
}
catch(...)
{
cout << "something is dreadfully wrong" << endl;
}
system("pause");
return 0;
}

Solutions

Expert Solution


Only 1 file has changed and I have given you the updated the changed file. Please use other files as is.
Please do rate the answer if it helped. Thank you.

TowersOfHanoi.cpp
------------------

#include "TowersOfHanoi.h"
//
ostream& operator<<(ostream& sink, const TowersOfHanoi& hanoi)
{
for (unsigned index = 0; index < hanoi._n_towers; index++)
{
sink << index + 1 << ": ";
stack<unsigned> temp;
while (!hanoi._tower[index].empty())
{
unsigned top = hanoi._tower[index].top();
hanoi._tower[index].pop();
temp.push(top);
}
while (!temp.empty())
{
unsigned top = temp.top();
temp.pop();
hanoi._tower[index].push(top);
sink << top << ' ';
}
sink << endl;
}
sink << "---" << endl;
return sink;
}
TowersOfHanoi::TowersOfHanoi(void)
:_n_towers(3)
{
_tower = NULL;
_n_moves = 0;
}
unsigned TowersOfHanoi::move(unsigned n_disks, ostream& sink)
{
if(n_disks < 1)
throw string("TowersOfHanoi:move - new-n-disks is 0 or negative");

_tower = new stack<unsigned>[_n_towers];
for(unsigned disk = n_disks; disk > 0; disk--)
_tower[0].push(disk);
  
_n_moves = 0;

sink << *this << endl;
move_aux(sink, n_disks, 0, 2, 1);
delete [] _tower;
return _n_moves;
}
void TowersOfHanoi::move_aux(ostream& sink, unsigned n_disks, unsigned srce, unsigned dest, unsigned aux)
{
if(n_disks > 1)
{
//move n -1 disks from source to auxilliary
move_aux(sink, n_disks-1, srce, aux, dest);
//move_ 1 disk from source to destination
move_aux(sink, 1, srce, dest, aux);
//mov n -1 disks from axuilliary to destination
move_aux(sink,n_disks-1, aux,dest, srce);
}
else
{
unsigned top = _tower[srce].top();
_tower[srce].pop();
_tower[dest].push(top);
_n_moves++;
sink << *this << endl;
}
}


Related Solutions

Tower of Hanoi problem please use this code #pragma once #include #include #include #include using namespace...
Tower of Hanoi problem please use this code #pragma once #include #include #include #include using namespace std; class TowersOfHanoi { friend ostream& operator<<(ostream& sink, const TowersOfHanoi& towers); public: //constructor TowersOfHanoi(); //custom methods unsigned move(unsigned new_n_disks = 6, ostream& new_sink = cout); private: //custom methods void move_aux(ostream& sink, unsigned n_disks, unsigned srce, unsigned dest, unsigned aux); //temporary variable unsigned _n_moves; //data const unsigned _n_towers; stack* _tower; }; #include "TowersOfHanoi.h" // ostream& operator<<(ostream& sink, const TowersOfHanoi& hanoi) { for (unsigned index =...
Tower of Hanoi - Java Code Use three rods labeled A, B, and C Use three...
Tower of Hanoi - Java Code Use three rods labeled A, B, and C Use three discs labels 1 (smallest), 2 (medium size), and 3 (largest disc) The program prompts you to enter the starting rod (A, B, or C) The program prompts you to enter the ending rod (A, B, or C, but cannot be same rod as entered as starting rod) The starting rod has discs 1, 2, 3, where 1 is on top of 2 is on...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h>...
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h> using namespace std; struct ListNode { int data; ListNode *next; ListNode(int x) : data(x), next(nullptr) {} }; class LinkedList { private: ListNode *head = nullptr; public: void addNode(int x) { ListNode *p = new ListNode(x); if (head == nullptr) head = p; else { ListNode *q = head; while (q->next != nullptr) q = q->next; q->next = p; } } void display() { ListNode...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include...
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include "sudoku.h" using namespace std; int main() { cout << "See Programming Exercise 18." << endl; } sudoku.cpp #include <iostream> #include "sudoku.h" using namespace std; sudoku::sudoku() { cout << "Write the definition. ." << endl; } sudoku::sudoku(int g[][9]) { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid() { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid(int g[][9]) { cout << "Write...
please use linux or unix to complete, and include pictures of the output. Modify the code...
please use linux or unix to complete, and include pictures of the output. Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the...
Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h>...
Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h> #include <semaphore.h> #include <pthread.h> using namespace std; #define NRO 6 // NĂºmero de coches //Puente declarado con matriz y valor entero void Puente(string array, int value); // Variable global int Norte = 1; int Sur = 1; sem_t mutex1; //Coche al norte void* NorteC(void* arg){ sem_wait(&mutex1); string array = "En el lado Norte "; // Norte cout<<array<<"del puente, el coche #"<<Norte<<" puede cruzar el...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std;...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std; int main() { enum AcceptedCoins {ADD_QUARTER, ADD_DIME, ADD_NICKEL, ADD_UNKNOWN}; AcceptedCoins amountDeposited = ADD_UNKNOWN; int totalDeposit = 0; int usrInput = 0; cout << "Add coin: 0 (add 25), 1 (add 10), 2 (add 5). "; cin >> usrInput; if (usrInput == ADD_QUARTER) { totalDeposit = totalDeposit + 25; } /* Your solution goes here */ else { cout << "Invalid coin selection." << endl;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT