实验一
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <time.h>
using namespace std;
struct Man
{
public:
int number,a;
int passwd;
int f_number;
};
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main()
{
int n, m,amount;
Man man[100] ;
unsigned int a = 2;
cout<<"=====请输入人数======"<<endl;
cin>>n;
cout<<"=====请输入初始密码=="<<endl;
cin>>m;
amount=n;
for(int i = 1; i <= n; i++)
{
man[i].f_number=i;
srand( (unsigned)i);
man[i].passwd=(int)rand();
man[i].number=i;
// cout<<"man["<< i <<" ].f_number="<<man[i].f_number<<" " ;
// cout<<"man["<< i <<" ].number ="<<man[i].number<<" ";
// cout<<"man["<< i <<" ] .passwd ="<<man[i].passwd <<endl;
}
for(int i=1;i<=n;i++)
{
m=m%amount; //圆 对密码进行处理对人数取余
cout<<"m="<<m<<endl;
if(m==0)
break;
for(int j = 1; j <= n; j++)
{
if(m == man[j].number )
{
cout<<"第"<<i<<"轮"<<man[j].f_number<<"号出局"<<endl; //输出这个人的序号;
m=man[j].passwd; //改变密码 ;
int a;
a = j;
int z=n;
for(z;z > a;z--)
{
man[z].number = man[z-1].number; //重新生成一条
}
man[a].number = -1; //出局
amount = amount-1; //重置链的长度
} //计数器清零
}
}
return 0;
}
实验二
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <time.h>
using namespace std;
#define stack_init_size 100 //栈的大小
#define ok 1
#define error 0
#define STAK_INCREASEMENT 1
typedef char selem_type; //存贮的数据类型
typedef char status; //函数返回的数据类型
class stack
{
public:
selem_type *base =NULL;
selem_type *top =NULL ;
int stacksize ;
selem_type test_char,popchar;
status init_stack() ;
status push(selem_type e) ;
status pop() ;
status clearstack() ;
status compete (selem_type popchar,selem_type test_char);
status show();
}
status stack::init_stack() //栈的初始化
{
base = (selem_type *)malloc( stack_init_size * sizeof(selem_type)); //申请空间
if(!base)
return error; //报错
top = base ; // top=base
stacksize = stack_init_size; //设置栈的大小
return ok;
} //建栈成功
status stack::push(selem_type e)
{
if( (top - base) >= stacksize )
{
base = (selem_type *)realloc( base, (stacksize + STAK_INCREASEMENT ) * sizeof(selem_type));
// stacksize += STAK_INCREASEMENT;
if(!base)
return error;
}
*top = e;
top++;
// cout<<"已存入符号栈中" <<endl;
return ok;
}
status stack::pop()
{ selem_type e;
if(base == top)
{
cout<<"空栈,没有符号与之匹配"<<endl;
return error;
}
top--;
e = *top;
return e;
}
status stack::clearstack ( )
{
top = base ;
base = (selem_type *)realloc(base,stack_init_size * sizeof(selem_type)) ;
stacksize = 100 ;
return ok;
}
status stack::compete (selem_type popchar,selem_type test_char)
{
if ( popchar == '(' )
{
switch(test_char)
{
case ')' : cout<<"right"<<endl; break;
case ']' : cout<<"error,bacause "<<test_char<&