65,186
社区成员




/*******************JOSEPH.CPP*********************/
#include<string>
using namespace std;
//异常
class NullLocationPointer {
private:
string info;
public:
NullLocationPointer():info("Location pointer is NULL.") {}
string& what() {return info;}
};
//采用单循环链表
//约定头结点为0号位置
//节点类
template<class T>
class Node {
friend class CircChain<T>;
friend class Iterator<T>;
private:
T data;
Node<T> *next;
public:
Node() {next=0;}
};
//单循环链表
template<class T>
class CircChain {
friend class Iterator<T>;
private:
Node<T> *head,*tail; //头结点尾结点
int size; //链表长度
public:
CircChain(const T *tArray,int n=10); //仅仅专门针对这个问题方便起见,传个数组来构造单循环链表
~CircChain();
int getSize() const {return size;} //获取表长
};
//迭代器
template<class T>
class Iterator {
private:
CircChain<T> &backup_target; //表示某个迭代器指向的是哪个链表对象
Node<T> *before_location;
Node<T> *location; //迭代器指向的结点
public:
Iterator(const CircChain<T> &x):backup_target(x),location(x.head) {
before_location=location;
}
void next() {} //向后挪
void toHead() {} //挪到头结点处
void refresh() {} //指向另一个链
T getData() const throw NullLocationPointer {} //获取数据
void del() {} //删除当前指向的结点
};
//主函数
int main() {
int a[]={1,2,3,4,5};
CircChain<int> c(a,5);
return 0;
}
/***************纯属试一试.CPP****************/
template<class T>
class Iterator {
private:
T location;
};
template<class T>
class Chain {
friend class Iterator<T>;
private:
T head;
};
template<class T>
class Node {
friend class Iterator<T>;
friend class Chain<T>;
private:
T next;
};
int main() {
Chain<int> c;
return 0;
}
/*******************JOSEPH.CPP*********************/
#include<string>
using namespace std;
//异常
class NullLocationPointer {
private:
string info;
public:
NullLocationPointer():info("Location pointer is NULL.") {}
string& what() {return info;}
};
//采用单循环链表
//约定头结点为0号位置
//节点类
template<class T>
class Node {
friend class CircChain; //这里不需要<T>
friend class Iterator; //同样
private:
T data;
Node<T> *next;
public:
Node() {next=0;}
};
//单循环链表
template<class T>
class CircChain {
// friend class Iterator<T>; 多余
private:
Node<T> *head,*tail; //头结点尾结点
int size; //链表长度
public:
CircChain(const T *tArray,int n=10); //仅仅专门针对这个问题方便起见,传个数组来构造单循环链表
~CircChain();
int getSize() const {return size;} //获取表长
};
//迭代器
template<class T>
class Iterator {
private:
CircChain<T> &backup_target; //表示某个迭代器指向的是哪个链表对象
Node<T> *before_location;
Node<T> *location; //迭代器指向的结点
public:
Iterator(const CircChain<T> &x):backup_target(x),location(x.head) {
before_location=location;
}
void next() {} //向后挪
void toHead() {} //挪到头结点处
void refresh() {} //指向另一个链
T getData() const throw NullLocationPointer {} //获取数据
void del() {} //删除当前指向的结点
};
//主函数
int main() {
int a[]={1,2,3,4,5};
CircChain<int> c(a,5);
return 0;
}
//假设有n个人团团围做,从第1个人开始数数,数到第m个人时候,第m个人出列,
//然后继续从1开始数数,数到第m个人退出
#include <stdio.h>
#include <conio.h>
int i,k,t;
int n,m;
static char f[1001];//0该座位未出圈,1该座位已出圈
void main() {
while (1) {
printf("Input n m(1000>=n>=m>=1):");
fflush(stdout);
rewind(stdin);
if (2==scanf("%d%d",&n,&m)) {
if (1000>=n && n>=m && m>=1) break;
}
}
t=0;//已出圈总人数
i=1;//座位编号
k=1;//当前要数的数
while (1) {
if (0==f[i]) {
if (m==k) {
t++;
f[i]=1;
printf("%3d ",i);
if (0==t%10) printf("\n");
if (t>=n) break;
}
k++;if (k>m) k=1;
}
i++;if (i>n) i=1;
}
cprintf("Press any key ...");
getch();
}