开发进行时...

crazy coder

Avatar

原创--C#实例编程2

模拟超市购物

一、需求描述

超市客流符合泊松分布,有一个收银台,有三个购物通道,来人随机进入三个通道,在一

通道的购物数为2-5,停留时间60-180;在二通道的购物数为3-6,停留时间120-180;在三通道的

购物数为4-6,停留时间75-165;收银台的处理时间为每件物品为3s.输出结果:最大篮子数,

最长等待队列。

二、使用的知识要点

1.随机数
2.多线程

三、算法思想

自定义mydata类,mydata类中主要有两个数组,一个作为存储通道中的人Buyers,一个队列中的人myQ。整个程序有三个线程并发进行,第一个线程StartThread产生来人,向Buyers中插入数据,增加篮子数;第二个线程每隔10毫秒扫描Buyers数据,使其待待时间减10ms,如果为0则删除同时插入myQ数组,增加等待队列数;第三个线程每次取出队列中的第一个人,减少篮子数,减少等待队列数。如果队列中有人则增加收银员工作时间,反之则增加收银员的空闲时间。

四、主程序界面

五、主要程序段

客流符合泊松分布,则来人间隔时间符合负指数分布log(u)/lamla,u则符合均匀分布。
首先定义一个随机数

public Random myrond = new Random();//随机数



u=this.myrond.NextDouble();//0-1之间均匀分布的随机数


来人时间间隔为v

v=-1*this.averagetime*Math.Log(u);//产生来人时间间隔 产生负指数分布时间

自定mydata类
class myarrclass{...
}
自定义结构Buyer{
public int WayFlag;//通道
public int WaitTime;//等待时间
public int CountGoods;//购物数
}

六、全部程序代码

mlgw类


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace 经济管理模型
{
	/// <summary>
	/// mlgw 的摘要说明。
	/// </summary>
	public class mlgw : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.ListBox listBox2;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.Button button3;
		/*==================================*/
		//自动义变量和类
		public Random myrond = new Random();//随机数
		public static MyArrClass mydata;//数据类
		//
		public int Count_Baskets = 0;//篮子数
		public int Count_MaxBaskets=0;//最大篮子数
		public int Count_Queue = 0;//队列长度
		public bool flag=true;//是否运行
		public Thread StartThread,MiddleThread,EndThread;//三个线程
		public int multiple=100;
		private double averagetime;//平均来人时间
		private double worktime=0;//工作时间
		private double sparetime=0;//空闲时间
		//End自动义变量和类
		/*==================================*/
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public mlgw() 
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(mlgw));
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.button3 = new System.Windows.Forms.Button();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.button2 = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.listBox2 = new System.Windows.Forms.ListBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.tabControl1.SuspendLayout();
			this.tabPage1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.tabPage2.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.Add(this.tabPage1);
			this.tabControl1.Controls.Add(this.tabPage2);
			this.tabControl1.Location = new System.Drawing.Point(-8, 0);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(576, 416);
			this.tabControl1.TabIndex = 0;
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.Add(this.groupBox2);
			this.tabPage1.Controls.Add(this.groupBox1);
			this.tabPage1.Controls.Add(this.pictureBox1);
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(568, 390);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "模拟";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.button3);
			this.groupBox2.Controls.Add(this.textBox6);
			this.groupBox2.Controls.Add(this.label6);
			this.groupBox2.Controls.Add(this.textBox5);
			this.groupBox2.Controls.Add(this.label5);
			this.groupBox2.Controls.Add(this.button2);
			this.groupBox2.Controls.Add(this.button1);
			this.groupBox2.Location = new System.Drawing.Point(304, 248);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(248, 136);
			this.groupBox2.TabIndex = 8;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "参数";
			// 
			// button3
			// 
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button3.Location = new System.Drawing.Point(152, 48);
			this.button3.Name = "button3";
			this.button3.TabIndex = 8;
			this.button3.Text = "改变";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// textBox6
			// 
			this.textBox6.BackColor = System.Drawing.SystemColors.Info;
			this.textBox6.Location = new System.Drawing.Point(128, 80);
			this.textBox6.Name = "textBox6";
			this.textBox6.TabIndex = 7;
			this.textBox6.Text = "1000";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(16, 80);
			this.label6.Name = "label6";
			this.label6.TabIndex = 6;
			this.label6.Text = "最大容纳人数:";
			// 
			// textBox5
			// 
			this.textBox5.BackColor = System.Drawing.SystemColors.Info;
			this.textBox5.Location = new System.Drawing.Point(128, 24);
			this.textBox5.Name = "textBox5";
			this.textBox5.TabIndex = 5;
			this.textBox5.Text = "75";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(16, 24);
			this.label5.Name = "label5";
			this.label5.TabIndex = 4;
			this.label5.Text = "平均来人时间:";
			// 
			// button2
			// 
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button2.Location = new System.Drawing.Point(104, 104);
			this.button2.Name = "button2";
			this.button2.TabIndex = 3;
			this.button2.Text = "停止";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button1.Location = new System.Drawing.Point(24, 104);
			this.button1.Name = "button1";
			this.button1.TabIndex = 2;
			this.button1.Text = "确定";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.textBox3);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.textBox2);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.textBox1);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Location = new System.Drawing.Point(16, 248);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(240, 136);
			this.groupBox1.TabIndex = 7;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = " 统计信息";
			// 
			// textBox3
			// 
			this.textBox3.BackColor = System.Drawing.SystemColors.Info;
			this.textBox3.Location = new System.Drawing.Point(126, 88);
			this.textBox3.Name = "textBox3";
			this.textBox3.TabIndex = 12;
			this.textBox3.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(14, 88);
			this.label3.Name = "label3";
			this.label3.TabIndex = 11;
			this.label3.Text = "工作效率:";
			// 
			// textBox2
			// 
			this.textBox2.BackColor = System.Drawing.SystemColors.Info;
			this.textBox2.Location = new System.Drawing.Point(126, 56);
			this.textBox2.Name = "textBox2";
			this.textBox2.TabIndex = 10;
			this.textBox2.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(14, 56);
			this.label2.Name = "label2";
			this.label2.TabIndex = 9;
			this.label2.Text = "最长等待队列:";
			// 
			// textBox1
			// 
			this.textBox1.BackColor = System.Drawing.SystemColors.Info;
			this.textBox1.Location = new System.Drawing.Point(126, 24);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 8;
			this.textBox1.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(14, 24);
			this.label1.Name = "label1";
			this.label1.TabIndex = 7;
			this.label1.Text = "最大篮子数:";
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(8, 8);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(552, 224);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// tabPage2
			// 
			this.tabPage2.Controls.Add(this.listBox2);
			this.tabPage2.Controls.Add(this.listBox1);
			this.tabPage2.Controls.Add(this.panel1);
			this.tabPage2.Location = new System.Drawing.Point(4, 22);
			this.tabPage2.Name = "tabPage2";
			this.tabPage2.Size = new System.Drawing.Size(568, 390);
			this.tabPage2.TabIndex = 1;
			// 
			// listBox2
			// 
			this.listBox2.Location = new System.Drawing.Point(144, 224);
			this.listBox2.Name = "listBox2";
			this.listBox2.Size = new System.Drawing.Size(120, 160);
			this.listBox2.TabIndex = 2;
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(16, 224);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(120, 160);
			this.listBox1.TabIndex = 1;
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.Color.Honeydew;
			this.panel1.Location = new System.Drawing.Point(8, 8);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(552, 208);
			this.panel1.TabIndex = 0;
			// 
			// mlgw
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(560, 413);
			this.Controls.Add(this.tabControl1);
			this.Name = "mlgw";
			this.Text = "购物模拟";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.mlgw_Closing);
			this.Load += new System.EventHandler(this.mlgw_Load);
			this.tabControl1.ResumeLayout(false);
			this.tabPage1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.tabPage2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		public void  HandleStartThread()
		{
			double u,v;
			int vv,WayFlag;
			while(flag==true)
			{
				//u=Drand();
				u=this.myrond.NextDouble();//0-1之间均匀分布的随机数
				v=-1*this.averagetime*Math.Log(u);//产生来人时间间隔   产生负指数分布时间
				vv=(int)(v);
				WayFlag = this.myrond.Next(1,4);//选择的通道
				switch(WayFlag)
				{
					case 1:
						mydata.Add(1,this.myrond.Next(60,181),this.myrond.Next(2,6));//增加buyers
						break;
					case 2:
						mydata.Add(2,this.myrond.Next(120,180),this.myrond.Next(3,7));
						break;
					case 3:
						mydata.Add(3,this.myrond.Next(75,165),this.myrond.Next(4,7));
						break;
					default:
						break;
				}				
				//
				this.Count_Baskets+=1;
				if (this.Count_Baskets>this.Count_MaxBaskets)
					this.Count_MaxBaskets = this.Count_Baskets;
				this.textBox1.Text = this.Count_MaxBaskets.ToString();
				Thread.Sleep(vv);
			}
		}
		public double Drand()//优化随机数
		{
			double x;
			int i;
			for(i=0;i<20;i++) this.myrond.Next();
			x = this.myrond.Next();
			x=65539*x+1743251541;
			x=Math.IEEERemainder(x,2147483638);
			return(x/2147483638);
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.averagetime = Convert.ToDouble(this.textBox5.Text)*100;
			mydata = new MyArrClass(Convert.ToInt16(this.textBox6.Text));
			StartThread = new Thread(new ThreadStart(HandleStartThread));
			StartThread.Name="StartThread";
			MiddleThread = new Thread(new ThreadStart(HandleMiddleThread));
			MiddleThread.Name="MiddleThread";
			EndThread = new Thread(new ThreadStart(HandleEndThread));
			EndThread.Name="EndThread";
			this.flag = true;
			StartThread.Start();
			MiddleThread.Start();
			EndThread.Start();
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.flag = false;
			if(this.StartThread!=null)
			{
				this.StartThread.Abort();
				this.MiddleThread.Abort();
				this.EndThread.Abort();
			}
			this.textBox3.Text = Convert.ToString(this.worktime/(this.worktime+this.sparetime));
			//this.StartThread.Suspend();
			
		}
		private void HandleMiddleThread()//中间线程(第二个线程)
		{
			while(flag == true)
			{
				mydata.DelOne();
				//this.listBox1.Items.Add(this.Count_Baskets);
				this.listBox2.Items.Add(mydata.myQ_Count());
				this.textBox2.Text = mydata.MaxQueueLen.ToString();
				Thread.Sleep(10);
			}
		}
		private void HandleEndThread()//第三个线程
		{
			while(flag == true)
			{
				int temptime;//收银台处理时间
				if(mydata.myQ_Del()>0)//myQ_Del出队列,删除第一个人
				{
					Monitor.Enter(this.Count_Baskets);
					this.Count_Baskets--;
					Monitor.Exit(this.Count_Baskets);
				}
				if((temptime = mydata.myQ_First())>0)//获得当前队列第一个人的处理时间
					this.worktime+=temptime;
				else
					this.sparetime+=0.01;
				Thread.Sleep(temptime);
			}
		}

		private void mlgw_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{//程序关闭事件
			if(this.StartThread != null) this.StartThread.Abort();
			if(this.MiddleThread != null) this.MiddleThread.Abort();
			if(this.EndThread != null) this.EndThread.Abort();
			this.Dispose();
		}

		private void mlgw_Load(object sender, System.EventArgs e)
		{
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			this.averagetime = Convert.ToDouble(this.textBox5.Text)*100;
		}
	}
}

MyArrClass类

using System;
using System.Windows.Forms;
using System.Collections;
using System.Threading;

namespace 经济管理模型
{
/// <summary>
/// MyArrClass 的摘要说明。
/// </summary>
public class MyArrClass
{
struct Buyer
{
public int WayFlag;
public int WaitTime;
public int CountGoods;
}
private Buyer[] Buyers;//存储购买的人
private Queue myQ = new Queue();//存储等待的队列
public int MaxQueueLen = 0;
private int BuyerLen = 0;
public MyArrClass(int MaxLen)
{
//
// TODO: 在此处添加构造函数逻辑
//
Buyers = new Buyer[MaxLen];
}
public int Add(int a,int b,int c)
{
this.Buyers[BuyerLen].WayFlag = a;
this.Buyers[BuyerLen].WaitTime = b;
this.Buyers[BuyerLen].CountGoods = c;
this.BuyerLen++;
return this.BuyerLen;
}
public void Del(int i)
{
for(int j=i;j<this.BuyerLen;j++)
{
Buyers[j]=Buyers[j+1];
this.BuyerLen--;
}
}
public void DelOne()
{
for(int i=0;i<BuyerLen;i++)
{
//while(Buyers.IsSynchronized != true)
//{continue;}
Monitor.Enter(Buyers);
Buyers[i].WaitTime -=10;
if(Buyers[i].WaitTime < 0)
{
this.myQ_Add(Buyers[i].CountGoods);
this.Del(i);
}
Monitor.Exit(Buyers);
if (i>=BuyerLen) break;
}
}
public int myQ_Add(int Countgoods)
{
//while (myQ.IsSynchronized != true)
//{continue;}
myQ.Enqueue(Countgoods);
return myQ.Count;
}
public int myQ_Del()
{
if(myQ.Count >1)//myQ.count 等待队列的长度
{
Monitor.Enter(this.myQ);
int temp = Convert.ToInt16(myQ.Dequeue());//dequeue出队列
Monitor.Exit(this.myQ);
return temp>0?temp:0;
}
else
return 0;
}
public int myQ_First()
{
return 100*3*(Convert.ToInt16(myQ.Count>0?myQ.Peek():0));
}
public int myQ_Count()
{
if(this.MaxQueueLen < this.myQ.Count) this.MaxQueueLen = this.myQ.Count;
return this.myQ.Count;
}
}
}

评论已关闭