1. 程式人生 > >最短作業優先演算法

最短作業優先演算法

public class Operating {
 public static String dealTime(String time, int parMinutes) {
  StringTokenizer tokenizer = new StringTokenizer(time,":");
  String hour = tokenizer.nextToken();
  String minute = tokenizer.nextToken();
  int intHour = Integer.parseInt(hour);
  int intMinute = Integer.parseInt(minute);
  if((intMinute + parMinutes) < 60) {
   intMinute = intMinute + parMinutes;
  }else {
   intHour = (int)(intMinute + parMinutes)/60 + intHour;
   intMinute = (intMinute + parMinutes)%60;
  }
  if(intMinute < 10)
   return intHour+":0"+intMinute;
  else
   return intHour+":"+intMinute;
 }
 public static boolean compareTime(String time1,String time2) {
  
  StringTokenizer tokenizer1 = new StringTokenizer(time1,":");
  String hour1 = tokenizer1.nextToken();
  String minute1 = tokenizer1.nextToken();
  int intHour1 = Integer.parseInt(hour1);
  int intMinute1 = Integer.parseInt(minute1);
  
  StringTokenizer tokenizer2 = new StringTokenizer(time2,":");
  String hour2 = tokenizer2.nextToken();
  String minute2 = tokenizer2.nextToken();
  int intHour2 = Integer.parseInt(hour2);
  int intMinute2 = Integer.parseInt(minute2);
  
  if(intHour1 > intHour2) {
   return true;
  }else if (intHour1 == intHour2) {
   if(intMinute1 > intMinute2) {
    return true;
   }
  }
  return false;
 }
 public static void main(String[] args) throws Exception {
  int sumOverturn = 0;
  float sumWithPower = 0;
  int jobNum = 0;
  BufferedReader fromFile = new BufferedReader(new FileReader("in.txt"));
  Vector<Job> jobs = new Vector<Job>();  
  String jobInLine;
  while((jobInLine = fromFile.readLine()) !=null) {
   Job job = new Job();
   StringTokenizer  strTokenizer = new StringTokenizer(jobInLine);
   job.setName(strTokenizer.nextToken());
   job.setInTime(strTokenizer.nextToken());
   job.setMaybeRunTime(Integer.parseInt(strTokenizer.nextToken()));
   jobs.addElement(job);
  }  
  System.out.println("作業名稱 " + "/t進入 " + "/t開始" + "/t結束 " + "/t週轉時間 " + "/t帶權週轉時間 ");
  while(!jobs.isEmpty()) {
   Job toDealJob = new Job();
   int k = 0;
   for (int i = 0,j = 0;i < jobs.size();) {
    i = 0;
    j = 0;
    k = 0;
    toDealJob = jobs.elementAt(i);    
    for (j = i + 1;j < jobs.size(); j++) {
     String jInTime = jobs.elementAt(j).getInTime();
     String nowTime = Operating.dealTime(toDealJob.getInTime(),toDealJob.getWaitTime());
     
     
     if((jobs.elementAt(j).getMaybeRunTime() < toDealJob.getMaybeRunTime())
       && (Operating.compareTime(jInTime, nowTime))) {
       toDealJob = jobs.elementAt(j);
       k = j;
     }
    }
    String startTime = Operating.dealTime(toDealJob.getInTime(), toDealJob.getWaitTime());    
    toDealJob = jobs.elementAt(k);
    int overturn = toDealJob.getWaitTime() + toDealJob.getMaybeRunTime();
    float withPower = overturn/toDealJob.getMaybeRunTime();
    String endTime = Operating.dealTime(toDealJob.getInTime(), overturn);
    System.out.println(toDealJob.getName() + "/t" //作業名稱
         + toDealJob.getInTime()+ "/t" //進入
         + startTime +  "/t" //開始
         + endTime + "/t"//結束
         + overturn + "/t"
         + withPower);
    sumOverturn = sumOverturn + overturn;
    sumWithPower = sumWithPower + withPower;
    jobNum++;    
    for(int t = 0;t < jobs.size();t++) {
     jobs.elementAt(t).setWaitTime(jobs.elementAt(t).getWaitTime() + toDealJob.getMaybeRunTime());
    }
    jobs.remove(k);
   }
  }
  System.out.println();
  float averageOverturn = sumOverturn/jobNum;
  float averageWithPower = sumWithPower/jobNum;
  System.out.println("平均週轉時間為 :" + averageOverturn + " 分鐘");
  System.out.println("平均帶權週轉時間為 :" + averageWithPower + " 分鐘");
 }
}

/////////////////////////////////////////////////////////////////////////////////////////


in.txt的內容:

Job1 12:00 30
Job2 12:30 40
Job3 12:40 30
Job4 13:00 30
Job5 13:50 50
Job6 14:00 30
Job7 14:40 30
Job8 15:01 30