数据结构:用队列模拟理发店的排队情况(C#)

来源:互联网 发布:unity 烘焙 知乎 编辑:程序博客网 时间:2024/05/03 23:16

题目内容:使用的排队现象,通过仿真手法评估其营业状况。
*基本要求:设某理发馆有N把理发椅,可同时为N位顾客进行理发。
*当顾客进门时,若有空椅,则可以立即坐下理发,否则需要依次排队等候。
*一旦有顾客理完发离去时,排在队头的顾客便可开始理发。
*若理发馆每天连续营业T小时,求一天内顾客在理发馆内的平均逗留时间
*顾客排队等候的队列平均长度

N和T在运行的时候输入

用C#写的,有注释,很混乱,请高人指教~~

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. namespace 队列应用
  6. {
  7.     class Seat
  8.     {
  9.       
  10.         public  bool IsFree;
  11.         public Customer cus=null;
  12.         public Seat(bool b) 
  13.         {
  14.             IsFree = b;      
  15.         }
  16.     }
  17.     class Customer
  18.     {
  19.         public int cometime;
  20.         public int timetogo;
  21.         public int cost =30- new Random().Next(10);//理发需要20~30分钟
  22.         
  23.         public Customer() { }
  24.     }
  25.     class Program
  26.     {
  27.         static void Main(string[] args)
  28.         {
  29.             System.Console.WriteLine("每天营业多少小时?");
  30.             int workinghours = int.Parse(Console.ReadLine());
  31.             System.Console.WriteLine("有多少个椅子?");
  32.             int seats = int.Parse(Console.ReadLine());
  33.             Process(seats,workinghours);
  34.             Console.ReadLine();
  35.         }
  36.         static void Process (int num,int time)
  37.         {
  38.             //------------准备变量------------------------
  39.             Queue q = new Queue();//队列
  40.             Seat[] S=new Seat[num];//所有的椅子
  41.             for (int i = 0; i <num; i++)//初始化椅子
  42.             {
  43.                 S[i] = new Seat(true);
  44.                 S[i].cus = null;
  45.             }
  46.             int somebodycome = 1;//第一个顾客来的时间
  47.             //int count = 0;//顾客计数器
  48.             List<Customer> cuslist = new List<Customer>();
  49.             int st = num;//椅子数
  50.             int Qlen = 0;//队列长度
  51.             int Qchangetime = 0;//队列长度改变次数
  52.             //------------准备变量--------------------------
  53.             
  54.             for (int t = 1; t <= time*60; t ++)//时间从第一分钟开始流逝,每分钟检查状态
  55.             {
  56.                 //--------检查现在有没有人需要离开--------------
  57.                 CheckLeave(S, t);
  58.                 //--------检查现在有没有人需要离开--------------
  59.                 //-------检查排队的人是否可以找到座位---------
  60.                 CheckSeat(q, S, ref Qlen, ref Qchangetime, t);
  61.                 //-------检查排队的人是否可以找到座位---------
  62.                 //-------------如果来人了,有座位就坐下,没座位就排队-------------------
  63.                 if (t  == somebodycome)
  64.                 {
  65.                     bool IsSitted = false;//当前刚来的顾客是否找到了座位
  66.                     Customer c = new Customer();
  67.                     c.cometime = t;
  68.                     cuslist.Add(c);
  69.                     
  70.                     foreach (Seat s in S)
  71.                     {
  72.                         if (s.IsFree == true)
  73.                         {
  74.                             s.IsFree = false;
  75.                             s.cus = c;
  76.                             s.cus.timetogo = t+s.cus.cost;
  77.                             
  78.                             IsSitted = true;
  79.                             break;
  80.                         }
  81.                     }
  82.                     if (IsSitted == false)
  83.                     {
  84.                         q.Enqueue(c);
  85.                         Qlen += q.Count;
  86.                         Qchangetime++;
  87.                     }
  88.                     else
  89.                     {
  90.                         IsSitted = false;
  91.                     }
  92.                     somebodycome +=10- new Random().Next(5);//下一个顾客来的时间,假设5~10分钟之内会有一个
  93.                 }
  94.                 //------如果来人了,有座位就坐下,没座位就排队---------
  95.             
  96.             }
  97.             //---------加班----------
  98.             bool Inseat = true;
  99.             //bool InQ = true;
  100.             bool KeepWorking = true;
  101.             int curtime = time * 60 + 1;
  102.             while (KeepWorking)
  103.             {
  104.                 CheckLeave(S, curtime);
  105.                 CheckSeat(q, S, ref Qlen, ref Qchangetime, curtime);
  106.                 
  107.                 foreach (Seat s in S)
  108.                 {
  109.                     if (s.IsFree==false)
  110.                     {
  111.                         Inseat = true;
  112.                         break;
  113.                     }
  114.                     else
  115.                     {
  116.                         Inseat = false;
  117.                     }
  118.                 }
  119.                 KeepWorking = Inseat;
  120.                 curtime++;
  121.             }
  122.             //---------加班----------
  123.             //--------------
  124.             int no = 1;
  125.             foreach (Customer c in cuslist)
  126.             {
  127.                 Console.Write("第{0}个顾客  " ,no);
  128.                 Console.Write("来的时间:"+c.cometime);
  129.                 Console.WriteLine("  走的时间:"+c.timetogo);
  130.                 no++;
  131.             }//------------------
  132.             //--------打印结果-------------
  133.             int totalstaytime = 0;
  134.             foreach (Customer c in cuslist)
  135.             {
  136.                 int staytime = c.timetogo - c.cometime;
  137.                 totalstaytime += staytime;
  138.             }
  139.             int averragestay = totalstaytime / cuslist.Count;
  140.             System.Console.WriteLine("平均逗留时间:"+averragestay);
  141.             System.Console.WriteLine("顾客数量:" + cuslist.Count);
  142.             if (Qchangetime != 0)
  143.             {
  144.                 int averagelen = Qlen / Qchangetime;
  145.                 Console.WriteLine("队列平均长度:" + averagelen);
  146.             }
  147.             else
  148.             {
  149.                 Console.WriteLine("椅子充足,不用排队");
  150.             }
  151.             Console.WriteLine("加班时间:"+(curtime-time*60)+"分钟");
  152.             //--------打印结果------------
  153.         }
  154.         private static void CheckSeat(Queue q, Seat[] S, ref int Qlen, ref int Qchangetime, int t)
  155.         {
  156.             if (q.Count != 0)//如果有人排队
  157.             {
  158.                 foreach (Seat s in S)
  159.                 {
  160.                     if (s.IsFree == true)
  161.                     {
  162.                         s.IsFree = false;
  163.                         s.cus = (Customer)q.Dequeue();
  164.                         Qlen += q.Count;
  165.                         Qchangetime++;
  166.                         s.cus.timetogo = t + s.cus.cost;//
  167.                     }
  168.                 }
  169.             }
  170.         }
  171.         private static void CheckLeave(Seat[] S, int t)
  172.         {
  173.             foreach (Seat s in S)
  174.             {
  175.                 if (s.cus != null)
  176.                 {
  177.                     if (s.cus.timetogo == t)
  178.                     {
  179.                         s.IsFree = true;
  180.                     }
  181.                 }
  182.             }
  183.         }
  184.     }
  185. }
原创粉丝点击