题目内容:使用队列模拟理发馆的排队现象,通过仿真手法评估其营业状况。 6 T* n; K0 X& F l/ R# [基本要求:设某理发店有N把理发椅和M个烫发机,可同时为N位顾客理发或洗发和M个顾客烫发。理发服务分为三个等级(洗头 理发 烫发),对应不同收费,当顾客进门时,若有相应服务类型有空椅,则可立即坐下理发,否则需依次排队等候。一旦有顾客服务完离去时,排在队头的顾客便可开始受理服务。若此顾客服务时间超过关闭时间则放弃此服务。1 T l5 A& |- Y: T9 l: A
若理发店每天连续营业T小时,要求输出一天内平均顾客数.平均收入.顾客在理发馆内平均的逗留时间、顾客排队等候理发的队列的平均长度。 0 Q* L3 n q* d % U* H; T) f& l" F测试数据:理发椅数N和烫发机M由用户读入,第一个顾客进门的时刻为0,之后每个顾客进门的时刻由前一个顾客进门时间确定。即在进门事件发生时即产生两个随机数(durtime,intertime),durtime为进门顾客理发所需要的服务时间(即服务类型:洗发:20分钟,5元;理发:40分钟,10元;烫发:1小时,40元),intertime为下一个顾客将到达的时间间隔(下一个随机数产生时间)。R为由随机发生器产生的随机数,顾客理发时间和顾客之间的间隔时间不妨设与R有关 & Y! {" ?$ |$ d x; U2 \4 E& X& r0 X e* s: s+ K5 d
durtime=1+R%3 # f8 s% r( |: m# J% y" S0 b" s" \8 O. S2 e4 i% p6 t
intertime=2+R%10 - B9 R5 {% c, v# l e5 S: \6 B ?# `2 v
这里设早上9点开业,下午5点关门! o% A- F" @) q- G
#include<iostream> ! e% p- J" y0 `, l1 R. `- R#include<queue> //标准队列 % `) s8 _" Q ]7 J$ D. r. f#include<time.h> / M0 s5 ^7 W. R9 Y" j#include<fstream> 9 i7 b k2 V( I7 t6 wusing namespace std; 8 R1 D9 m6 N5 W8 h; X1 u! m# astruct hair_cut_seat//理发或着洗头位置# A/ q3 d4 d( ^7 d2 \( d$ V" N
{ 3 n! @3 H N8 q1 V, x0 Lint flag; //标记,表示这个位置有没有人 * a# L2 K* n! g* J: ~9 X _int times;//客人所选择的服务所需要的时间 H* }" v7 Q" ~' \6 Z
int price; , a5 W, |8 n- B5 iint number;//第几个服务+ `- N _4 J) q3 x2 P' P# A
int begin_time;//入门时间4 W) c, g0 ~0 o( _/ h+ [
int count;//第几号客人 . T7 F% S8 P+ [& Y0 A7 C( Y/ x0 ]}; t2 I. s# w0 T1 Y' c
, Q2 r+ Q: `. g5 e. |" P
struct marcel_seat//烫发位置 8 F0 h, q8 W- X' f; x' X" G{ 3 m' L" ~7 \3 bint flag; 6 @! J, W1 \4 W" Q3 Iint times; 9 N9 f4 u8 w# ^" [! T& P5 \5 i9 R: yint price;. S B3 Y: v9 |2 x$ ` D
int begin_time;//入门时间 % ^, r7 y; l1 F- [; |int count;: t5 H/ _" R# w9 v# |3 J4 S8 s
};0 h, x w4 C( b. l3 b) a5 w: W
5 ]( J1 H$ o% h5 \# b3 Istruct Time //这个是为了储存 20,5,40,10,60,40 . F4 z: L& v6 Z/ N- f5 d7 q# ?& b( S{ / A; V9 t2 }. J# L; n- u4 eint times; / ?4 m% F$ f$ \0 J _3 {# Iint price; & C. P. ]. J. \7 l! K5 L}; / J/ B6 z, l* g+ w7 N/ Q: \& B( t$ H- S& Hstruct arriver//用来储存等待中客人的消息( Q2 c5 j- j8 ^+ s0 F6 a
{ $ {" V' ~2 H- b$ Jint arriver_time;//到达时间 % R1 ]5 I) ^0 ?. j& sint times;//接受服务时间3 c* A2 o4 A+ E5 U E* K0 @1 t% I" {. D
int number;; t5 o+ J. X& _8 U
int count; ! d5 e1 D8 k3 s) e: \/ C& c1 ^}; " p+ E" [* z' T3 i% f* cclass hair_cut_shop//理发店类/ R6 l) W3 Z% K8 g: c
{* D; U8 p( Z0 i* Q4 \' }
public: % R' f' I; \/ e, jhair_cut_shop(int n,int m);//初试化 4 E% Y* ^2 Y6 U# |2 _4 A. ~~hair_cut_shop();: I n* h- _ ?7 B% a
float stay_time(); //平均顾客逗留时间6 H4 ]& Q$ p6 Z
int average_queue_long();//平均等待队列长度( Y7 ?- V; o2 K/ ?0 W' b. ]
void action();//开始进行操作; " p5 k5 S h7 F* m; \4 V+ C/ z0 Pvoid display(ostream & out); + N$ ~ F& e" C6 Pprivate: a0 Z& P) A) A! n* ~int h_c_s_count;//理发或者洗头位置的个数 * ^/ b1 L* _/ C8 P, {0 E8 uint m_s_count;//烫发位置的个数; Y8 n( _& a7 N' n! w. ~
const int end_time; / W8 ?9 ~2 M2 \, e! H9 s8 ~" \int sum_time;//用来记录等待的客人所要的服务的时间总和 7 U5 L1 v. _# a! g4 h3 A4 Mfloat que_long;//队列长度 5 ]* Q* Q+ S" qint que_change;//队列改变长度的次数 ! J1 C- E& ^6 Vint sum_cousterm,sum_intertime;//总共客人数,总共赚钱数,总共间隔时间 s+ e5 e! J7 O% K+ g/ ^
float sum_earn;//总共赚的钱2 a4 U8 d% o4 s# o
hair_cut_seat *h_c_s_array;% [5 y0 v# W) ?" H; o, o1 I, p" J
marcel_seat *m_s_array; 9 u, ? P: Y& X- ^/ a0 Z6 ]1 y3 mTime *t_array; 6 i1 m8 }3 y: [' _};6 s/ H: r- L( H+ U( j! j
$ ~. S& F' d( d6 ?* r1 I. @
hair_cut_shop::hair_cut_shop(int n,int m):h_c_s_count(n),m_s_count(m),end_time(8*60) s# n8 e1 k- T/ l
{ ( W8 J, o9 Q* F0 B( B! J6 N- I( Th_c_s_array = new hair_cut_seat[h_c_s_count];( j A; |$ D* Y7 m7 ]
m_s_array = new marcel_seat[m_s_count];' d9 N. _ P% v, g
t_array = new Time[3]; 4 N% W* U! W* ~$ d+ nint i; ( O. o W0 J0 [! s4 Ofor(i=0;i<n;i++) ; Z% d% O4 G/ F, E{* ~% [: g/ }, @$ a* K& r2 P
h_c_s_array[i].flag=0;//初始化空位置7 }% W: M4 J" U) U
h_c_s_array[i].times = 0; : G. @& w a8 e, S. b7 dh_c_s_array[i].count = 0;- r) { Y+ g# D: v
h_c_s_array[i].number = -1; t. c% [, u" J% V3 C
}+ D5 M5 w4 v# }2 Y$ o# B
for(i=0;i<m;i++)3 e1 ~! j- \$ p& ^
{ * |( A9 A7 m1 O- L$ T' U
m_s_array[i].flag=0;6 {' v" a1 K$ ^. Z7 j% @
m_s_array[i].times=0;1 P1 q1 ]; z5 h" Z# q
m_s_array[i].count = 0; ' x5 K5 f1 d5 ?* h6 c1 D4 U}+ v/ A& I5 I; X& K( G
( B6 r) G- L) T- q4 v% }6 |$ q6 \1 \t_array[0].times = 20;//0号是洗头,1号是理发,3号是烫发) K4 o/ u, m W9 p' J. p
t_array[0].price = 5; 5 k8 ?5 ]% k9 _6 M* b8 Z) C; it_array[1].times = 40;, ]$ w' B! ]' g1 w9 l
t_array[1].price = 10; 8 Y0 P3 U1 _" R2 b# E: Zt_array[2].times = 60;3 u* a2 D% U9 ]- K7 A
t_array[2].price = 40; 4 {8 j4 ~! u: C, P ( v* }( p# G3 Fsum_cousterm=0;, z1 [) B; @' G& C* o/ n- @* t6 v
sum_earn=0; 1 R) [2 v0 O: j1 l& \sum_intertime=0; 7 T( |5 \' I, T6 }que_long = 0;; K: b. o3 B1 ?$ i. S6 T. i9 n
que_change = 0; $ R6 P2 n4 B& s) F) usum_time = 0; - j! k. u/ s0 t- v + l4 @0 }. ~- K} 3 l" F9 ^' }, E9 v: V
+ R2 T+ @- t' }# {' U2 s3 qhair_cut_shop::~hair_cut_shop()/ s U+ p6 s+ \/ x
{3 N) {. @3 n+ f2 m4 X2 d; O' Q" c
delete [] h_c_s_array;4 Z8 q7 D6 U! f1 ~6 t: G4 |) t
delete [] m_s_array;" B( R4 L5 c& y$ e
delete [] t_array; $ ~ H% R: E% W# F6 F3 O}( R/ n6 }# \) k& H! s
void hair_cut_shop::action(). V$ h7 {$ w, w
{1 f: J- }* I$ j( G: v0 m3 D0 o
int durtime=-1,intertime=-1; 5 b) }9 L+ T; z9 ~& q' L- Y& }int j;//获得理发或者洗头位置的下标 & V/ h& `; @* y: a' ^/ E8 n7 ?7 o& Mint z;//获得烫发位置的下标3 A' I4 ?1 V% t5 z2 p
arriver a;//用来储存等待客人的资料 j4 o6 K: }; ^/ Y
queue<arriver> w_c_que;//等待理发跟洗头队列% b( t6 I# P% s: B+ U) H- ^3 Z# M
queue<arriver> m_que;( i- ?6 k9 V# X5 y2 n
queue<arriver> tem_que;//辅助队列 ' n" s* b3 j! V, P% j/ Y( V3 {" N) tint flag=1;//判断是否第一个客人,为0的时候就表示不是第一个客人;9 t1 A# n' \ ]3 @# ~
int copy_end_time = end_time;- ~9 W/ O. q# H9 L
int insert_flag=1;* r: b( ^! t* q4 W1 K
7 V; z+ S; z% I3 {' ]+ q5 L Cint c_flag=0;//用来判断是否接受服务4 S# j/ p( l1 E# M
int count=1; # C3 N7 \- G' [8 V) H6 K! aint min;//用来计算最剩下的最小时间! D7 ^2 P2 l) |9 W3 {
vector<int> temp_c_h;//用来记录理洗发区的工作时间 r9 i. T5 |4 ]
vector<int> temp_m; //用来记录烫发区的工作时间 5 a6 S+ ~5 k' I$ W0 ?2 o0 p, h9 ?vector<int> tem_w_c_que;! }. I/ Q6 E/ E8 w; c: x2 J
cout<<"还有 "<<copy_end_time<<" 分就关门了"<<endl; 4 F! o2 {. y6 j0 x# }9 Q: w$ E3 U( T( ]# @2 B- j4 `' n
while(copy_end_time>0)//外循环,用来计算还有多少分钟 & o3 [. o h. h9 k{1 L0 \& \! f" k* g2 ]7 C7 [
* [# a( F; Y9 k0 Q a5 m
if(flag==0 && insert_flag == 1)//这个是用来判断空隙 ! v5 ?6 `, ^0 @5 J) I, i{ O& `( F! R' u- m& Vcount++;: h! A4 N( `& l6 c
srand((unsigned)time( NULL ));2 S, Y# q6 I: E& L; E! Z
intertime = 2+rand()%10; & r3 G& k, D9 G. Ucout<<"下一次客人来的时间是:"<<intertime<<endl<<endl;& s s( g8 k7 r; H- b
insert_flag=0;" ^4 l$ S1 N4 j6 ^' i) W1 M& r+ @# V) f
}# G/ u+ Q( l4 I; O' z
if(intertime==0||flag==1); D8 f4 b) e, ?9 Z0 @( E) V$ @1 ?
{% k' p3 V7 {/ D, P: d
cout<<"有客人"<< count<<" 到来"<<endl; # F/ B& F. H7 p5 ]- K$ a: Zinsert_flag = 1; * [" k5 @+ a- e1 Z0 W" X9 t4 U# ?flag=0;1 G0 X* O! ?4 T
srand((unsigned)time( NULL )); " c; l, t9 k q# vconst int i =rand()%3; k( C6 Y- x( ~0 z. p# P8 N( `
durtime = t_array[i].times; 1 R) z# C0 I3 x- S; m+ A6 a , ?; n3 n; a+ c. z# p" [; }if(i==0||i==1) S3 `4 x8 F: ?{ //做一系列的初始化工作 : Y P7 g; `: Y% n. X8 B# U1 f ; F' z1 I0 m; i1 |0 {tem_w_c_que.clear(); ) j( i& A6 a3 Y) f8 {. B - T- y1 V& O; owhile(!w_c_que.empty()) $ }: X2 z: x8 G9 m, F. |3 L8 j2 G! w{. _5 A6 ^8 u/ ^
tem_que.push(w_c_que.front());: m, D- u) Z4 \: P/ [3 j+ i7 L
tem_w_c_que.push_back(w_c_que.front().times);& @' f. q3 ?& Q4 U! E& t( a
w_c_que.pop();! j, C$ N* e2 i7 d# h$ r
}5 I9 w6 {1 }/ X- l4 X
8 F: @9 z4 x6 C! i8 h6 L
while(!tem_que.empty()) 5 G, ?/ D; a# v( {3 C" {2 y: k{ , L U' ~: A# fw_c_que.push(tem_que.front());7 w& E: c; v* ]5 k; i8 c6 j
tem_que.pop();/ b" _+ n8 ?8 B% f
} 4 @$ l+ f; t$ h3 B6 E* \3 ?8 s( V0 M4 X% d" z- l' O+ _( v
if(i==0) * M5 D5 a& F5 i( ]cout<<"客人"<<count<<"选择的是洗头"<<endl;3 D' o9 D4 ]8 a2 q( l& l% i
else if(i==1) : F9 ~$ n1 t# q3 o1 Mcout<<"客人"<<count<<"选择的是理发"<<endl; * x- ^5 B e+ s8 l' u6 Mif(w_c_que.empty())! U- Y( f2 i/ L( A; k5 o
{ ! I+ k; y0 I! K5 `min = 0;/ ? f2 b# e8 Q+ P6 l" q |' i
for(int index2 = 1; index2< h_c_s_count;index2++) 2 r& a. \7 T: x- ^# o. f/ O$ zif(h_c_s_array[min].times > h_c_s_array[index2].times)/ ]1 \4 h/ G7 d% Z" D1 \- o
min = index2;5 Y& O5 C T) q) S h
; F9 }& T% S; z% ?- g* n) @- c1 c* z; S, S3 m+ R+ P
' X8 K& O0 i/ w, V) Q; H
if(h_c_s_array[min].times+durtime>copy_end_time)9 W: C3 f6 Y' }) e
{ / f6 H3 | l( u- I; |' Yc_flag = 1;3 b3 w7 D1 V8 o; g3 X9 V
if(i==0): d2 c u' i/ s
cout<<"时间不够,不能接受洗发服务"<<endl<<endl;* J/ Q$ e0 E$ C, Q* E, Y
else if(i==1)4 ^% E% L* {& y7 N8 I& P
cout<<"时间不够,不能接受理发服务"<<endl<<endl;- ~% S% ]8 f' `
} 5 ^3 ~5 f0 S# b) f: U' [* A}//if 7 [' A( A" j1 B/ ]. D8 L* M& Delse{ ) [. \; t' ~" X0 F H# b5 Utemp_c_h.clear();7 ?4 C7 a. E8 D9 z" z
for(int index = 0;index<h_c_s_count; index++) 6 N$ l7 H% ?! m8 a/ ?, d' Utemp_c_h.push_back(h_c_s_array[index].times); 2 U# P, g& M" c2 ]/ B7 @' o2 o/ c$ A3 f) t+ A# g
int count_tem_w_c_que = 0;; C: a) }. |( `
" z5 f; q* ]" u+ T* v8 Gfor(int index1 = 0;index1<w_c_que.size();index1++)//预计理洗发混合队列中的人要完成服务的最少时间 1 j, \( h# U2 D) ?) f2 L3 ^9 ~' g{5 l' p$ T6 x! X. s, y. n
min = 0;# f8 Y7 K# h* R
for(int index2 = 1; index2< h_c_s_count;index2++) # q, P$ j; G+ Q0 H. k: ~& u0 h) ?4 Dif(temp_c_h[min] > temp_c_h[index2] ) $ ^9 Q9 |( R6 \& jmin = index2; . J' ^5 `. V; utemp_c_h[min] += tem_w_c_que[count_tem_w_c_que++];# Y1 K& b# d2 }. j% p. ]
}4 E9 j) w( E8 a* C7 B$ A5 O2 A
min = 0;: ~' q* m; w% r- b7 |. k ?
for(int index2 = 1; index2< h_c_s_count;index2++). r- ]5 H( Z4 y: f& q3 ^* |! q
if(temp_c_h[min] > temp_c_h[index2] ) + Q. M- V4 h) B& g) Zmin = index2; 2 H; x0 N( }$ ^! ^* Q * @: l- R+ a/ M4 y+ p " M* g/ W' H' t) x# u9 K3 R5 U. h" k( J
if(temp_c_h[min]+durtime > copy_end_time)$ T. n3 |+ z* |7 o9 E: n
{ 8 K% Y' |. F# S- l# q) qc_flag = 1;! w, m. v. s$ C& J( ^* C9 e4 J
if(i==0)- g: J3 B/ w# k0 s$ K1 d+ y
cout<<"时间不够,不能接受洗发服务"<<endl<<endl; 6 a$ k! U; H7 \% q! {. N1 s: X4 belse if(i==1)% W3 \. w2 m. o3 G3 l, G
cout<<"时间不够,不能接受理发服务"<<endl<<endl;) K% X. G4 @( ~7 O, L' {
} ( G9 K; g0 q2 e) p $ I5 W# h t$ ^; z0 ~( J5 B4 c, L1 D7 Z+ L7 i
}//else ) M: X) w ^- s' y. l}//if0 m7 U/ ^4 p/ r1 q) |" f0 c
& f' T) }1 C e2 t; G! ~4 _. q3 W: G! I. c& ~
else if(i==2)//用来判断是否接受服务$ s' s2 R M. a+ ]' h6 `
{1 [* H, v# H" @$ j# a
//做一系列的初始化工作6 U! I% F' M/ E- k6 u7 [- B