سلام
این پروژه شبیه سازی مربوط به مبحث صف ها در ساختمان داده هستش
پروژه مشترک @Pouya و @Mahshid
در صورت داشتن سوالی در مورد پروژه به یکی از ماها بگید
کلاس simulation
//in the name of god
//pouya darabi & mahshid fallah
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simulator
{
class Simulation
{
public Server[] MyServer;
public MyQueue Queue;
public int Removed = -1;
private int Servers_Size = 10;
private double SimulationLenght;
private double WaitSum;
private double TransAction_Time;
private double ArriveTime;
private int ClinetNum;
private int LostClients;
private Clock time;
public Simulation(int Server_Numbers, double transAction_Time, double ArrTime, double SimLenght)
{
Servers_Size = Server_Numbers;
MyServer = new Server[Servers_Size];
TransAction_Time = transAction_Time;
ClinetNum = 0;
ArriveTime = ArrTime;
SimulationLenght = SimLenght;
WaitSum = 0.0;
LostClients = 0;
time = new Clock();
for (int i = 0; i < Servers_Size; i++)
{
MyServer[i] = new Server();
}
Queue = new MyQueue(100);
}
public bool Add2Q()
{
time.GetTime++;
Random Rn = new Random();
double ArrRnd = Rn.NextDouble();
if (ArrRnd < ArriveTime)
{
if (!Queue.Add(new Client()))
{
LostClients++;
return false;
}
return true;
}
return false;
}
public void Check(out double AvTime, out int LostNum, out int Complete)
{
if (time.GetTime >= SimulationLenght)
{
AvTime = AverageTime();
LostNum = LostClients + Queue.GetNum();
Complete = ClinetNum;
return;
}
UpdateQueue();
UpdateServers();
for (int j = 0; j < Servers_Size; j++)
{
if (MyServer[j].IsFree)
{
if (!Queue.IsEmpty())
{
ClinetNum++;
Client MyCL = Queue.Remove();
Removed = j;
WaitSum += MyCL.WaitTime;
MyServer[j].GiveClient(MyCL);
MyServer[j].TransActionTime = TransAction_Time;
}
}
}
AvTime = 0;
LostNum = 0;
Complete = 0;
}
public void SetTransactionTime(double AverageTime)
{
TransAction_Time = AverageTime;
}
private void UpdateQueue()
{
Queue.IncreaseWaitTime();
}
private double AverageTime()
{
return WaitSum / ClinetNum;
}
public void UpdateServers()
{
for (int j = 0; j < Servers_Size; j++)
{
if (!MyServer[j].IsFree)
{
MyServer[j].UpdateTime();
}
}
}
}
}
کلاس server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Simulator
{
class Server
{
bool _Busy = false;
Client MyCL;
double Time = 0;
double _TransActionTime = 0.0;
public bool IsFree
{
get
{
if (_Busy) return false;
else return true;
}
}
public double TransActionTime
{
set
{
_TransActionTime = value;
}
get
{
return _TransActionTime;
}
}
public void UpdateTime()
{
if (TransActionTime != 0)
{
TransActionTime--;
// return false;
}
else
{
Time += MyCL.WaitTime;
_Busy = false;
MyCL = null;
// return true;
}
}
/* public void SetW8()
{
MyCL.WaitTime++;
}*/
public void GiveClient(Client CL)
{
_Busy = true;
MyCL = CL;
//Thread.Sleep(Convert.ToInt32(CL.WaitTime));
// _Busy = false;
}
}
}
کلاس myqueue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simulator
{
class MyQueue
{
Client[] List;
int Rear = -1;
//int Front = -1;
static int Max = 100;
public MyQueue(int QueueMax)
{
Max = QueueMax;
List = new Client[Max];
}
public MyQueue()
: this(Max)
{
}
public Client Remove()
{
Client myclient = List[0];
Shift2Left();
Rear--;
return myclient;
}
public bool Add(Client CL)
{
if (!IsFull())
List[++Rear] = CL;
else
return false;
return true;
}
private void Shift2Left()
{
for (int i = 0; i < Rear; i++)
{
List[i] = List[i + 1];
}
}
public void IncreaseWaitTime()
{
for (int i = 0; i < Rear; i++)
{
List[i].UpdateW8();
}
}
public bool IsEmpty()
{
if (Rear == -1)
return true;
else return false;
}
public bool IsFull()
{
if (Rear == Max - 1)
return true;
else return false;
}
public int GetNum()
{
return Rear + 2;
}
}
}
کلاس clock
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simulator
{
class Clock
{
private double _Time = 0.0;
public double GetTime
{
set { _Time = value; }
get { return _Time; }
}
}
}
کلاس client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simulator
{
class Client
{
private double _WaitTime = 0.0;
public Client()
{
}
public double WaitTime
{
get
{
return _WaitTime;
}
}
public void UpdateW8()
{
_WaitTime++;
}
}
}
فرم اصلی
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Media;
using System.Runtime.InteropServices;
using System.IO;
namespace Simulator
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
Simulation SIM;
CheckBox[] ChBox;
int LastServers = 0;
private void PaintChBox(int Num)
{
if (LastServers != 0)
for (int i = 0; i < LastServers; i++)
{
panel1.Controls.RemoveAt(panel1.Controls.Count - 1);
}
ChBox = new CheckBox[Num];
for (int i = 0; i < Num; i++)
{
ChBox[i] = new CheckBox();
ChBox[i].Location = new System.Drawing.Point(50, 95 + (i * 45));
ChBox[i].Text = "Server " + (i + 1).ToString();
ChBox[i].AutoSize = true;
ChBox[i].ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
ChBox[i].BackColor = Color.Transparent;
panel1.Controls.Add(ChBox[i]);
}
}
private bool CheckNumber(char MyChar)
{
if (MyChar != '1' && MyChar != '2' && MyChar != '3' && MyChar != '4' && MyChar != '5' && MyChar != '6' && MyChar != '7' && MyChar != '8' && MyChar != '9' && MyChar != '0')
return false;
else
return true;
}
private bool IsNumber(string MyNum)
{
bool dot = false;
if (MyNum == "" || MyNum == null) return false;
for (int i = 0; i < MyNum.Length; i++)
{
if (MyNum[i] == '.')
{
if (dot == true)
return false;
if (i == 0 || i == MyNum.Length - 1)
return false;
if (!CheckNumber(MyNum[i + 1]) || !CheckNumber(MyNum[i - 1]))
return false;
else
dot = true;
}
else
if (!CheckNumber(MyNum[i]))
return false;
}
return true;
}
private void Btn_Start_Click(object sender, EventArgs e)
{
if (CBox_ServerNum.SelectedItem.ToString() == "")
{
MessageBox.Show("Please Select Servers Num");
return;
}
if (TXT_TransActionTime.Text == "" || !IsNumber(TXT_TransActionTime.Text))
{
MessageBox.Show("Please Enter Valid TransActionTime");
return;
}
if (Txt_Arvtime.Text == "" || !IsNumber(Txt_Arvtime.Text))
{
MessageBox.Show("Please Enter Valid Arrivetime");
return;
}
if (Txt_SimLength.Text == "" || !IsNumber(Txt_SimLength.Text))
{
MessageBox.Show("Please Enter Valid Simulation Length");
return;
}
Txt_Queue.Text = "";
lbl_queue.Text = "";
int ServerNum = Convert.ToInt32(CBox_ServerNum.SelectedIndex + 1);
double TransActionTime = Convert.ToDouble(TXT_TransActionTime.Text);
double ArrTime = Convert.ToDouble(Txt_Arvtime.Text);
double SimLenght = Convert.ToDouble(Txt_SimLength.Text);
ArrTime = EhtemalCompute(ArrTime);
SIM = new Simulation(ServerNum, TransActionTime, ArrTime, SimLenght);
PaintChBox(ServerNum);
LastServers = ServerNum;
timer1.Start();
}
private double EhtemalCompute(double Num)
{
while (Num > 1)
{
Num /= 10;
}
return Num;
}
private void timer1_Tick(object sender, EventArgs e)
{
double Avtime;
int LostedNum;
int Complete;
SIM.Check(out Avtime, out LostedNum, out Complete);
bool Status = SIM.Add2Q();
if (Status) Txt_Queue.Text += "1";
if (SIM.Removed != -1)
{
Txt_Queue.Text = Txt_Queue.Text.Substring(1, Txt_Queue.Text.Length - 1);
SIM.Removed = -1;
}
for (int i = 0; i < ChBox.Length; i++)
{
ChBox[i].Checked = !SIM.MyServer[i].IsFree;
}
lbl_queue.Text = (SIM.Queue.GetNum() - 1).ToString();
if (Avtime != 0)
{
timer1.Stop();
timer1.Dispose();
MessageBox.Show(LostedNum.ToString());
MessageBox.Show(Avtime.ToString());
}
}
private void Btn_Stop_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Close();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
}
}
علاقه مندی ها (Bookmarks)