Skip to content

Commit

Permalink
separate comilation enabled
Browse files Browse the repository at this point in the history
added stack labels to output
  • Loading branch information
Redent0r committed Oct 13, 2017
1 parent 408a04b commit 5f9a77e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 34 deletions.
38 changes: 6 additions & 32 deletions hanoi.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
#include <hanoi.h>
#include <iostream>
#include <vector>
#include <stack>
#include <unordered_map>
#include <cstdlib> // exit, EXIT_FAILURE
#include <algorithm> // std::max
#include <iomanip> // std::setw, std::setfill

class Hanoi
{
public:
Hanoi(const unsigned int &);
void play();

private:
std::vector <std::stack<int>> stacks; // the 3 stacks
std::unordered_map <int, int> moves; // maps disks to their last position before current one
int lrud; // least recently used disk

unsigned int moveCounter; // tracks the number of moves
unsigned int disksWidth; // used to format output

void rHanoi(); // main, recursive function. Called by play
std::stack<int> & getFrom(); // rHanoi helper
std::stack<int> & getTo(std::stack<int> &); //rHanoi helper
void move(std::stack<int> &, std::stack<int> &); // called by rHanoi
void printStacks(); // called by move to output process
};

int main()
{
const unsigned int DISKS = 11;
Hanoi hanoi(DISKS);
hanoi.play();
}

Hanoi::Hanoi(const unsigned int & numberOfDisks):
stacks{3}, // 3 stacks
moves{numberOfDisks}, // moves is size n
Expand Down Expand Up @@ -109,6 +79,7 @@ void Hanoi::rHanoi()

void Hanoi::play()
{
std::cout << "\nBegin:\n";
printStacks();
move(stacks[0], stacks[2]); // first move
rHanoi();
Expand Down Expand Up @@ -160,7 +131,10 @@ void Hanoi::printStacks()

std::cout << '\n';
}
std::cout << '\n';
std::cout << std::internal <<std::setw(disksWidth + 2) << "s1"
<< std::setw(disksWidth + 4) << "s2"
<< std::setw(disksWidth + 4) << "s3";
std::cout << "\n\n";

}

Expand Down
29 changes: 29 additions & 0 deletions hanoi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <vector>
#include <stack>
#include <unordered_map>

#ifndef _HANOI_H
#define _HANOI_H

class Hanoi
{
public:
Hanoi(const unsigned int &);
void play();

private:
std::vector <std::stack<int>> stacks; // the 3 stacks
std::unordered_map <int, int> moves; // maps disks to their last position before current one
int lrud; // least recently used disk

unsigned int moveCounter; // tracks the number of moves
unsigned int disksWidth; // used to format output

void rHanoi(); // main, recursive function. Called by play
std::stack<int> & getFrom(); // rHanoi helper
std::stack<int> & getTo(std::stack<int> &); //rHanoi helper
void move(std::stack<int> &, std::stack<int> &); // called by rHanoi
void printStacks(); // called by move to output process
};

#endif
8 changes: 8 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include<hanoi.h>

int main()
{
const unsigned int DISKS = 3;
Hanoi hanoi(DISKS);
hanoi.play();
}
11 changes: 9 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
CC = g++ -std=c++11
DEBUG = -g
WARN = -Wall
INCLUDE = -I .

hanoi.x: hanoi.cpp
$(CC) $(WARN) hanoi.cpp -o hanoi.x
hanoi.x: hanoi.o main.o
$(CC) $(WARN) $(INCLUDE) hanoi.o main.o -o hanoi.x

main.o: main.cpp
$(CC) $(WARN) $(INCLUDE) main.cpp -c

hanoi.o: hanoi.h hanoi.cpp
$(CC) $(WARN) $(INCLUDE) hanoi.cpp -c

clean:
\rm *.o *~ hanoi.x

0 comments on commit 5f9a77e

Please sign in to comment.