您的位置:首页 > 理论基础 > 数据结构算法

(c++)数据结构与算法之链表线性表的应用:银行叫号系统

2017-12-07 20:29 465 查看
#include <iostream>
#define D 4                 //每个人业务处理时间
#define T 2                 //入队时间间隔
#define ST 12               //按要求模拟12秒(共7次)的银行叫号;
//共有3个普通窗口(n1,n2,n3),一个vip(v1),一个对公(b1);
using namespace std;
class user                                  //对象:客户
{
private:
int id;                             //编号,区分不同的用户
int IsWait;                         //是否在等待:是(1)  否(1)
int ArriveTime,ServeTime;           //到达开始服务时间(不确定),  服务时间统一为4s
char type;                          //用户类型  普通:n;  对公:b;    VIP:v;
user * next;                        //用于排队的结点
public:
user()
{
id=IsWait=0;
ArriveTime=-1;
ServeTime=D;
next=NULL;
}
user(char stype,int sid)          //stype顾客类型;sid顾客;
{
id=sid;
type=stype;
IsWait=0;
ArriveTime=-1;
ServeTime=D;
next=NULL;
}
void set_arrivtime(int sArrivetime)
{
ArriveTime=sArrivetime;         //MMP
}
char get_type()
{
return type;
}
int get_id()
{
return id;
}
user * get_next()
{
return next;
}
int get_finish_time()              //返回服务结束的时间
{
return ArriveTime+4;
}
void Clear()
{
id=IsWait=0;
ArriveTime=-1;
ServeTime=-1;
next=NULL;
}
};
struct queue_node                           //用于队列的结点node
{
user User;
queue_node * next;
};
typedef queue_node node;
class Queue                                 //不同类型客户有不同的队列:三条队列(n,b,v)
{
private:
bool empty;
char type_queue;
node *front;
node *rear;
public:
friend class user;              //为什么声明了友元类函数还是不能访问私有数据
Queue()
{
empty=true;
type_queue='\0';
front=rear=NULL;
}
bool Inqueue(user u)
{
node *n=new node;
n->User=u;
n->next=NULL;
if(n->User.get_type()==type_queue)
{
cout<<"排队类型错误"<<endl;
return false;
}
//p->next=n.next;        //为什么声明了友元类函数还是不能访问私有数据
if(empty)
{
front=n;
rear=n;
empty=false;
}
else
{
rear->next=n;
rear=n;
}
//if(front==NULL&&rear==NULL)     empty=true;
//else    empty=false;
return true;
}
user outqueue()
{
user out_user=front->User;

//front=front->next;
if(front->next==NULL)
{
front=rear=NULL;
}
else
{
front=front->next;
}
//        cout<<"queue is empty"<<endl;
//        node *p=front->next;
//        front=p;
if(front==NULL&&rear==NULL)     empty=true;
else    empty=false;
return out_user;
}
bool isempty()
{
return empty;
}
};
class bank_window                           //对象:银行窗口
{
private:
int id;                         //窗口编号
char type;                      //窗口类型: 普通:n;  对公:b;    VIP:v;
user client;                    //使用的客户
bool isbusy;                    //是否使用
public:
friend class user;
bank_window()
{
id=-1;
type='\0';
client.set_arrivtime(-1);
isbusy=false;
}
bool isBusy()
{
return isbusy;
}
void set_type(char c)
{
type=c;
}
char get_type()
{
return type;
}
user get_client()
{
return client;
}
void set_user(user p,int now_time)
{
client=p;
client.set_arrivtime(now_time);
isbusy=true;
}

void clear_client()
{
client.Clear();
isbusy=false;
}
};
typedef bank_window win;
class simulater                             //对象:模拟程序,包括五个窗口三条队列(n,b,v)
{
private:
Queue normalQ;
Queue vipQ;
Queue businessQ;
bank_window window[5];          //5个窗口,012为普通,3为vip,4为对公
public:
friend class user;
friend class Queue;
friend class bank_window;
simulater()                                                 //构造函数初始化窗口的类型:012为普通,3为vip,4为对公
{
for(int i=0;i<3;i++)
{
window[i].set_type('n');
}
window[4].set_type('b');
window[3].set_type('v');
}
void enter_bank(user userq[],int times)//,int now_Time    //进入银行  times:批次  now_Time:现在时间
{
for(int i=0;i<3;i++)                    //每次进银行的三人进入各自的队列
{
cout<<userq[times].get_id()<<"号开始排队"<<endl;
switch(userq[times].get_type())
{
case 'n':
{
normalQ.Inqueue(userq[times]);
times++;
break;
}
case 'b':
{
businessQ.Inqueue(userq[times]);
times++;
break;
}
case 'v':
{
vipQ.Inqueue(userq[times]);
times++;
break;
}
}
}

}
void call_customer(int now_Time)//int serve_time,            //窗口叫号
{
for(int i=0;i<5;i++)                    //遍历每个窗口
{
if(window[i].isBusy())    continue;
if(window[i].get_type()=='n')           //n型
{
if(!normalQ.isempty())
{
window[i].set_user(normalQ.outqueue(),now_Time);
cout<<i<<"号窗口:开始为"<<window[i].get_client().get_id()<<"号的"<<window[i].get_client().get_type()<<"型客户服务"<<endl;
}
//else    cout<<"normalQ空了,没有n型客户了"<<endl;
}
else if(window[i].get_type()=='v')      //v型
{
if(!vipQ.isempty())
{
window[i].set_user(vipQ.outqueue(),now_Time);
cout<<i<<"号窗口:开始为"<<window[i].get_client().get_id()<<"号的"<<window[i].get_client().get_type()<<"型客户服务"<<endl;
}
else
{
//cout<<"vipQ空了,看看有没有普通客户"<<endl;
if(!normalQ.isempty())
{
window[i].set_user(normalQ.outqueue(),now_Time);
cout<<i<<"号窗口:开始为"<<window[i].get_client().get_id()<<"号的"<<window[i].get_client().get_type()<<"型客户服务"<<endl;
}

//else    cout<<"没有普通客户,normalQ也空了"<<endl;
}
}
else if(window[i].get_type()=='b')      //n型
{
if(!businessQ.isempty())
{
window[i].set_user(businessQ.outqueue(),now_Time);
cout<<i<<"号窗口:开始为"<<window[i].get_client().get_id()<<"号的"<<window[i].get_client().get_type()<<"型客户服务"<<endl;
}
else
{
//cout<<"businessQ空了,看看有没有普通客户"<<endl;
if(!normalQ.isempty())
{
window[i].set_user(normalQ.outqueue(),now_Time);
cout<<i<<"号窗口:开始为"<<window[i].get_client().get_id()<<"号的"<<window[i].get_client().get_type()<<"型客户服务"<<endl;
}
//else    cout<<"没有普通客户,normalQ也空了"<<endl;
}
}
}
}
void serve_customer(int now_Time)                               //开始服务,判断窗口状态
{
for(int i=0;i<5;i++)
{
if(now_Time==window[i].get_client().get_finish_time())
{
cout<<window[i].get_client().get_id()<<"号客户服务结束"<<endl;
window[i].clear_client();
}
}
for(int i=0;i<5;i++)
{
//cout<<window[i].get_client().get_finish_time()<<endl;
if(!window[i].isBusy())
{
cout<<i<<"号窗口目前空"<<endl;
continue;
}

}

}
void simulate(user userq[])                                     //主程序
{
int now_time=0;
while(now_time<=ST)
{
cout<<"-----------第"<<now_time<<"秒:------------    "<<endl;
if(now_time!=ST)        enter_bank(userq,(now_time/2)*3);       //进入银行
serve_customer(now_time);                                       //开始服务
call_customer(now_time);                                        //窗口叫号
now_time+=2;
}
}
};
int main()
{
int i=0,j=0;
user *user_queue=new user[3*(ST/T)];                //共12秒,每次3人进入银行排队
{                                                   //初始化user_queue
user a0('n',++i);       user_queue[j++]=a0;
user a1('n',++i);       user_queue[j++]=a1;
user a2('n',++i);       user_queue[j++]=a2;
user a3('n',++i);       user_queue[j++]=a3;
user a4('b',++i);       user_queue[j++]=a4;
user a5('n',++i);       user_queue[j++]=a5;
user a6('n',++i);       user_queue[j++]=a6;
user a7('v',++i);       user_queue[j++]=a7;
user a8('n',++i);       user_queue[j++]=a8;
user a9('b',++i);       user_queue[j++]=a9;
user a10('n',++i);      user_queue[j++]=a10;
user a11('n',++i);      user_queue[j++]=a11;
user a12('v',++i);      user_queue[j++]=a12;
user a13('n',++i);      user_queue[j++]=a13;
user a14('n',++i);      user_queue[j++]=a14;
user a15('n',++i);      user_queue[j++]=a15;
user a16('n',++i);      user_queue[j++]=a16;
user a17('n',++i);      user_queue[j++]=a17;
}
simulater sim;
sim.simulate(user_queue);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: