Use only C++ codes please. please Help this, it has some codes. I need some help
ID: 658547 • Letter: U
Question
Use only C++ codes please.
please Help this, it has some codes. I need some help to fill these codes.
Static member can be used.
In this project, you will design and implement a class called towers, which is part of a program that lets a child play a game called Towers of Hanoi. The game consists of three pegs and a collection of rings that stack on the pegs. The rings are different sizes. The initial configuration for a five-ring game, with the first tower having rings ranging in size from one inch (on the top) to five inches (on the bottom).
The rings are stacked in decreasing in decreasing order of their size, and the second and third towers are initially empty. During the game, the child may transfer rings one at a time from the top of one peg to the top of another. The object of the game is to move all the rings from the first peg to the second peg. The difficulty is that the child may not place a ring on top of one with a smaller diameter. There is the one extra peg to hold rings temporarily, but the prohibition against a larger ring on a smaller ring applies to it as well as to the other two pegs.
The towers class must keep track of the status of all three pegs. You might use an array of three pegs, where each peg is an object from the previous project. The towers functions are specified here:
towers::towers(size_t n = 64);
// Precondition: 1 <= n < 64.
// Postcondition: The towers have been initialized
// with n rings on the first peg and no rings on
// the other two pegs. The diameters of the first
// peg
Explanation / Answer
#include<iostream>
#include<stdlib.h>
using namespace std;
void tower(int num,char Beg,char Aux,char End)
{
if(num>=1)
{
tower(num-1,Beg,End,Aux);
cout<<"Move disk "<<num<<" from "<<Beg<<" to "<<End<<endl;
tower(num-1,Aux,Beg,End);
}
}
int main()
{
int N;
cout<<"Enter the number of disk: ";
cin>>N;
tower(N,'B','A','E');
}
********************************
#define MAXSTACK 100
struct details
{
int number;
char beg;
char end;
char aux;
};
struct stack
{
int top;
struct details item [MAXSTACK];
};
void push(struct stack *s, struct details *current)
{
if(s->top==MAXSTACK-1)
{cout<<"Overflow";
exit(1);
}
else
s->item[++(s->top)]= *current;
return;
}
void popandtest(struct stack *s, struct details *current,int *flag)
{
if(s->top==-1)
{
*flag=1;
return;
}
*flag=0;
*current= s->item[s->top--];
return;
}
//towers of hanoi without recursion
void towers(int n, char begpeg, char endpeg, char auxpeg)
{
struct stack s;
struct details current;
int flag;
char temp;
s.top=-1;
curr.num=n;
curr.end=endpeg;
curr.beg=begpeg;
curr.aux=auxpeg;
while(1)
{
while(curr.num!=1)
{
push(&s,¤t);
--curr.num;
temp=curr.end;
curr.end=curr.aux;
curr.aux=temp;
}
cout<<"Move Disc 1 from "<<curr.beg<<" to "<<curr.end<<endl;
popandtest(&s,¤t,&flag);
if(flag==1)
return;
cout<<"Move Disc "<<curr.num<<" from "<<curr.beg<<" to "<<curr.end<<endl;
--curr.num;
temp=curr.beg;
curr.beg=curr.aux;
curr.aux=temp;
}
}
int main()
{
int N;
cout<<"Enter the number of disk: ";
cin>>N;
towers(N,'B','E','A');
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.