⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ring.cpp

📁 VC++处学者可能用到的小程序,是著名的JOSEPHUS问题,书上已经有,但本人又编了一边
💻 CPP
字号:
//*********      ring.cpp      *********

# include <iostream>
# include <iomanip>
# include "ring.h"

using namespace std;

//定义构造函数
Ring::Ring (int n)
{
	pBegin = new Boy[n];
	pCurrent = pBegin;

	for (int i = 1; i <= n; i ++, pCurrent = pCurrent -> next)
	{
      pCurrent -> next = pBegin + i % n;
	  pCurrent -> code = i;
	  PutBoy();
	}
	cout << endl;
	pCurrent = &pBegin[n-1];
}


//定义Count函数
void Ring::Count (int m)
{
	for (int i = 0; i < m; i++)
	{
		pivot = pCurrent;
		pCurrent = pivot -> next;
	}
}


//定义PutBoy函数
void Ring::PutBoy()
{
	static int numInLine;
	if (numInLine++ % 10 == 0)
		cout <<endl;

	cout << setw(4) << pCurrent -> code;
}


//定义ClearBoy函数
void Ring::ClearBoy()
{
	pivot -> next = pCurrent -> next;
	pCurrent = pivot;
}


//析构函数
Ring::~Ring()
{
	delete[]pBegin;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -