关闭

最短作业优先算法

标签: 作业算法stringimportfloatexception
4030人阅读 评论(2) 收藏 举报
 

public class Job {
 private String name = "";
 private String inTime = "";
 private int maybeRunTime = 0;
 private int waitTime = 0;
 private int turnoverTime = 0;
 private int withPoweroverTime = 0;
 public int getTurnoverTime() {
  return turnoverTime;
 }
 public void setTurnoverTime(int turnoverTime) {
  this.turnoverTime = turnoverTime;
 }
 public int getWithPoweroverTime() {
  return withPoweroverTime;
 }
 public void setWithPoweroverTime(int withPoweroverTime) {
  this.withPoweroverTime = withPoweroverTime;
 }
 public String getInTime() {
  return inTime;
 }
 public void setInTime(String inTime) {
  this.inTime = inTime;
 }
 public int getMaybeRunTime() {
  return maybeRunTime;
 }
 public void setMaybeRunTime(int maybeRunTime) {
  this.maybeRunTime = maybeRunTime;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getWaitTime() {
  return waitTime;
 }
 public void setWaitTime(int waitTime) {
  this.waitTime = waitTime;
 }
 
}


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

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.Vector;

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:80557次
    • 积分:949
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:20篇
    • 译文:0篇
    • 评论:14条
    最新评论