C++笔记2

1.Josephus数小孩

#include 
#include
using namespace std;
typedef struct Node
{
    int data;
    struct Node *next;
}Node,*List;
class Ring//环链表
{
private:
    List First;
    int Count;
    int Current;
    //List Begin;
public:
    void Print();
    void Clear(){};
    void Creatlist(int n,int m,int z);
    void Pivot();
};
void Ring::Creatlist(int n,int m,int z)
{
    List p;
    int i;
    First=(Node*)malloc(sizeof(Node));
    if(!First)
    {
        cout<<"memory allocation error!\n";
        return ;
    }
    First->data=1; First->next=First;
    for(i=n;i>1;--i)
    {
        p=(Node*)malloc(sizeof(Node));
        if(!p)
        {
            cout<<"memory allocation error!\n";
            return;
        }
        p->data=i; p->next=First->next; First->next=p;
    }
    Count = n;
    Current = m;
    if(z<1)return;
    for(int j = 0;jnext;
    }
}
void Ring::Print()
{
    List p=First;
    do
    {
        cout<data<<" ";
        p=p->next;
    }while(p!=First);
    cout<next;
        }
        cout<<"出列的人是"<data<next=p->next; free(p);
        p=pre->next;
    }
    cout<<"The winner is "<data<>Boynumber>>Inteval>>Beginpos;
    if(Inteval < 2)Inteval = 2;
    Head.Creatlist(Boynumber,Inteval,Beginpos);
}
void Josephus::Getwinner()
{
    Head.Pivot();
}
void Josephus::Output()
{
    Head.Print();
}
int main()
{
Josephus jose;
jose.Inital();
jose.Output();
jose.Getwinner();
return 0;
}

2.静态成员

#include
using namespace std;
class Student
{
    static int num;
     int id;
public:
    Student()
    {
        num++;
    }
    ~Student()
    {
        num--;
    }
    int getid()//普通函数可以操作普通成员和静态成员,静态函数只能操作静态成员 
    {
        id=0;
        return num;
    }
     int getnum()
    {
        return num;
    }
};
int Student::num=0;
int main()
{
    Student t1;
    Student t2;
    Student *t3=new Student;
    cout<<"student num1="<

你可能感兴趣的:(C++笔记2)