📄 class1.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
//定义结构体数组,放顾客进入的相应事件.
public class customer
{
int NO; /*编号*/
int durtime; /*理发所需时间*/
int intime; /*进入理发店时刻*/
int starttime; /*开始理发时刻*/
int interval; /*他的下一个人到来的时间间隔*/
int wait_flag,serve_flag; /*是否在等待,是否在理发*/
int R;
int i;
int j;//判断当前时间栈中剩余的人数是否为零,若为零则此刻到来的人立即理发,否则入队等候。
int[] wait;
const int TRUE=1;
const int FALSE=0;
int N1;//剩余的空位。
int totaltime; /*顾客理发所需总时间*/
int start; /*开张时间*/
int curtime; /*当前时间*/
//等待队列.
int T,N;//开门时间,可用的椅子数.
/*顾客进入理发店*/
public void customer_in(customer n)
{ /*每次产生不同的随机数*/
int i=0;
i++;
//程序运行结束,totalnum表示进店总人数。
intime=0;
Random ra=new Random();//产生随机数.
R=ra.Next(10,100);
n.NO=i;
n.intime=curtime; /*记录顾客进入时间*/
n.durtime=15+R%50; /*产生随机数记录顾客理发所需时间*/
n.interval=2+R%10; /*此顾客的下一个顾客来的时间间隔*/
if(n.wait[0]==0&&n.N1>0)
{customer_serve( );} /*有空闲位置并无人参与竞争,调用服务函数*/
else
{
n.wait[0]=1;//等候的顾客总数。
n.wait_flag=TRUE; /*否则入队等待*/
wait[i++]= n.NO;
}
}
/*为顾客理发*/
public void customer_serve(customer n)
{
n.starttime=curtime;
N1--;
n.serve_flag=TRUE;
n.wait_flag=FALSE;
totaltime+=n.durtime;
}
/*顾客离开理发店*/
public void customer_leave(customer n)
{
n.serve_flag=FALSE;
N1++;
}
public void InitQueue(customer n) //定义一个顺序栈,将要等待的人放入栈中
{
i=0;j=0;
wait[i]=0;
}
public void EnQueue(customer n) //将要等候的人入栈。
{
wait[0]++;//程序运行结束,wait[0]表示排队总人数。wait[0]/totalnum=平均队长。
wait[++i]=wait_flag;
j++;}
public void DeQueue(customer n) //将栈中接受服务的人删除。
{
int k=0;
wait[++k]=0;//若等待的人去理发,则将他原先所排的位子赋予0,表是此位无人。
j--;//若最后j=0,则表示无人排对,此刻到来的顾客可立即接受服务。
}
public static void Main()
{
customer u=new customer();
while(u.curtime<u.T) /*当前时间属于营业时间,允许顾客进入、为顾客服务*/
{
u.curtime++; /*当前时间*/
for(int k=1;k<=u.wait[0];k++)
{ /*判断有没有人离开*/
if((u.serve_flag==TRUE)&&(u.starttime+u.durtime==u.curtime))
u.customer_leave(u.wait );
}
while(u.N1>0&&u.j!=0) /*让等待队列中的人去理发*/
customer_serve(DeQueue(u.wait));
/*判断是否有人符合要进的条件*/
if((u.intime+u.interval)==u.curtime)
customer_in( );
}
while(u.j!=0) /* 为等待的顾客服务,但 不允许顾客进来了*/
{
u.curtime++;
for(int k=1;k<=u.i;k++)
{ /*判断有没有人离开*/
if((u.serve_flag==TRUE)&&(u.starttime+u.durtime==u.curtime))
customer_leave(u.wait);
}
while(u.N1>0&&u.j!=0) /*让等待队列中的人去理发*/
customer_serve(DeQueue(u.wait));
}
Console.WriteLine("顾客总数为:{0}",u.NO);
Console.WriteLine("平均等候时间为:{0}",u.totaltime/u.NO);
Console.WriteLine("平均队长为:{0}",u.i/u.wait[0]);}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -