//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//ring.cpp
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <iostream.h>
#include <iomanip.h>
#include "ring.h"
Ring::Ring(int n)
{
head = new Node[n];
pCur = head;
for (int i=1; i<=n; i++,pCur=pCur->next)
{
pCur -> code = i;
pCur -> next = head + (i % n);
// PrintNode();//if you want to look the boy who was you input,you can live out "//"
}
Display();
pCur = &head[n-1];
}
Ring::~Ring()
{
delete []head;
}
void Ring::Progress(int m)
{
for (int i=0; i<m; i++)
{
pivot = pCur;
pCur = pCur -> next;
}
}
void Ring::PrintNode()
{
static int lineCount;
if (((lineCount++) % 10) == 0)
cout<<endl;
cout << setw(4)
<< pCur -> code;
}
void Ring::ClearCode()
{
pivot->next=pCur->next;
pCur=pivot;
}
void Ring::Display()
{
Node *pB=pCur;
do{
PrintNode();
pCur=pCur->next;
}while(pB!=pCur);
cout<<endl;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//jose.h
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Jose
{
public:
Jose(int boys=10, int begin=1, int m=3);
void Initial();
void GetWinner();
protected:
int numOfBoys;
int beginPos;
int interval;
int wins;
};
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Jose.cpp
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <iostream.h>
#include "ring.h"
#include "Jose.h"
Jose::Jose(int boys , int begin, int m)
{
if (boys < 1)
{
cout << "Bad number of boys!/n";
return;
}
if (begin < 0)
{
cout << "Bad beginning position!/n";
return;
}
if ((m<1) || (m>boys))
{
cout << "Bad interval number!/n";
return;
}
numOfBoys = boys;
beginPos = begin;
interval = m;
}
void Jose::GetWinner()
{
Ring x(numOfBoys);
x.Progress(beginPos);
for (int i=1; i<numOfBoys-wins+1; i++)
{
x.Progress(interval);
//x.PrintNode(); //if you want to look the boy who is out,you can live out "//"
x.PrintNode();
x.ClearCode();
}
cout<<"/nThe winner is:";
x.Display();
}
void Jose::Initial()
{
int num,begin,m,w;
cout<<"Please input the number of boys:"
<<endl
<<"Beginning position,/n innterbal per count"
<<endl
<<"number of winners:"
<<endl;
cin>>num>>begin>>m>>w;
if(num<2)
{
cerr<<"bad number of boys"
<<endl;
return;
}
if(begin<0)
{
cerr<<"bad beginning position."
<<endl;
return;
}
if(m<1||m>num)
{
cerr<<"bda interval number."
<<endl;
}
if(w<1||w>=num)
{
cerr<<"bad number of winners.";
return;
}
numOfBoys=num;
beginPos=begin;
interval=m;
wins=w;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//main.cpp
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "Jose.h"
void main()
{
Jose jose;
jose.Initial();
jose.GetWinner();
}