1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
// stacks.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include<conio.h>
#include "stack.h"
using namespace std;
//------------------------Globals--------------------------------------------------------
int count= 0; //Counter to display the number of moves.
//---------------------------------------------------------------------------------------
void move( stack ,stack );
void move2( stack ,stack ) ;
void move3( stack ,stack ) ;
void move4( stack ,stack ) ;
void move5( stack ,stack ) ;
void move6( stack ,stack ) ;
int main()
{
stack source;// create 3 empty stacks
stack target;
stack aux;
char ss;
char ds;
int N;// Number of discs.
cout<<"\n**********************************************************\n";
cout<<"This C++ program is to solve the towers of hanoi problem";
cout<<"\n**********************************************************\n";
cout<<"Enter the no. of disks ";
cin>>N;//Input number of disks
cout<<endl;
while(N<0)//to make sure that the user didnot enter an invalid number
{
cout<<"The number you entered is invalid...Please reneter!"<<endl;
cin>>N;
cout<<endl;
}
for(int i=1; i<=N; i++)//Fill the source peg with the number of discs.
{
source.push(i);
}
cout<<"\nsource stack "<<endl;
if(aux.isempty())
{
cout<<" \t\tintermediate stack "<<endl;
}
if(target.isempty())
{
cout<<" \t\t\t\t\ttarget stack "<<endl;
}
while(count==0)
{
cout<<"\nenter source stack a, b or c"<<endl;
cin>>ss;
cout<<"\nenter destination stack a, b or c"<<endl;
cin>>ds;
if(ss=='a'&&ds=='c')
{
move3(source , target);
// element that will be removed from one stack and added to the other
//source.pop(N-1);//pop from source
//target.push(N);//and move to target
}
else if(ss=='a'&&ds=='b')
{
move(source, aux);
//source.pop(N-1);//pop from source
//aux.push(N);//and move to target
}
else if(ss=='b'&&ds=='c')
{
move2(aux, target);
//aux.pop(N-1);//pop from source
//target.push(N);//and move to target
}
else if(ss=='b'&&ds=='a')
{
move5(aux, source);
//aux.pop(N-1);//pop from source
//source.push(N);//and move to target
}else if(ss=='c'&&ds=='b')
{
move6(target, aux);
//target.pop(N-1);//pop from source
//aux.push(N);//and move to target
}else if(ss=='c'&&ds=='a')
{
move4(target, source);
//target.pop(N-1);//pop from source
//source.push(N);//and move to target
}
if(target.isfull()){
count=1;
}
else if(aux.isfull()){
count=2;}
//system("cls");
}
if(count==1){
cout<<"you win";
}
else if(count==2){
cout<<"try again";
}
getch();
return 0;
}
void move( stack s,stack t) //This will move an element from first stack to the second stack
{
int e; // element that will be removed from one stack and added to the other
s.pop(e);//pop from source
t.push(e);//and move to target
cout<<e<<endl;
}
void move2( stack t,stack d) //This will move an element from first stack to the second stack
{
int e; // element that will be removed from one stack and added to the other
t.pop(e);//pop from source
d.push(e);//and move to target
cout<<e<<endl;
}
void move3( stack s,stack d) //This will move an element from first stack to the second stack
{
int e; // element that will be removed from one stack and added to the other
s.pop(e);//pop from source
d.push(e);//and move to target
cout<<e<<endl;
}
void move4( stack d,stack s) //This will move an element from first stack to the second stack
{
int e; // element that will be removed from one stack and added to the other
d.pop(e);//pop from source
s.push(e);//and move to target
cout<<e<<endl;
}
void move5( stack t,stack s) //This will move an element from first stack to the second stack
{
int e; // element that will be removed from one stack and added to the other
t.pop(e);//pop from source
s.push(e);//and move to target
cout<<e<<endl;
}
void move6( stack d,stack t) //This will move an element from first stack to the second stack
{
int e; // element that will be removed from one stack and added to the other
d.pop(e);//pop from source
t.push(e);//and move to target
cout<<e<<endl;
}
|