~~~
package experiment;
import java.util.*;
import java.text.DecimalFormat;
class Process
{
Process(){}
Process(String Name,int ArrivalTime,int ServiceTime)
{
this.Name=Name;
this.ArrivalTime=ArrivalTime;
this.ServiceTime=ServiceTime;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getArrivalTime() {
return ArrivalTime;
}
public void setArrivalTime(int arrivalTime) {
ArrivalTime = arrivalTime;
}
public int getServiceTime() {
return ServiceTime;
}
public void setServiceTime(int serviceTime) {
ServiceTime = serviceTime;
}
public int getFinishTime() {
return FinishTime;
}
public void setFinishTime(int finishTime) {
FinishTime = finishTime;
}
public int getWholeTime() {
return WholeTime;
}
public void setWholeTime(int wholeTime) {
WholeTime = wholeTime;
}
public double getWeightWholeTime() {
return WeightWholeTime;
}
public void setWeightWholeTime(double weightWholeTime) {
WeightWholeTime = weightWholeTime;
}
private String Name; //进程名称
private int ArrivalTime; //到达时间
private int ServiceTime;//服务时间
private int FinishTime; //完成时间
private int WholeTime;//周转时间
private double WeightWholeTime; //带权周转时间
public void WholeTime(Process[]process)
{
//求进程的周转时间 周转时间=完成时间-到达时间
for(int i=0;i process[i].setWholeTime(process[i].getFinishTime()-process[i].getArrivalTime()); } public void WeightWholeTime(Process[]process) { //求进程的带权周转时间 DecimalFormat a = new DecimalFormat(".##"); for(int i=0;i process[i].setWeightWholeTime(Double.parseDouble(a.format((double)process[i].getWholeTime()/(double)process[i].getServiceTime()))); } public double AVG_WholeTime(Process[]process) { //求进程的平均周转时间 DecimalFormat a = new DecimalFormat(".##"); double AVG_WholeTime=0; for(int i=0;i AVG_WholeTime=AVG_WholeTime+process[i].getWholeTime(); return ( Double.parseDouble(a.format(AVG_WholeTime/process.length)) ); } public double AVG_WeightWholeTime(Process[]process) { //求进程的平均带权周转时间 DecimalFormat a = new DecimalFormat(".##"); double AVG_WeightWholeTime=0; for(int i=0;i AVG_WeightWholeTime=AVG_WeightWholeTime+process[i].getWeightWholeTime(); return (Double.parseDouble(a.format(AVG_WeightWholeTime/process.length))); } public void SortWithArrivalTime(Process[]process) { //按到达时间排序 for(int i=0;i { for(int j=i+1;j { if(process[i].getArrivalTime()>process[j].getArrivalTime()) swap(process[i],process[j]); } } } public void SortWithServiceTime(Process[]process) { //按服务时间排序 int lastProcessFinishTime=0;//上一个进程的完成时间 for(int i=0;i { for(int j=i+1;j { //在到达时间排序的基础上,进行服务时间的排序 if(process[j].getArrivalTime() { if(process[i].getServiceTime()>process[j].getServiceTime()) swap(process[i],process[j]); } } lastProcessFinishTime=lastProcessFinishTime+process[i].getServiceTime(); } } public void swap(Process process1,Process process2) { //交换两个进程 Process newProcess=new Process(process1.getName(),process1.getArrivalTime(),process1.getServiceTime()); process1.setName(process2.getName()); process1.setArrivalTime(process2.getArrivalTime()); process1.setServiceTime(process2.getServiceTime()); process2.setName(newProcess.getName()); process2.setArrivalTime(newProcess.getArrivalTime()); process2.setServiceTime(newProcess.getServiceTime()); } public void ProcessDetail(Process[] process,int totalTime) { //打印进程的过程 for(int time=0;time<=totalTime;time++) { for(int i=0;i { if(process[i].getArrivalTime()==time) System.out.println("时刻"+time+"进程"+process[i].getName()+"到达"); if(i==0&&time System.out.println("时刻"+time+"进程"+process[i].getName()+"正在执行"); if(1<=i) { if(process[i-1].getFinishTime()<=time&&time System.out.println("时刻"+time+"进程"+process[i].getName()+"正在执行"); } if(process[i].getFinishTime()==time) System.out.println("时刻"+time+"进程"+process[i].getName()+"完成"); if(i { if(time>process[i].getFinishTime()&&time { while(time!=process[i+1].getArrivalTime()) { System.out.println("时刻"+time+"系统空闲"); time++; } } } } } } } class FCFS { public void PrintProcess(Process[] process) { //打印进程的名称,到达时间,服务时间 for(int i=0;i { System.out.println("第"+(i+1)+"个进程的名称"+process[i].getName()+" "+"到达时间"+process[i].getArrivalTime()+" "+"服务时间"+process[i].getServiceTime()+" "+"完成时间"+process[i].getFinishTime()+" "+"周转时间"+process[i].getWholeTime() +" "+"带权周转时间"+process[i].getWeightWholeTime()); } } public int FinishTime(Process[] process) { //求先来先服务算法的完成时间 //按到达时间排序 new Process().SortWithArrivalTime(process); int totalTime=0; int lastProcessFinishTime=process[0].getArrivalTime();//因为已经排序了 ,第一个进程到达的时间用来初始化上一个进程的完成时间 for(int i=0;i { if(i==0) { //第一个进程单独处理 process[i].setFinishTime(process[i].getServiceTime()+lastProcessFinishTime); lastProcessFinishTime=process[i].getServiceTime()+lastProcessFinishTime; } while(i!=0) { if(process[i].getArrivalTime()<=lastProcessFinishTime) { //当前进程到达时间小于上一个进程的完成时间 process[i].setFinishTime(process[i].getServiceTime()+lastProcessFinishTime); lastProcessFinishTime=process[i].getServiceTime()+lastProcessFinishTime; break; } while(process[i].getArrivalTime()>lastProcessFinishTime) { //当前进程在上一个进程完成时还没到达 lastProcessFinishTime++; } } } totalTime=lastProcessFinishTime;//返回给系统总时间 return totalTime; } } class SJF { public void PrintProcess(Process[] process) { //打印 for(int i=0;i { System.out.println("第"+(i+1)+"个进程的名称"+process[i].getName()+" "+"到达时间"+process[i].getArrivalTime()+" "+"服务时间"+process[i].getServiceTime()+" "+"完成时间"+process[i].getFinishTime()+" "+"周转时间"+process[i].getWholeTime() +" "+"带权周转时间"+process[i].getWeightWholeTime()); } } public void swap(Process process1,Process process2) { //交换两个进程 Process newProcess=new Process(process1.getName(),process1.getArrivalTime(),process1.getServiceTime()); process1.setName(process2.getName()); process1.setArrivalTime(process2.getArrivalTime()); process1.setServiceTime(process2.getServiceTime()); process2.setName(newProcess.getName()); process2.setArrivalTime(newProcess.getArrivalTime()); process2.setServiceTime(newProcess.getServiceTime()); } public int FinishTime(Process[] process) { //求短作业优先的完成时间 //在FCFS的基础上,再对服务时间进行排序 new Process().SortWithArrivalTime(process); new Process().SortWithServiceTime(process); int totalTime=0; int lastProcessFinishTime=process[0].getArrivalTime(); for(int i=0;i { if(i==0) { //第一个进程单独处理 process[i].setFinishTime(process[i].getServiceTime()+lastProcessFinishTime); lastProcessFinishTime=process[i].getServiceTime()+lastProcessFinishTime; } while(i!=0) { if(process[i].getArrivalTime()<=lastProcessFinishTime) { //当前进程到达时间小于上一个进程的完成时间 process[i].setFinishTime(process[i].getServiceTime()+lastProcessFinishTime); lastProcessFinishTime=process[i].getServiceTime()+lastProcessFinishTime; break; } while(process[i].getArrivalTime()>lastProcessFinishTime) { //当前进程在上一个进程完成时还没到达 lastProcessFinishTime++; } } } totalTime=lastProcessFinishTime; return totalTime; } } public class experiment { public static void sjf(Process[] process) { //进程个数为5 //预期输出结果如下 // 名称 到达时间 服务时间 完成时间 周转时间 带权周转时间 平均周转时间为 7.8 平均带权周转时间为2.233 // A 0 4 4 4 1.0 // B 1 3 9 8 2.666 // C 2 4 13 11 2.75 // D 3 2 6 3 1.5 // E 4 4 17 13 3.25 System.out.println("SJF算法结果如下:——————————————————"); int totalTime=new SJF().FinishTime(process); new Process(). WholeTime(process); new Process().WeightWholeTime(process); new Process().ProcessDetail( process, totalTime); System.out.println("SJF算法结束:——————————————————"); new SJF(). PrintProcess(process); System.out.println("平均周转时间为"+new Process().AVG_WholeTime(process)); System.out.println("平均带权周转时间为"+new Process().AVG_WeightWholeTime(process)); } public static void fcfs(Process[] process) { //进程个数为5 //预期输出结果如下 // 名称 到达时间 服务时间 完成时间 周转时间 带权周转时间 平均周转时间为 8.4 平均带权周转时间为2.7 // A 0 4 4 4 1.0 // B 1 3 7 6 2.0 // C 2 4 11 9 2.25 // D 3 2 13 10 5.0 // E 4 4 17 13 3.25 System.out.println("FCFS算法结果如下:——————————————————"); int totalTime=new FCFS().FinishTime(process); new Process().WholeTime(process); new Process().WeightWholeTime(process); new Process().ProcessDetail( process, totalTime); System.out.println("FCFS算法结束:——————————————————"); new FCFS().PrintProcess(process); System.out.println("平均周转时间为"+new Process().AVG_WholeTime(process)); System.out.println("平均带权周转时间为"+new Process().AVG_WeightWholeTime(process)); } public static void main(String[] args) { Scanner Input=new Scanner(System.in); // System.out.println("请输入进程个数:"); // int num=Input.nextInt(); // Process [] process=new Process[num]; // for(int i=0;i // { // System.out.println("请输入第"+(i+1)+"个进程的名称,到达时间,服务时间"); // String name=Input.next(); // int arrivalTime=Input.nextInt(); // int ServiceTime=Input.nextInt(); // process[i]=new Process(name,arrivalTime,ServiceTime); // } Process [] process=new Process[5]; process[0]=new Process("A",0,4); process[1]=new Process("B",1,3); process[2]=new Process("C",2,4); process[3]=new Process("D",3,2); process[4]=new Process("E",4,4); // process[0]=new Process("A",1,2); // process[1]=new Process("B",6,3); // process[2]=new Process("C",14,5); // process[3]=new Process("D",9,2); // process[4]=new Process("E",20,3); //考虑乱序,两个进程之间产生空闲时间 System.out.println("选择FCFS算法请输入1,选择SJF算法请输入2,退出请输入0"); String exe=Input.next(); Input.close(); if(Integer.parseInt(exe)==1) fcfs(process); if(Integer.parseInt(exe)==2) sjf(process); if(Integer.parseInt(exe)==0) return; } } ~~~