#include<iostream>
#include<queue> //标准队列
#include<time.h>
#include<fstream>
using namespace std;
struct hair_cut_seat//理发或着洗头位置
{
int flag; //标记,表示这个位置有没有人
int times;//客人所选择的服务所需要的时间
int price;
int number;//第几个服务
int begin_time;//入门时间
int count;//第几号客人
};
struct marcel_seat//烫发位置
{
int flag;
int times;
int price;
int begin_time;//入门时间
int count;
};
struct Time //这个是为了储存 20,5,40,10,60,40
{
int times;
int price;
};
struct arriver//用来储存等待中客人的消息
{
int arriver_time;//到达时间
int times;//接受服务时间
int number;
int count;
};
class hair_cut_shop//理发店类
{
public:
hair_cut_shop(int n,int m);//初试化
~hair_cut_shop();
float stay_time(); //平均顾客逗留时间
int average_queue_long();//平均等待队列长度
void action();//开始进行操作;
void display(ostream & out);
private:
int h_c_s_count;//理发或者洗头位置的个数
int m_s_count;//烫发位置的个数
const int end_time;
int sum_time;//用来记录等待的客人所要的服务的时间总和
float que_long;//队列长度
int que_change;//队列改变长度的次数
int sum_cousterm,sum_intertime;//总共客人数,总共赚钱数,总共间隔时间
float sum_earn;//总共赚的钱
hair_cut_seat *h_c_s_array;
marcel_seat *m_s_array;
Time *t_array;
};
hair_cut_shop::hair_cut_shop(int n,int m):h_c_s_count(n),m_s_count(m),end_time(8*60)
{
h_c_s_array = new hair_cut_seat[h_c_s_count];
m_s_array = new marcel_seat[m_s_count];
t_array = new Time[3];
int i;
for(i=0;i<n;i++)
{
h_c_s_array[i].flag=0;//初始化空位置
h_c_s_array[i].times = 0;
h_c_s_array[i].count = 0;
h_c_s_array[i].number = -1;
}
for(i=0;i<m;i++)
{
m_s_array[i].flag=0;
m_s_array[i].times=0;
m_s_array[i].count = 0;
}
t_array[0].times = 20;//0号是洗头,1号是理发,3号是烫发
t_array[0].price = 5;
t_array[1].times = 40;
t_array[1].price = 10;
t_array[2].times = 60;
t_array[2].price = 40;
sum_cousterm=0;
sum_earn=0;
sum_intertime=0;
que_long = 0;
que_change = 0;
sum_time = 0;
}
hair_cut_shop::~hair_cut_shop()
{
delete [] h_c_s_array;
delete [] m_s_array;
delete [] t_array;
}
void hair_cut_shop::action()
{
int durtime=-1,intertime=-1;
int j;//获得理发或者洗头位置的下标
int z;//获得烫发位置的下标
arriver a;//用来储存等待客人的资料
queue<arriver> w_c_que;//等待理发跟洗头队列
queue<arriver> m_que;
queue<arriver> tem_que;//辅助队列
int flag=1;//判断是否第一个客人,为0的时候就表示不是第一个客人;
int copy_end_time = end_time;
int insert_flag=1;
int c_flag=0;//用来判断是否接受服务
int count=1;
int min;//用来计算最剩下的最小时间
vector<int> temp_c_h;//用来记录理洗发区的工作时间
vector<int> temp_m; //用来记录烫发区的工作时间
vector<int> tem_w_c_que;
cout<<"还有 "<<copy_end_time<<" 分就关门了"<<endl;
while(copy_end_time>0)//外循环,用来计算还有多少分钟
{
if(flag==0 && insert_flag == 1)//这个是用来判断空隙
{
count++;
srand((unsigned)time( NULL ));
intertime = 2+rand()%10;
cout<<"下一次客人来的时间是:"<<intertime<<endl<<endl;
insert_flag=0;
}
if(intertime==0||flag==1)
{
cout<<"有客人"<< count<<" 到来"<<endl;
insert_flag = 1;
flag=0;
srand((unsigned)time( NULL ));
const int i =rand()%3;
durtime = t_array[i].times;
if(i==0||i==1)
{ //做一系列的初始化工作
tem_w_c_que.clear();
while(!w_c_que.empty())
{
tem_que.push(w_c_que.front());
tem_w_c_que.push_back(w_c_que.front().times);
w_c_que.pop();
}
while(!tem_que.empty())
{
w_c_que.push(tem_que.front());
tem_que.pop();
}
if(i==0)
cout<<"客人"<<count<<"选择的是洗头"<<endl;
else if(i==1)
cout<<"客人"<<count<<"选择的是理发"<<endl;
if(w_c_que.empty())
{
min = 0;
for(int index2 = 1; index2< h_c_s_count;index2++)
if(h_c_s_array[min].times > h_c_s_array[index2].times)
min = index2;
if(h_c_s_array[min].times+durtime>copy_end_time)
{
c_flag = 1;
if(i==0)
cout<<"时间不够,不能接受洗发服务"<<endl<<endl;
else if(i==1)
cout<<"时间不够,不能接受理发服务"<<endl<<endl;
}
}//if
else{
temp_c_h.clear();
for(int index = 0;index<h_c_s_count; index++)
temp_c_h.push_back(h_c_s_array[index].times);
int count_tem_w_c_que = 0;
for(int index1 = 0;index1<w_c_que.size();index1++)//预计理洗发混合队列中的人要完成服务的最少时间
{
min = 0;
for(int index2 = 1; index2< h_c_s_count;index2++)
if(temp_c_h[min] > temp_c_h[index2] )
min = index2;
temp_c_h[min] += tem_w_c_que[count_tem_w_c_que++];
}
min = 0;
for(int index2 = 1; index2< h_c_s_count;index2++)
if(temp_c_h[min] > temp_c_h[index2] )
min = index2;
if(temp_c_h[min]+durtime > copy_end_time)
{
c_flag = 1;
if(i==0)
cout<<"时间不够,不能接受洗发服务"<<endl<<endl;
else if(i==1)
cout<<"时间不够,不能接受理发服务"<<endl<<endl;
}
}//else
}//if
else if(i==2)//用来判断是否接受服务
{
//做一系列的初始化工作
tem_w_c_que.clear();
while(!m_que.empty())
{
tem_que.push(m_que.front());
tem_w_c_que.push_back(m_que.front().times);
m_que.pop();
}
while(!tem_que.empty())
{
m_que.push(tem_que.front());
tem_que.pop();
}
cout<<"客人"<<count<<"选择的是烫发"<<endl;
if(m_que.empty())
{
min = 0;
for(int index2 = 1; index2< m_s_count;index2++)
if(m_s_array[min].times > m_s_array[index2].times)
min = index2;
if(m_s_array[min].times+durtime>copy_end_time)
{
c_flag = 1;
cout<<"时间不够,不能接受烫发服务"<<endl<<endl;
}
}//if
else
{
temp_m.clear();
for(int index = 0;index<m_s_count; index++)
temp_m[index] = m_s_array[index].times;
int count_tem_w_c_que = 0;
for(int index1 = 0;index1<m_que.size();index1++)//预计烫发队列中的人要完成服务的最少时间
{
min = 0;
for(int index2 = 1; index2< m_s_count;index2++)
if(temp_m[min] > temp_m[index2])
min = index2;
temp_m[min] += tem_w_c_que[count_tem_w_c_que++];
}
min = 0;
for(int index2 = 1; index2< m_s_count;index2++)
if(temp_m[min] > temp_m[index2])
min = index2;
if(temp_m[min]+durtime > copy_end_time)
{
c_flag = 1;
cout<<"时间不够,不能接受烫发服务"<<endl<<endl;
}
}//else
}//else_if
if(c_flag==0)
{
if(i==0 || i==1)
{
j=0;
while(j<h_c_s_count)
{
if(h_c_s_array[j].flag == 0)
{
cout<<"客人"<<count<<"坐的是 "<<j<<"号理发或者洗发位置"<<endl;
h_c_s_array[j].begin_time = copy_end_time;
h_c_s_array[j].flag=1;
h_c_s_array[j].times = durtime;
h_c_s_array[j].price = t_array[i].price;
h_c_s_array[j].number = i;
h_c_s_array[j].count = count;
break;
}
j++;
}
if(j==h_c_s_count)
{
cout<<"理发或洗发位置满了,请等一等"<<endl;
a.arriver_time = copy_end_time;
a.times = durtime;
a.number = i;
a.count = count;
w_c_que.push(a);
que_long += w_c_que.size();
que_change++;
}
}//if
else if(i==2)
{
z=0;
while(z<m_s_count)
{
if(m_s_array[z].flag == 0)
{
cout<<"客人坐的是 "<<z<<"号烫发位置"<<endl;
m_s_array[z].flag = 1;
m_s_array[z].begin_time = copy_end_time;
m_s_array[z].times = durtime;
m_s_array[z].count = count;
m_s_array[z].price = t_array[i].price;
break;
}
z++;
}
if(z == m_s_count)
{
cout<<"烫发位置满了,请等一等"<<endl;
a.arriver_time = copy_end_time;
a.times = durtime;
a.count = count;
a.number = i;
m_que.push(a);
que_long += m_que.size();
que_change++;
}
}//else if
}//if
c_flag=0;
}//if
for(int index = 0;index<h_c_s_count;index++)
{
if(h_c_s_array[index].flag==1 && h_c_s_array[index].times>0)
{
--h_c_s_array[index].times;
if(h_c_s_array[index].times == 0)
{
sum_cousterm++;
if(h_c_s_array[index].number == 0)
{
cout<<"座位号"<<index <<"的客人"<<h_c_s_array[index].count<<"的洗发服务完成了"<<endl;
cout<<"这个客人逗留了"<<h_c_s_array[index].begin_time - copy_end_time+1<<"分"<<endl;
h_c_s_array[index].number = -1;
sum_time += h_c_s_array[index].begin_time - copy_end_time+1;
}
else if(h_c_s_array[index].number == 1)
{
cout<<"座位号"<<index <<"的客人"<<h_c_s_array[index].count<<"的理发服务完成了"<<endl;
cout<<"这个客人逗留了"<<h_c_s_array[index].begin_time - copy_end_time+1<<"分"<<endl;
h_c_s_array[index].number = -1;
sum_time += h_c_s_array[index].begin_time - copy_end_time+1;
}
cout<<"总共完成 "<<sum_cousterm<<" 客人的服务"<<endl;
h_c_s_array[index].count = 0;
sum_earn += h_c_s_array[index].price;
cout<<"总共赚了:"<<sum_earn<<endl;
h_c_s_array[index].flag = 0;
h_c_s_array[index].times=0;
cout<<"理发或者洗发"<<index<<"号位置空了"<<endl;
if(!w_c_que.empty())
{
if(w_c_que.front().number == 0)
{
cout<<"等待洗发队列中的客人 "<<w_c_que.front().count<<" 号去 "<<index<<" 号理发或者洗发位置开始接受洗发服务"<<endl;
h_c_s_array[index].flag=1;
h_c_s_array[index].begin_time = w_c_que.front().arriver_time;
h_c_s_array[index].times = w_c_que.front().times;
h_c_s_array[index].price = 5;
h_c_s_array[index].number = w_c_que.front().number;
h_c_s_array[index].count = w_c_que.front().count;
w_c_que.pop();
que_long += w_c_que.size();
que_change++;
}
else if(w_c_que.front().number == 1)
{
cout<<"等待理发队列中的客人"<<w_c_que.front().count<<" 号去 "<<index<<" 号理发或者洗发位置开始接受理发服务"<<endl;
h_c_s_array[index].flag=1;
h_c_s_array[index].begin_time = w_c_que.front().arriver_time;
h_c_s_array[index].times = w_c_que.front().times;
h_c_s_array[index].price = 10;
h_c_s_array[index].number = w_c_que.front().number;
h_c_s_array[index].count = w_c_que.front().count;
w_c_que.pop();
que_long += w_c_que.size();
que_change++;
}
}//if
}//if
}//if
}//for
for(index = 0;index<m_s_count;index++)
{
if(m_s_array[index].flag==1 && m_s_array[index].times>0)
{
--m_s_array[index].times ;
if(m_s_array[index].times == 0)
{
cout<<"座位号"<<index <<"的客人"<<m_s_array[index].count<<" 烫发服务完成了"<<endl;
cout<<"这个客人逗留了"<<m_s_array[index].begin_time - copy_end_time+1<<"分"<<endl;
sum_cousterm++;
cout<<"总共完成 "<<sum_cousterm<<" 客人的服务"<<endl;
sum_earn += m_s_array[index].price;
cout<<"总共赚了:"<<sum_earn<<endl;
m_s_array[index].flag = 0;
m_s_array[index].times = 0;
m_s_array[index].count = count;
sum_time += m_s_array[index].begin_time - copy_end_time+1;
if(!m_que.empty())
{
cout<<"等待烫发的客人"<<m_que.front().count<<"开始去"<<index<<"号烫发位置接受服务"<<endl;
m_s_array[index].flag=1;
m_s_array[index].times = m_que.front().times;
m_s_array[index].price = 40;
m_s_array[index].begin_time = m_que.front().arriver_time;
m_s_array[index].count = m_que.front().count;
m_que.pop();
que_long += m_que.size();
que_change++;
}
}
}
}
copy_end_time--;
cout<<"还有 "<<copy_end_time<<" 分就关门了"<<endl;
cout<<endl;
for(int t = 0;t <50000000;t++);
if(flag==0&&intertime>0)
intertime--;
}//while
}
int hair_cut_shop::average_queue_long()
{
return static_cast<int>(que_long / que_change);
}
float hair_cut_shop::stay_time()
{
return static_cast<float>(sum_time/sum_cousterm);
}
void hair_cut_shop::display(ostream &out)
{
out<<"总共赚 "<<sum_earn<<"元"<<endl;
out<<"平均队列长度是:"<<average_queue_long()<<endl;
out<<"顾客平均逗留时间:"<<stay_time()<<endl;
}
void main()
{
int m,n;
cout<<"请输入理发位置的个数"<<endl;
cin>>m;
cout<<"请输入烫发位置的个数"<<endl;
cin>>n;
cout<<endl;
hair_cut_shop h(m,n);
h.action();
cout<<"过程输出到文本里,是D盘的"<<endl;
h.display(cout);
}