From the structure-oriented object ----- josephus problem (method 4: object-oriented implementation)

xiaoxiao2021-03-06  40

Today, I saw Josephus questions, suddenly I want to write some things, combined with my own thoughts, so I wrote these posts. Because there are a few code a bit long, I will be developed. If you have any help you have, I am gratifying. Maybe you will say that there is a lot of books in this question, but I am writing in different ways, so that beginners will experience the difference between the facing-oriented object; and you can also see what I wrote. And the differences in some books. If you are a prawn, you can smile, or enlighten me.

Josephus question: Several children are enclosed in a circle, from any child spacing M. The child will leave when every time to M-child, the child leaves. The last thing left is a winner. The first few is the winner?

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Method 4: Object-oriented implementation

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//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 #include #include "ring.h"

Ring :: rG (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 = 0; i 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 number; int start: int number;

/ / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ / Jose.cpp//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ # include #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> 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

Void Jose :: Initial () {Int Num, Begin, M; COUT << "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.initial (); jose.getwinner ();

转载请注明原文地址:https://www.9cbs.com/read-71350.html

New Post(0)