约瑟夫环 c++ 循环输入

#include<iostream>
#include<string.h>
#include<cstdio>
#include <sstream>
using namespace std;
template <class T>
class joseph
{
    struct node
    {
        T data;
        node * next;
        node():next(NULL) {}
        node(T d):data(d),next(NULL) {}
    };
private:
    node * head;
    node * cur;
    node * pre;

public:
    joseph(T d);
    int len = 0;
    void setValue(T d)
    {
        node * tem = new node(d);
        cur ->next = tem;
        tem -> next = head;
        cur = cur -> next;
        pre = pre -> next;
        len++;
    }
    void out()
    {
        pre -> next = cur -> next;
        cout<<cur -> data<<" ";
        delete cur;
        cur = pre -> next;
        len--;
    }
    void nextmove()
    {
        cur  = cur -> next;
        pre = pre -> next;
    }
    void init()
    {
        cur = cur -> next;
        pre = pre -> next;
    }

};
//模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)
template <class Type>
Type stringToNum(const string& str)
{
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;
}

template <class T>
joseph<T> :: joseph(T d)
{
    head = new node(d);
    node * fz = new node();
    cur = head;
    pre = fz;
    pre -> next = head;
}
int main()
{
    string z,x;
    cout<<"please input n"<<endl;
    cout<<"please input m"<<endl;
    while(cin>>z>>x)
    {
        int a = 1 ;//为第一个节点赋值而创建的
        int j = 0 ;//j为n的输入判断因子,当j=1时说明输入的n不是int型
        int k = 0 ;//k为m的输入判断因子,当j=1时说明输入的n不是int型
        joseph<int> dusk(a);//创建第一个节点,并调用构造函数把第一个节点赋值为1
        dusk.len++;//链表长度加一
        int n , m ;//n为人数,m为密码
        int flag = 0;//是否继续
        for(int i = 0 ; i < z.length() ; i++)//判断n是不是int型
        {
            if(z[i]<'0'||z[i]>'9')
            {
                cout<<"n input error"<<endl;
                j = 1;
                flag = 1;
                break;
            }
        }
        n = stringToNum<int>(z);//调用函数把string型的n转换成int型
        for(int i = 0 ; i < x.length() ; i++)//判断m是不是int型
        {
            if(x[i]<'0'||x[i]>'9')
            {
                cout<<"m input error"<<endl;
                k=1;
                flag = 1;
                break;
            }
        }
        if(flag)
        {
            cout<<"please input n"<<endl;
            cout<<"please input m"<<endl;
            continue;
        }
        m = stringToNum<int>(x);//调用函数把string型的m转换成int型
        if(n == 1)
        {
            cout << 1 << endl;
            cout<<"please input n"<<endl;
            cout<<"please input m"<<endl;
            continue;
        }
        if(k==1||j==1)break;//判断因子有一个等于1说明:n,m有一个输入的不是int型,结束循环

        for(int i = 2 ; i <= n ; i++)//初始化赋值
        {
            dusk.setValue(i);
        }
        dusk.init();//把cur指针指向head,把pre的next指向cur
        while(dusk.len!=0)//长度不为0时循环
        {
            for(int i = 1 ; i < m ; i++)//移动
            {
                dusk.nextmove();
            }
            dusk.out();
        }
        cout<<endl;
        cout<<"please input n"<<endl;
        cout<<"please input m"<<endl;
    }

}

  

原文地址:https://www.cnblogs.com/Duskcl/p/3748009.html