本代码包含了
1.先来先服务算法
2.最短寻找时间优先调度算法
3.单向扫描调度算法
4.双向扫描调度算法
5.电梯调度算法
代码如下:
//测试例子 98 183 37 122 14 124 65 67 -1
//测试例子 98 183 37 122 14 124 65 67 -1
//测试例子 98 183 37 122 14 124 65 67 -1
//测试例子 98 183 37 122 14 124 65 67 -1
import java.util.*;
public class ArmShiftSchedulingAlgorithm {
private int arm;//模拟移动臂
private int distance = 0;//保存移动臂移动距离
private int surfaceNum = 0;//保存柱面数
//生成队列保存输入序列
LinkedList<Integer> intputQueue(){
Scanner in = new Scanner(System.in);
LinkedList<Integer> inputQueue = new LinkedList<Integer>();
int temp = -1;
temp = in.nextInt();
while(temp != -1) {
inputQueue.offer(temp);
temp = in.nextInt();
}
return inputQueue;
}
//模拟移动臂移动
void move(int address) {
for(int i = 0;i < Math.abs(address - arm);i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("到达柱面" + address + " >> ");
distance += Math.abs(address - arm);
arm = address;
}
//输出访问序列
void showOutputQueue(LinkedList<Integer> outputQueue) {
System.out.print("柱面响应序列为:");
while(!outputQueue.isEmpty()) {
System.out.print(outputQueue.poll() + " ");
}
System.out.println();
System.out.println("总移动柱面个数:" + distance + "\n");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
}
//生成选择菜单
@SuppressWarnings("resource")
void menu() {
System.out.print("请你输入请求访问的柱面序列(以-1作为结束标志):");
LinkedList<Integer> intputQueue = intputQueue();
System.out.print("请输入移动臂的初始位置:");
arm = (new Scanner(System.in)).nextInt();
System.out.print("请输入最里的柱面号:");
surfaceNum = (new Scanner(System.in)).nextInt();
System.out.println("\n请你选择算法");
System.out.println("1.先来先服务算法");
System.out.println("2.最短寻找时间优先调度算法");
System.out.println("3.单向扫描调度算法");
System.out.println("4.双向扫描调度算法");
System.out.println("5.电梯调度算法");
System.out.println("0.退出");
System.out.println("");
System.out.print("请选择:");
int selectFunction = (new Scanner(System.in)).nextInt();
while(selectFunction != 0) {
switch(selectFunction) {
case 1:
System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("\n\t\t先来先服务算法\n");
execFiFOArithmetic(intputQueue);
break;
case 2:
System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("\n\t\t最短寻找时间优先调度算法\n");
execSSTFArithmetic(intputQueue);
break;
case 3:
System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("\n\t\t单向扫描调度算法\n");
execOneWayScanArithmetic(intputQueue);
break;
case 4:
System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("\n\t\t双向扫描调度算法\n");
execDoubleWayScanArithmetic(intputQueue);
break;
case 5:
System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("\n\t\t电梯调度算法\n");
execElevatorSchedulingArithmetic(intputQueue);
break;
default:
System.out.println("\n你的输入不正确!");
break;
}
System.out.print("请你输入请求访问的柱面序列(以-1作为结束标志):");
intputQueue = intputQueue();
System.out.print("请输入移动臂的初始位置:");
arm = (new Scanner(System.in)).nextInt();
System.out.print("请输入最里的柱面号:");
surfaceNum = (new Scanner(System.in)).nextInt();
System.out.println("\n请你选择算法");
System.out.println("1.先来先服务算法");
System.out.println("2.最短寻找时间优先调度算法");
System.out.println("3.单向扫描调度算法");
System.out.println("4.双向扫描调度算法");
System.out.println("5.电梯调度算法");
System.out.println("0.退出");
System.out.println("");
System.out.print("请选择:");
selectFunction = (new Scanner(System.in)).nextInt();
distance = 0;
}
}
//FIFO算法
LinkedList<Integer> FIFOArithmetic(LinkedList<Integer> queue1){
LinkedList<Integer> outputQueue = new LinkedList<Integer>();
while(!queue1.isEmpty()) {
int popNum = queue1.poll();
outputQueue.offer(popNum);
move(popNum);
}
System.out.println("");
return outputQueue;
}
void execFiFOArithmetic(LinkedList<Integer> InputQueue) {
showOutputQueue( FIFOArithmetic(InputQueue));
}
//SSTF算法
//1.先创建一个临时队列
//2.再把队列的值和当前arm的值相减
//3.从队列中找出最小的值
//4.将该值加上当前arm的值后进入一个新的队列
//5,将新进入的值从旧的队列中删除
//6.重复1直到队列为空
LinkedList<Integer> SSTFArithmetic(LinkedList<Integer> queue1){
LinkedList<Integer> outputQueue = new LinkedList<Integer>();
while(!queue1.isEmpty()) {
findMin2AddOutputQueue(queue1,outputQueue);
}
return outputQueue;
}
//把队列中的最小值添加到输出对列
void findMin2AddOutputQueue(LinkedList<Integer> InputQueue,LinkedList<Integer> outputQueue) {
int min = -999999;
int temp = 0;
LinkedList<Integer> tempQueue = new LinkedList<Integer>(InputQueue);
Iterator<Integer> it = tempQueue.iterator();
int i = 1;
int minNum = 1;
if(it.hasNext())
min = it.next()- arm;
while(it.hasNext()) {
temp = it.next() - arm;
i++;
if(Math.abs(temp) < Math.abs(min)) {
min = temp;
minNum = i;
}
}
outputQueue.offer(min + arm);
InputQueue.remove(minNum - 1);
move(min + arm);
}
void execSSTFArithmetic(LinkedList<Integer> InputQueue) {
LinkedList<Integer> outputQueue = SSTFArithmetic(InputQueue);
System.out.println("");
showOutputQueue(outputQueue);
}
//单向扫描算法
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void OneWayScanArithmetic(LinkedList<Integer> queue1,LinkedList<Integer> outputQueueL,LinkedList<Integer> outputQueueR){
int[] array = changeQueueToOrderIntArray(queue1);
boolean firstAdd = true;
int firstAddr = 0;
for(int i = 0;i < array.length;i++) {
if(array[i] >= arm) {
if(firstAdd == true) {
firstAdd = false;
firstAddr = i;
}
outputQueueL.offer(array[i]);
move(array[i]);
}
}
move(surfaceNum);
move(0);
for(int i = 0;i < firstAddr;i++) {
outputQueueL.offer(array[i]);
move(array[i]);
}
System.out.println();
showOutputQueue(outputQueueL);
}
//把输入队列转换成有序数组
int[] changeQueueToOrderIntArray(LinkedList<Integer> InputQueue) {
int[] array = new int[InputQueue.size()];
Iterator<Integer> it = InputQueue.iterator();
for(int i = 0;i < array.length;i++) {
array[i] = it.next();
}
return insertArithmetic(array);
}
//插入排序算法
int[] insertArithmetic(int[] array) {
int len = array.length;
int temp;
for(int i = 1;i < len;i++) {
temp = array[i];
int j;
for(j = i - 1;j >= 0;j--) {
if(temp < array[j]) {
array[j+1] = array[j];
}
else
break;
}
array[j + 1] = temp;
}
return array;
}
void execOneWayScanArithmetic(LinkedList<Integer> InputQueue) {
distance = 0;
LinkedList<Integer> OutputLeftQueue = new LinkedList<Integer>();
LinkedList<Integer> OutputRightQueue = new LinkedList<Integer>();
OneWayScanArithmetic(InputQueue,OutputLeftQueue,OutputRightQueue);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//双向扫描算法
void DoubleWayScanArithmetic(LinkedList<Integer> queue1,LinkedList<Integer> outputQueueL,LinkedList<Integer> outputQueueR){
int[] array = changeQueueToOrderIntArray(queue1);
boolean firstAdd = true;
int firstAddr = 0;
for(int i = 0;i < array.length;i++) {
if(array[i] >= arm) {
if(firstAdd == true) {
firstAdd = false;
firstAddr = i;
}
outputQueueL.offer(array[i]);
move(array[i]);
}
}
move(surfaceNum);
for(int i = firstAddr - 1;i >= 0;i--) {
outputQueueL.offer(array[i]);
move(array[i]);
}
System.out.println();
showOutputQueue(outputQueueL);
}
void execDoubleWayScanArithmetic(LinkedList<Integer> InputQueue) {
distance = 0;
LinkedList<Integer> OutputLeftQueue = new LinkedList<Integer>();
LinkedList<Integer> OutputRightQueue = new LinkedList<Integer>();
DoubleWayScanArithmetic(InputQueue,OutputLeftQueue,OutputRightQueue);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//电梯调度算法
void ElevatorSchedulingArithmetic(LinkedList<Integer> queue1,LinkedList<Integer> outputQueueL,LinkedList<Integer> outputQueueR){
int tempArm1 = arm;
//从右向左扫描
System.out.println("移动臂由外向里移动:");
int[] array = changeQueueToOrderIntArray(queue1);
boolean firstAdd = true;
int firstAddr = 0;
for(int i = 0;i < array.length;i++) {
if(array[i] >= arm) {
if(firstAdd == true) {
firstAdd = false;
firstAddr = i;
}
outputQueueL.offer(array[i]);
move(array[i]);
}
}
for(int i = firstAddr - 1;i >= 0;i--) {
outputQueueL.offer(array[i]);
move(array[i]);
}
System.out.println("");
showOutputQueue(outputQueueL);
System.out.println("");
//从左向右扫描
System.out.println("移动臂由里向外移动:");
distance = 0;
arm = tempArm1;
firstAdd = true;
firstAddr = 0;
for(int i = array.length - 1;i >= 0;i--) {
if(array[i] <= arm) {
if(firstAdd == true) {
firstAdd = false;
firstAddr = i;
}
outputQueueR.offer(array[i]);
move(array[i]);
}
}
for(int i = firstAddr + 1;i < array.length;i++) {
outputQueueR.offer(array[i]);
move(array[i]);
}
System.out.println("");
showOutputQueue(outputQueueR);
}
void execElevatorSchedulingArithmetic(LinkedList<Integer> InputQueue) {
distance = 0;
LinkedList<Integer> OutputLeftQueue = new LinkedList<Integer>();
LinkedList<Integer> OutputRightQueue = new LinkedList<Integer>();
ElevatorSchedulingArithmetic(InputQueue,OutputLeftQueue,OutputRightQueue);
}
}
测试类Java文件:
public class runTest {
public static void main(String args[]) {
(new ArmShiftSchedulingAlgorithm()).menu();
}
}
运行结果如下:
版权声明:本文为weixin_52047251原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。