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

📄 frmdiskacess.cs

📁 磁盘调度程序
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DiskAcess
{
    

    public partial class FrmDiskAcess : Form
    {
        public FrmDiskAcess()
        {
            InitializeComponent();
        }

        private void butFCFS_Click(object sender, EventArgs e)
        {
            string str1 = this.InputNumber.Text; //输入的磁道访问序列
            str1 = str1.Trim();
            string[] number1 = str1.Split(new char[] { ' ',',','.' });
            int[] number2 = new int[number1.Length];
                       
            
            string str2 = this.cylinder.Text;    //柱面总数
            int cylinder = Convert.ToInt32(str2); 

            int exe = 0;
            if (str2 == "0")
            {
                MessageBox.Show("请输入柱面总数!");
                this.cylinder.Focus();
            }

            else
            {

                //将string类型转化为int类型
                for (int i = 0; i < number1.Length; i++)
                {
                    number2[i] = Convert.ToInt32(number1[i]);
                }

                //判断是否有大于柱面的磁道

                for (int i = 0; i < number2.Length; i++)
                {
                    if (number2[i] > cylinder)
                    {
                        MessageBox.Show("访问序列中第" + (i + 1) + "个元素超出柱面范围!");
                        this.InputNumber.Focus();
                        exe = 1;
                    }
                }
            }
                //当前磁头位置
            if (exe == 0)
            {
                int position = Convert.ToInt32(this.InputPosition.Text);

                string output1 = "";      //调度过程
                int SumTrack = 0;         //总磁道数

                //先来先服务算法:根据访问的序列,从当前位置转到第一个访问位,依次进行,磁道总
                //数为相应两磁道位置之差的总和
                SumTrack = Math.Abs(position - number2[0]);
                for (int i = 1; i < number2.Length; i++)
                {
                    int n = 0;
                    n = Math.Abs(number2[i] - number2[i - 1]);
                    SumTrack += n;
                }

                output1 += "磁盘调度前位置 " + position.ToString() + ";";
                output1 += " 磁盘调度过程如下:";

                //磁头移动位置转化成string类型
                for (int i = 0; i < number2.Length; i++)
                {
                    output1 += "→";
                    output1 += number2[i].ToString();
                }
                output1 += " 磁头移动的总磁道数:" + SumTrack;
                this.Output.Text = output1;
            }
        }

        private void butTime_Click(object sender, EventArgs e)
        {
            string str1 = this.InputNumber.Text;                  //输入的磁道访问序列
            str1 = str1.Trim();
            string[] number1 = str1.Split(new char[] { ' ' });    //将字符串以空格为界分装到数组中
            int[] number2 = new int[number1.Length];
            string str2 = this.cylinder.Text;    //柱面总数
            int cylinder = Convert.ToInt32(str2);

            int exe = 0;
            if (str2 == "0")
            {
                MessageBox.Show("请输入柱面总数!");
                this.cylinder.Focus();
            }
            else  
            {
                //将string类型转化为int类型
                for (int i = 0; i < number1.Length; i++)
                {
                    number2[i] = Convert.ToInt32(number1[i]);
                }
                //判断是否有大于柱面的磁道

                for (int i = 0; i < number2.Length; i++)
                {
                   if (number2[i] > cylinder)
                   {
                       MessageBox.Show("访问序列中第" + (i + 1) + "个元素超出柱面范围!");
                       this.InputNumber.Focus();
                       exe = 1;
                   }
                }
             }
             if (exe == 0)
             {

                ListAcess[] array = new ListAcess[number2.Length];  //类数组

                for (int i = 0; i < array.Length; i++)
                {
                array[i] = new ListAcess();     //分配内存
                array[i].setDis(number2[i]);
                array[i].setVisit(false);
                }

                int position = Convert.ToInt32(this.InputPosition.Text); //当前磁道
                string output2 = "";
                output2 += "磁盘调度前位置:" + position.ToString() + "; 磁盘调度过程如下: ";


                //最短寻道时间优先算法实现
                int SumTrack = 0;
                for (int i = 0; i < array.Length; i++)
                {
                int dis = int.MaxValue;
                int pos = -1;
                for (int j = 0; j < array.Length; j++)
                {
                    if (array[j].getVisit())
                        continue;
                    int n = Math.Abs(position - array[j].getDis());

                    if (n < dis)
                    {
                        dis = n;
                        pos = j;
                    }
                }
                SumTrack += dis;
                output2 += "→" + array[pos].getDis().ToString();
                array[pos].setVisit(true);
                position = array[pos].getDis();

                }
                output2 += " 磁头移动的总磁道数: " + SumTrack;
                this.Output.Text = output2;
                }

          }
        public class ListAcess
        {
            private int dis;
            private bool visit;

            public int getDis()
            {
                return dis;
            }
            public bool getVisit()
            {
                return visit;
            }
            public void setDis(int array)
            {
                this.dis = array;
            }
            public void setVisit(bool x)
            {
                this.visit = x;
            }
        }  

        private void butclear_Click(object sender, EventArgs e)
        {
            this.Output.Text = "";
            if (this.InputPosition.Text == "")
            {
                MessageBox.Show("请输入要访问的磁道!");
                this.InputPosition.Focus();
            }
        }

        private void butClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

⌨️ 快捷键说明

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