⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 C#写的模拟磁盘磁头调度程序 包含fcfs sstf elevator3种算法
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Diskatt
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox InBox;
		private System.ComponentModel.IContainer components;
		string input;
		string [] process;
		char a='\n';
		private System.Windows.Forms.Button BtRead;
		private System.Windows.Forms.Label label1;
		public System.Windows.Forms.Label ReadedBox;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton radioButton1;
		private System.Windows.Forms.RadioButton radioButton2;
		private System.Windows.Forms.RadioButton radioButton3;
		int [] track;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox BeginBox;
		private System.Windows.Forms.Label BeginTrack;
		private System.Windows.Forms.Button Btreset;
		private System.Windows.Forms.Button BtRun;
		int begin=0;
		int algocase=0;
		private System.Windows.Forms.Label ResultBox;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Panel PnDraw;
		private System.Windows.Forms.Button BtSim;
		private System.Windows.Forms.Button BtReSim;
		int [] result;
		int x=210;
		int simnum=0;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.InBox = new System.Windows.Forms.TextBox();
			this.BtRead = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.ReadedBox = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.radioButton3 = new System.Windows.Forms.RadioButton();
			this.radioButton2 = new System.Windows.Forms.RadioButton();
			this.radioButton1 = new System.Windows.Forms.RadioButton();
			this.BeginBox = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.BeginTrack = new System.Windows.Forms.Label();
			this.Btreset = new System.Windows.Forms.Button();
			this.BtRun = new System.Windows.Forms.Button();
			this.ResultBox = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.PnDraw = new System.Windows.Forms.Panel();
			this.BtSim = new System.Windows.Forms.Button();
			this.BtReSim = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// InBox
			// 
			this.InBox.AcceptsReturn = true;
			this.InBox.AcceptsTab = true;
			this.InBox.AutoSize = false;
			this.InBox.Location = new System.Drawing.Point(440, 32);
			this.InBox.Multiline = true;
			this.InBox.Name = "InBox";
			this.InBox.Size = new System.Drawing.Size(152, 224);
			this.InBox.TabIndex = 0;
			this.InBox.Text = "";
			// 
			// BtRead
			// 
			this.BtRead.Location = new System.Drawing.Point(448, 272);
			this.BtRead.Name = "BtRead";
			this.BtRead.Size = new System.Drawing.Size(136, 32);
			this.BtRead.TabIndex = 1;
			this.BtRead.Text = "Read";
			this.BtRead.Click += new System.EventHandler(this.BtRead_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(440, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(112, 24);
			this.label1.TabIndex = 2;
			this.label1.Text = "Input HD Serial";
			// 
			// ReadedBox
			// 
			this.ReadedBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.ReadedBox.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.ReadedBox.Location = new System.Drawing.Point(280, 32);
			this.ReadedBox.Name = "ReadedBox";
			this.ReadedBox.Size = new System.Drawing.Size(152, 224);
			this.ReadedBox.TabIndex = 3;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(272, 8);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(128, 24);
			this.label2.TabIndex = 4;
			this.label2.Text = "Readed Serial";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.radioButton3);
			this.groupBox1.Controls.Add(this.radioButton2);
			this.groupBox1.Controls.Add(this.radioButton1);
			this.groupBox1.Location = new System.Drawing.Point(88, 24);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(168, 128);
			this.groupBox1.TabIndex = 5;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Algorithms";
			// 
			// radioButton3
			// 
			this.radioButton3.Location = new System.Drawing.Point(16, 96);
			this.radioButton3.Name = "radioButton3";
			this.radioButton3.Size = new System.Drawing.Size(120, 24);
			this.radioButton3.TabIndex = 2;
			this.radioButton3.Text = "Elevator";
			this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
			// 
			// radioButton2
			// 
			this.radioButton2.Location = new System.Drawing.Point(16, 56);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.Size = new System.Drawing.Size(128, 24);
			this.radioButton2.TabIndex = 1;
			this.radioButton2.Text = "SSTF";
			this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
			// 
			// radioButton1
			// 
			this.radioButton1.Location = new System.Drawing.Point(16, 16);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.Size = new System.Drawing.Size(120, 24);
			this.radioButton1.TabIndex = 0;
			this.radioButton1.Text = "FCFS";
			this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
			// 
			// BeginBox
			// 
			this.BeginBox.Location = new System.Drawing.Point(16, 192);
			this.BeginBox.Name = "BeginBox";
			this.BeginBox.Size = new System.Drawing.Size(72, 20);
			this.BeginBox.TabIndex = 6;
			this.BeginBox.Text = "0";
			this.BeginBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(96, 184);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(80, 32);
			this.button1.TabIndex = 7;
			this.button1.Text = "Set as Begin Track";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// BeginTrack
			// 
			this.BeginTrack.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.BeginTrack.Location = new System.Drawing.Point(184, 192);
			this.BeginTrack.Name = "BeginTrack";
			this.BeginTrack.Size = new System.Drawing.Size(88, 16);
			this.BeginTrack.TabIndex = 8;
			this.BeginTrack.Text = "0";
			// 
			// Btreset
			// 
			this.Btreset.Location = new System.Drawing.Point(48, 240);
			this.Btreset.Name = "Btreset";
			this.Btreset.Size = new System.Drawing.Size(192, 32);
			this.Btreset.TabIndex = 9;
			this.Btreset.Text = "Reset";
			this.Btreset.Click += new System.EventHandler(this.Btreset_Click);
			// 
			// BtRun
			// 
			this.BtRun.Location = new System.Drawing.Point(288, 272);
			this.BtRun.Name = "BtRun";
			this.BtRun.Size = new System.Drawing.Size(136, 32);
			this.BtRun.TabIndex = 10;
			this.BtRun.Text = "Run";
			this.BtRun.Click += new System.EventHandler(this.BtRun_Click);
			// 
			// ResultBox
			// 
			this.ResultBox.Location = new System.Drawing.Point(8, 320);
			this.ResultBox.Name = "ResultBox";
			this.ResultBox.Size = new System.Drawing.Size(648, 24);
			this.ResultBox.TabIndex = 11;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 296);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(112, 16);
			this.label3.TabIndex = 12;
			this.label3.Text = "Produced Serial";
			// 
			// PnDraw
			// 
			this.PnDraw.Location = new System.Drawing.Point(8, 352);
			this.PnDraw.Name = "PnDraw";
			this.PnDraw.Size = new System.Drawing.Size(512, 240);
			this.PnDraw.TabIndex = 13;
			this.PnDraw.Paint += new System.Windows.Forms.PaintEventHandler(this.PnDraw_Paint);
			// 
			// BtSim
			// 
			this.BtSim.Location = new System.Drawing.Point(544, 360);
			this.BtSim.Name = "BtSim";
			this.BtSim.Size = new System.Drawing.Size(96, 32);
			this.BtSim.TabIndex = 14;
			this.BtSim.Text = "Simulate";
			this.BtSim.Click += new System.EventHandler(this.BtSim_Click);
			// 
			// BtReSim
			// 
			this.BtReSim.Location = new System.Drawing.Point(544, 424);
			this.BtReSim.Name = "BtReSim";
			this.BtReSim.Size = new System.Drawing.Size(96, 32);
			this.BtReSim.TabIndex = 15;
			this.BtReSim.Text = "Reset Simulate";
			this.BtReSim.Click += new System.EventHandler(this.BtReSim_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(664, 606);
			this.Controls.Add(this.BtReSim);
			this.Controls.Add(this.BtSim);
			this.Controls.Add(this.PnDraw);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.ResultBox);
			this.Controls.Add(this.BtRun);
			this.Controls.Add(this.Btreset);
			this.Controls.Add(this.BeginTrack);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.BeginBox);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.ReadedBox);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.BtRead);
			this.Controls.Add(this.InBox);
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "Disk Attemper";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void BtRead_Click(object sender, System.EventArgs e)
		{
			int i;
			input=InBox.Text;
			if (input=="")
				return;
			process=input.Split(a);
			track=new int[process.Length];
			for (i=0;i<process.Length;i++)
				track[i]=Convert.ToInt32(process[i]);
			ReadedBox.Text=InBox.Text;
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			begin=Convert.ToInt32(BeginBox.Text);
			BeginTrack.Text=BeginBox.Text;
			x=begin;
			PnDraw.Invalidate();
		}

		private void Btreset_Click(object sender, System.EventArgs e)
		{
			BeginTrack.Text="0";
			BeginBox.Text="0";
			ReadedBox.Text="";
			InBox.Text="";
			ResultBox.Text="";
			begin=0;
			x=210;
			simnum=0;
			track=null;
			result=null;
		}

		private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
		{
			algocase=1;
		}

		private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
		{
			algocase=2;
		}

		private void radioButton3_CheckedChanged(object sender, System.EventArgs e)
		{
			algocase=3;
		}

		private void BtRun_Click(object sender, System.EventArgs e)
		{
			simnum=0;
			switch(algocase)
			{
				case 1:
					if(FCFS(track,ref result))
						   fill();
					break;
				case 2:
					if(SSTF(track,ref result,begin))
						fill();
					break;
				case 3:
					if(Elevator(track,ref result,begin))
						fill();
					break;
			}
		}
		
		private bool FCFS(int [] x,ref int [] y)
		{
			int i=0;
			if (x==null)
				return false;
			y=new int[x.Length];
			for (i=0;i<x.Length;i++)
			{
				y[i]=x[i];
			}
			return true;
		}

		private bool SSTF(int [] u,ref int [] y,int z)
		{
			int position=0;
			int i=0,j=0;
			int now,distance;
			if (u==null)
				return false;
			int [] x= new int [u.Length];
			for (i=0;i<u.Length;i++)
				x[i]=u[i];
			y=new int[x.Length];
			now=z;
			for (i=0;i<x.Length;i++)
			{
				distance=999;
				for(j=0;j<x.Length;j++)
				{
					if(x[j]!=0)
					{
						if (distance>Math.Abs(x[j]-now))
						{
							distance=Math.Abs(x[j]-now);
							position=j;
						}
					}
				}
				y[i]=x[position];
				now=x[position];
				x[position]=0;
			}
			x=null;
			return true;
		}
		
		private bool Elevator(int [] u,ref int [] y,int z)
		{
			int i=0,j=0,k=0;
			int now;
			int [] inner,outter;
			if (u==null)
				return false;
			int [] x= new int [u.Length];
			for (i=0;i<u.Length;i++)
				x[i]=u[i];
			y=new int[x.Length];
			now=z;
			for (i=0,j=0;i<x.Length;i++)
			{
				if (x[i]<=z)
					j++;
			}
			inner=new int[j];
			outter=new int[x.Length-j];
			for (i=0,j=0,k=0;i<x.Length;i++)
			{
				if (x[i]<=z)
				{
					inner[j]=x[i];
					j++;
				}
				if (x[i]>z)
				{
					outter[k]=x[i];
					k++;
				}
			}
			Array.Sort(inner);
			Array.Reverse(inner);
			Array.Sort(outter);
			if (inner.Length==x.Length)
			{
				for (i=0;i<x.Length;i++)
				{
					y[i]=inner[i];
				}
				return true;
			}
			if (outter.Length==x.Length)
			{
				for (i=0;i<x.Length;i++)
				{
					y[i]=outter[i];
				}
				return true;
			}
			if ((x[0]-z)<0)
			{
				for(i=0;i<inner.Length;i++)
				{
					y[i]=inner[i];
				}
				for(i=0;i<outter.Length;i++)
				{
					y[inner.Length+i]=outter[i];
				}
				return true;
			}
			if ((x[0]-z)>0)
			{
				for(i=0;i<outter.Length;i++)
				{
					y[i]=outter[i];
				}
				for(i=0;i<inner.Length;i++)
				{
					y[outter.Length+i]=inner[i];
				}
				return true;
			}
			return false;
		}

		private void fill()
		{
			ResultBox.Text="";
			ResultBox.Text+=Convert.ToString(begin);
			for (int i=0;i<result.Length;i++)
			{
				ResultBox.Text+="->";
				ResultBox.Text+=Convert.ToString(result[i]);
			}
		}

		private void PnDraw_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			Graphics g=e.Graphics;
			Pen pen= new Pen(Color.DarkBlue);
			for(int i=0;i<10;i++)
			{
				g.DrawEllipse(pen,0+i*20,20+i*10,400-i*40,200-i*20);
			}
			g.DrawLine(pen,200+x,100,200+x,120);
			g.DrawLine(pen,200+x,100,400,0);
		}

		private void BtSim_Click(object sender, System.EventArgs e)
		{
			if (simnum<result.Length)
			{
				x=result[simnum];
				simnum++;
			}
			PnDraw.Invalidate();
		}

		private void BtReSim_Click(object sender, System.EventArgs e)
		{
			simnum=0;
			x=begin;
			PnDraw.Invalidate();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -