c# 磁碟排程演算法:-最短尋道時間優先
阿新 • • 發佈:2018-12-26
Form1:
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; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { sys s = new sys(); s.input(textBox1.Text); s.running(); s.display(); textBox1.Text = ""; } } }
sys.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public class sys { private static int startPosition = 100;//磁頭開始位置 private static int totalMoving = 0; public static List<int> visitList = new List<int>();//訪問磁軌列表 private static int size; public static string sstr; public void input(string s) { string str; string[] array; str = s; array = str.Split(new char[] { ' ' }); try { foreach (string into in array) { visitList.Add(Convert.ToInt32(into)); } size = visitList.Count; } catch { MessageBox.Show("error"); } } public void running() { int visitNumber = 0; for (int i = 0, size = visitList.Count(); i < size; i++) { visitNumber = searchMinimum(startPosition); //System.out.println("當前訪問磁軌:"+visitNumber); sstr += visitNumber.ToString()+"\n"; } } public static int searchMinimum(int startPosition) { int min = Int32.MaxValue, index = 0, i = 0, thisPosition = 0, computering = 0; foreach (int it in visitList) { computering = Math.Abs(startPosition - it); if (min > computering) { index = i; min = computering; } i++; } thisPosition = visitList.ElementAt<int>(index); visitList.RemoveAt(index); totalMoving += Math.Abs(startPosition - thisPosition); sys.startPosition = thisPosition; return thisPosition; } public void display() { //MessageBox.Show("time is :" + ((double)totalMoving / size).ToString()); sstr += ((double)totalMoving / size).ToString(); MessageBox.Show(sstr); } } }