從面向結構到面向對象-----josephus問題(方法四:面向對象的實現)

今天看了一下josephus問題,突然有點想寫些東西的衝動,結合自己的部份思想,於是便寫了這幾篇帖子。因爲有幾篇代碼有點長,就分開發吧。如果對你有什麼幫助的話,本人勝感欣慰。也許你會說,這個問題好多書上都有代碼,但本人詣在於用不同的方法寫出,讓初學者體會一下從面向結構到面向對象的不同之處;同時你也可以看看我寫的和一些書中的不同之處。如果你是個大蝦,大可一笑了之,或賜教一番。
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />



josephus問題:幾個小孩圍成一圈,從任意一個小孩間隔m順時針方向數起,每數到第m個小孩時,該小孩就離開。最後一個剩下的就爲勝利者。第幾個爲勝利者?

 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//方法四:面向對象的實現
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//ring.h
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
struct Node
{
  int code;
  Node *next;
};

class Ring
{
  public:
    Ring(int n);
    ~Ring();
    void Progress(int m);
    void PrintNode();
    void ClearCode();
  protected:
    Node *head;
    Node *pivot;
    Node *pCur;
};


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//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 "//"
  }
  cout<<endl;
  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;

}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//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;
};
 

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//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; 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.PrintNode();
}

void Jose::Initial()
{
  int num,begin,m;
  cout<<"Please input the number of boys,Beginning position,innterbal per count"
      <<endl;
  cin>>num>>begin>>m;

  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;
  }

  numOfBoys=num;
  beginPos=begin;
  interval=m;
}

 

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//main.cpp
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include "Jose.h"

void main()
{
  Jose jose;
  jose.Initial();
  jose.GetWinner();
}
 

 
發佈了23 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章