磁盘调度算法java代码

第一步,创建一个CSCAN类

public class CSCAN {
    public int m=0; //用来存放磁头的初始位置
     public boolean Run=true;
     public int sum=0;
     public void Check(int a[],int n,int position){
         int temp;
         for (int i = n; i > 0; i--)
         {    // 冒泡排序
              for (int j = 0; j < i; j++){
    if (a[j] > a[j + 1]) {      // 按顺序依次比较大小
         temp = a[j]; // 把大的数字往前面放
         a[j] = a[j + 1];
         a[j + 1] = temp;
    }
              }
         }
         while (Run)
         {
             for (int i = 0; i <= n; i++){
                 if (a[i] == position){      m=i;
                 Run=false;
                 }
             }
         }
         System.out.println("CSCAN算法序列:");
         for(int i=m+1;i<=n;i++)
         {    //磁头向大号移动
              sum=sum+a[i]-a[i-1];
     System.out.print(a[i]+"   ");
         }
        sum=sum+200-a[n];
         sum=sum+199;
        sum=sum+a[0]-1;
     for(int i=0;i<=m-1;i++){
         if(i!=m-1){
             sum=sum+a[i+1]-a[i];
         }
         System.out.print(a[i]+"   ");
     }
     System.out.println();
     System.out.println("寻道长度为"+sum);
     System.out.println("平均寻道长度为"+sum/n);   }
}

第二步,创建一个DiskManage类

import java.util.Scanner;

public class DiskManage {

    public static void main (String [] args){
        int a[]=new int[20];
        int n=0;
        int m=0;
        double sum=0;
        double temp=0;
        int position=0;
        SSTF sstf=new SSTF();
        SCAN scan = new SCAN();
        CSCAN cscan = new CSCAN();
        System.out.println("请输入磁盘请求的个数:");
        try{
        Scanner reader=new Scanner(System.in);   n=reader.nextInt();  }
        catch(Exception e){ }
        System.out.println("请输入磁盘请求序列:");
        Scanner reader = new Scanner(System.in);
        for (int i = 1; i <= n; i++) {   try{
            a[i]=reader.nextInt();        }
            catch(Exception e){       }  }
        System.out.print("请输入磁头的初始位置:");

       // try{
            Scanner re = new Scanner(System.in);
            a[0]=re.nextInt();   position=a[0];   //  }
    //    catch(for (int i = 1; i <= n; i++){Exception e){     }
     //   System.out.print(a[i] + "    ");  }
      System.out.println("*****************************************************************");
        System.out.println("提示:磁头的序列范围1~200!磁头从小号到大号寻道!");
        System.out.println("请输入相应的选择操作::");
        System.out.println("1、先来先服务算法(FCFS)");
        System.out.println("2、最短寻道时间优先算法(SSTF)");
        System.out.println("3、扫描算法(SCAN)");
        System.out.println("4、循环扫描算法(CSCAN)");
        Scanner read = new Scanner(System.in);
        try{    m=read.nextInt();      }
        catch(Exception e){     }
        System.out.println("m的值为" + m);
        read.close();
        switch (m) {
   case 1:   sum=0;
   System.out.println("FCFS算法序列:");
   for (int i = 1; i <= n; i++){
       System.out.print(a[i] + "    ");
       temp = a[i] - a[i - 1];
       if (temp >= 0);
       else if (temp < 0) {     temp=-temp;         }
       sum=sum+temp;   }    System.out.println();
   System.out.println("sum的值为" + sum);
   System.out.println("平均寻到长度为" + sum / n);break;
   case 2:    sstf.Calculate(a, n, position);    break;
   case 3: scan.Check(a, n, position);    break;
   case 4:    cscan.Check(a, n, position);   break;     }
    }
}

第三步,创建一个SCAN类

public class SCAN {
    public int m=0; //用来存放磁头的初始位置
     public boolean Run=true;
     public int sum=0;
     public void Check(int a[],int n,int position){
         int temp;    for (int i = n; i > 0; i--) // 冒泡排序
              {     for (int j = 0; j < i; j++){
                  if (a[j] > a[j + 1]) // 按顺序依次比较大小
                    {       temp = a[j]; // 把大的数字往前面放
                         a[j] = a[j + 1];
                         a[j + 1] = temp;     }    }   }
                         while (Run) {//此循环用来寻找磁头的初始位置被排到了什么位置
                              for (int i = 0; i <= n; i++)
                              {     if (a[i] == position) {
                                  m = i;
                                  Run = false;
                              }
                              }
                         }
                         System.out.println("SCAN算法序列:");
                         for(int i=m+1;i<=n;i++){//磁头向大号移动
            sum=sum+a[i]-a[i-1];
            System.out.print(a[i]+"   ");
                         }
            sum=sum+200-a[n];
                         sum=sum+200-a[m-1];
            for(int i=m-1;i>=0;i--)
            {    if(i!=0){
                sum=sum+a[i]-a[i-1];
            }
                System.out.print(a[i]+"   ");
            }
            System.out.println();
            System.out.println("寻到长度为"+sum);
            System.out.println("平均寻到长度为"+sum/n);
     }
            }

第四步,创建一个SSTF类

public class SSTF {

    public int m; // 用来判断排序后 磁头所在的初始位置的下标
     public int b[];   public int rflag = 0, lflag = 0;
     public double SUM=0;  public int mleft, mright;
     public SSTF(){    b=new int[20];   }
     public void Calculate(int a[], int n, int position)
     {
         int temp;
         for (int i = n; i > 0; i--) // 冒泡排序
               {
                   for (int j = 0; j < i; j++)
                   {
       if (a[j] > a[j + 1]) // 按顺序依次比较大小
              {        temp = a[j]; // 把大的数字往前面放
                         a[j] = a[j + 1];
                         a[j + 1] = temp;      }
                   }
               }
                         if (a[n] == position) {//如果磁头的初始位置在最大号
                               System.out.println("SSTF序列为:");
                               for (int i = 0; i <= n; i++)
                               {      System.out.print(a[i] + "     ");
                               }
                         }
                         else if (a[0] == position)
                         {      System.out.println("SSTF序列为:");
                         for (int i = n; i >= 0; i--)
                         {      System.out.print(a[i] + "     ");
                         }
                         }
                         else {
                             System.out.println("SSTF序列为:");
                         Check(a, n, position);
                         }
     }
                  public void Check(int a[], int n, int position)
                  {
                      boolean Run = true, Running = true;
                  while (Run) {//此循环用来寻找磁头的初始位置被排到了什么位置
                       for (int i = 0; i <= n; i++){
                           if (a[i] == position)
                           {
                               m = i;
                           System.out.println("m的值为" + m);
                           mleft = m - 1;
                           mright = m + 1;
                           Run = false;
                           }
                       }
                  }
                  while (Running){
                      if (a[m] - a[mleft] >= a[mright] - a[m] && rflag != 1)
                      {
                          SUM=SUM+(a[mright] - a[m]); //用来统计寻到长度
                            m=mright;
                            mright++;
               if (mright > n){
                   rflag = 1;
               }
               System.out.print(a[m] + "   ");
                      }
                      else if (a[m] - a[mleft] < a[mright] - a[m] && lflag != 1)
                      {       SUM=SUM+(a[m] - a[mleft]);
                      m = mleft;
                      mleft--;
                      if (mleft < 0){
                          lflag = 1;
                      }
                      System.out.print(a[m] + "   ");
                      }
                      if (mleft != 0 && mright > n){
                          Running = false;
                      SUM=SUM+a[n]-a[mleft];
                      for (int j = mleft; j >= 0; j--) {
                          if(j!=0){
                          SUM=SUM+a[j]-a[j-1];       }
                System.out.print(a[j] + "   ");
                      }
                      }
                      if (mright != n && mleft < 0)
                      {
                          Running = false;
                          SUM=SUM+a[mright]-a[0];
                      for (int k = mright; k <= n; k++)
                      {
                          if(k!=0){
                              SUM=SUM+a[k+1]-a[k];
                          }
                      System.out.print(a[k] + "   ");
                      }
                      }
                  }
                  System.out.println();
                  System.out.println("sum的值为"+SUM);
                  System.out.println("平均寻到长度为"+SUM/n);    }

}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寻梦&之璐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值