package edu.swj;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Algorithm extends JPanel {
LogPane log;
public Algorithm() {
log = LogPane.getLog();
}
/* 冒泡排序算法 */
int[] bubble(int cidao[], int m) {
int i, j;
int temp;
for (i = 0; i < m; i++)// 使用冒泡法从小到大顺序排列
for (j = i + 1; j < m; j++) {
if (cidao[i] > cidao[j]) {
temp = cidao[i];
cidao[i] = cidao[j];
cidao[j] = temp;
}
}
return cidao;
}
/* 先来先服务算法 */
//顺序访问
public void FCFS(int cidao[], int now) throws InterruptedException { // 磁道号数组,个数为M
int sum = 0;// 总寻道长度
int i, j;
int count = 0;
int len = 0;
float ave;// 平均寻道长度
sum += Math.abs(cidao[0] - now); //计算长度
count = count + 1;
String buffer = "";
for (i = 0; i < cidao.length; i++) {// 输出磁盘扫描序列
if (cidao[i] > 0) {
len++;
buffer += cidao[i] + " ";
System.out.println("当前磁道:" + cidao[i]);
Thread.sleep(500);
}
}
log.addLog("磁盘扫描序列为: " + buffer.toString());
for (i = 0, j = 1; j < len; i++, j++) {
sum += Math.abs(cidao[j] - cidao[i]);
count++;
}
ave = sum / len;
log.addLog("总道数:" + sum);
log.addLog("平均寻道长度:" + ave);
}
/* 最短寻道时间优先调度算法 */
//优先访问距当前磁头最近的磁道
public void SSTF(int cidao[], int now) throws InterruptedException {
int len = 0;
int i, sum = 0;
int a, count = 0;
float ave;
//获取磁道个数
for (i = 0; i < cidao.length; i++) {
if (cidao[i] > 0) {
len++;
}
}
cidao = bubble(cidao, len); // 调用冒泡排序算法排序
String s = "";
for (int z = 0; z < len; z++) {
s += cidao[z] + " ";
}
log.addLog("磁道序列从小到大排序为:" + s);
if (cidao[len - 1] <= now) // 若当前磁道号大于请求序列中最大者,则直接由外向内依次给予各请求服务
{
String buffer = "";
for (i = len - 1; i >= 0; i--) {
buffer += cidao[i] + " ";
System.out.println("当前磁道:" + cidao[i]);
Thread.sleep(500);
}
log.addLog("磁盘扫描序列为: " + buffer.toString());
sum = now - cidao[0];
count = len;
}
if (cidao[0] >= now) { // 若当前磁道号小于请求序列中最小者,则直接由内向外依次给予各请求服务;
String buffer = "";
for (i = 0; i < len; i++) {
buffer += cidao[i] + " ";
System.out.println("当前磁道:" + cidao[i]);
Thread.sleep(500);
}
log.addLog("磁盘扫描序列为: " + buffer.toString());
sum = cidao[len - 1] - now;
count = len;
}
//若当前磁道号大于当前请求序列中最小者并且小于最大者
if (now > cidao[0] && now < cidao[len - 1]) {
StringBuffer buffer = new StringBuffer("");
int k = 1;
//确定当前磁道在已排的序列中的位置
while (cidao[k] < now) {
k++;
}
int l,r;
l = k - 1;
r = k;
//当前磁道在请求序列范围内
while ((l >= 0) && (r < len)) {
if (now - cidao[l] <= (cidao[r] - now)) {// 选择与当前磁道最近的请求给予服务
buffer.append(cidao[l] + " ");
System.out.println("当前磁道:" + cidao[l]);
Thread.sleep(500);
sum += now - cidao[l];
now = cidao[l];
l = l - 1;
} else {
buffer.append(cidao[r] + " ");
System.out.println("当前磁道:" + cidao[r]);
Thread.sleep(500);
sum += cidao[r] - now;
now = cidao[r];
r = r + 1;
}
}
//磁头移动到序列的最小号,返回外侧扫描仍未扫描的磁道
int j;
if (l == -1) {
for (j = r; j < len; j++) {
buffer.append(cidao[j] + " ");
System.out.println("当前磁道:" + cidao[j]);
Thread.sleep(500);
}
sum += cidao[len - 1] - cidao[0];
} else { //磁头移动到序列的最大号,返回内侧扫描仍未扫描的磁道
for (j = l; j >= 0; j--) {
buffer.append(cidao[j] + " ");
System.out.println("当前磁道:" + cidao[j]);
Thread.sleep(500);
}
sum += cidao[len - 1] - cidao[0];
}
log.addLog("磁盘扫描序列为: " + buffer.toString());
}
ave = sum / len;
log.addLog("总道数:" + sum);
log.addLog("平均寻道长度:" + ave);
}
/* 扫描调度算法 */
// 先要给出当前磁道号和移动臂的移动方向
public void SCAN(int cidao[], int now) throws InterruptedException {
int i, j, sum = 0;
int a, len = 0;
char str[] = new char[100];
float ave;
// 获取磁道长度
for (i = 0; i < cidao.length; i++) {
if (cidao[i] > 0) {
len++;
}
}
cidao = bubble(cidao, len);// 调用冒泡排序算法排序
String s = "";
for (int z = 0; z < len; z++) {
s += cidao[z] + " ";
}
log.addLog("磁道序列从小到大排序为:" + s);
// 若当前磁道号大于请求序列中最大者,则直接由外向内, 依次给予各请求服务,此情况同最短寻道优先
if (cidao[len - 1] <= now) {
StringBuffer buffer = new StringBuffer("");
for (i = len - 1; i >= 0; i--) {
buffer.append(cidao[i] + " ");
System.out.println("当前磁道:" + cidao[i]);
Thread.sleep(500);
}
log.addLog("磁盘扫描序列为: " + buffer.toString());
sum = now - cidao[0];
}
// 若当前磁道号小于请求序列中最小者,则直接由内向外,依次给予各请求服务,此情况同最短寻道优先
if (cidao[0] >= now) {
StringBuffer buffer = new StringBuffer("");
for (i = 0; i < len; i++) {
buffer.append(cidao[i] + " ");
System.out.println("当前磁道:" + cidao[i]);
Thread.sleep(500);
}
log.addLog("磁盘扫描序列为: " + buffer.toString());
sum = cidao[len - 1] - now;
}
// 若当前磁道号大于请求序列中最小者且,小于最大者
if (now > cidao[0] && now < cidao[len - 1]) {
StringBuffer buffer = new StringBuffer("");
int k = 1;
while (cidao[k] < now) {
k++;
}
int l, r;
int d; //方向,默认向内
l = k - 1;
r = k;
try {
String string = JOptionPane.showInputDialog(this, "请输入当前移动臂的移动的方向(1 表示向外,0表示向内):", "提示",
JOptionPane.INFORMATION_MESSAGE);
d = Integer.parseInt(string);
//选择移动臂方向向内,则先向内扫描
if (d == 0) {
for (j = l; j >= 0; j--) {
buffer.append(cidao[j] + " ");
System.out.println("当前磁道:" + cidao[j]);
Thread.sleep(500);
}
//磁头移动到最小号,则改变方向向内扫描为扫描的磁道
for (j = r; j < len; j++) {
buffer.append(cidao[j] + " ");
System.out.println("当前磁道:" + cidao[j]);
Thread.sleep(500);
}
sum = now - 2 * cidao[0] + cidao[len - 1];
} else { //选择移动臂方向向外,则先向外扫描
for (j = r; j < len; j++) {
buffer.append(cidao[j] + " ");
System.out.println("当前磁道:" + cidao[j]);
Thread.sleep(500);
}
for (j = l; j >= 0; j--) {
buffer.append(cidao[j] + " ");
System.out.println("当前磁道:" + cidao[j]);
Thread.sleep(500);
}
sum = 2 * cidao[len - 1] - now - cidao[0];
}
log.addLog("磁盘扫描序列为: " + buffer.toString());
} catch (Exception e) {
log.addLog(e.toString());
e.printStackTrace();
}
}
ave = sum / len;
log.addLog("总道数:" + sum);
log.addLog("平均寻道长度:" + ave);
}
//循环扫描调度算法
public void CSCAN(int cidao[], int now) throws InterruptedException {
int i, j, sum = 0;
int a, len = 0;
char str[] = new char[100];
float ave;
// 获取磁道长度
for (i = 0; i <
Java版移动臂调度算法(4种)
需积分: 24 124 浏览量
2018-01-04
15:33:31
上传
评论 6
收藏 19KB ZIP 举报
彧锋
- 粉丝: 4
- 资源: 4
最新资源
- 基于C++的程序设计大赛天梯赛L2答案(天梯赛)
- 基于python实现的三次样条插值和均值插值法实现
- Python语言教程2-python批量图片大小处理-多文件夹
- Python语言教程1-python批量图片重命名,将后缀某几个不想要的字去除
- Space Combat Kit 太空战斗套件Unity游戏开发插件资源unitypackage C#
- Universal Device Preview 通用设备预览Unity游戏开发插件资源unitypackage
- Paladin Anim Set 圣骑士动画集Unity游戏动作动画插件资源unitypackage
- 计算机财务管理期末考报表部分题目及答案.doc
- 计算机软件维护论文.doc
- 计算机软件著作权授权书.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈