帮我将下面代码转为java代码 #include "stdafx.h" #include <iostream> #include <iomanip> #include <math.h> using namespace std; const int MaxNumber=100; int TrackOrder[MaxNumber]; int MoveDistance[MaxNumber]; int FindOrder[MaxNumber]; double AverageDistance; bool direction; int BeginNum; int M=500; int N; int SortOrder[MaxNumber]; bool Finished[MaxNumber]; void Inith() { cout<<"请输入提出磁盘I/O申请数:"; cin>>N; cout<<"请依次输入要访问的磁道号"; for(int i=0;i<N;i++) cin>>TrackOrder[i]; for(int j=0;j<N;j++) MoveDistance[j]=0; cout<<"请输入开始磁道号:"; cin>>BeginNum; for(int k=0;k<N;k++) Finished[k]=false; for(int l=0;l<N;l++) SortOrder[l]=TrackOrder[l]; } void Sort() { int temp; for(int i=N-1;i>=0;i--) for(int j=0;j<i;j++) { if(SortOrder[j]>SortOrder[j+1]) { temp=SortOrder[j]; SortOrder[j]=SortOrder[j+1]; SortOrder[j+1]=temp; } }} void FCFS() { int temp; temp=BeginNum; for(int i=0;i<N;i++) { MoveDistance[i]=abs(TrackOrder[i]-temp); temp=TrackOrder[i]; FindOrder[i]=TrackOrder[i]; }} void SSTF() { int temp,n; int A=M; temp=BeginNum; for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { if(abs(TrackOrder[j]-temp)<A&&Finished[j]==false) { A=abs(TrackOrder[j]-temp); n=j; } else continue; } Finished[n]=true; MoveDistance[i]=A; temp=TrackOrder[n]; A=M; FindOrder[i]=TrackOrder[n]; }} void Count() { int Total=0; for(int i=0;i<N;i++) { Total+=MoveDistance[i]; } AverageDistance=((double)Total)/((double)N);} void Show() { cout<<setw(20)<<"被访问的下一个磁道号"<<setw(20)<<"移动距离(磁道数)"<<endl; for(int i=0;i<N;i++) { cout<<setw(15)<<FindOrder[i]<<setw(15)<<MoveDistance[i]<<endl; } cout<<setw(20)<<"平均寻道长度:"<<AverageDistance<<endl; cout<<endl;} int main() { int y=1; int s; Inith(); while(y) { cout<<"请选择寻道方式:--FCFS;2--SSTF:"; cin>>s; switch(s) { case 1:FCFS();Count();Show();break; case 2:SSTF();Count();Show();break; } cout<<"是否继续选择寻道算法?1--是;--否;"; int p; cin>>p; y=p; } exit; return 0;}
时间: 2024-04-22 08:28:46 浏览: 149
基于VC中使用ForceInclude来强制包含stdafx.h的解决方法
import java.util.Scanner;
public class DiskScheduling {
public static final int MaxNumber = 100;
public static int[] TrackOrder = new int[MaxNumber];
public static int[] MoveDistance = new int[MaxNumber];
public static int[] FindOrder = new int[MaxNumber];
public static double AverageDistance;
public static boolean direction;
public static int BeginNum;
public static int M = 500;
public static int N;
public static int[] SortOrder = new int[MaxNumber];
public static boolean[] Finished = new boolean[MaxNumber];
public static void Inith() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入提出磁盘I/O申请数:");
N = sc.nextInt();
System.out.print("请依次输入要访问的磁道号:");
for(int i=0;i<N;i++) {
TrackOrder[i] = sc.nextInt();
}
for(int j=0;j<N;j++) {
MoveDistance[j]=0;
}
System.out.print("请输入开始磁道号:");
BeginNum = sc.nextInt();
for(int k=0;k<N;k++) {
Finished[k]=false;
}
for(int l=0;l<N;l++) {
SortOrder[l]=TrackOrder[l];
}
}
public static void Sort() {
int temp;
for(int i=N-1;i>=0;i--) {
for(int j=0;j<i;j++) {
if(SortOrder[j]>SortOrder[j+1]) {
temp=SortOrder[j];
SortOrder[j]=SortOrder[j+1];
SortOrder[j+1]=temp;
}
}
}
}
public static void FCFS() {
int temp;
temp=BeginNum;
for(int i=0;i<N;i++) {
MoveDistance[i]=Math.abs(TrackOrder[i]-temp);
temp=TrackOrder[i];
FindOrder[i]=TrackOrder[i];
}
}
public static void SSTF() {
int temp,n;
int A=M;
temp=BeginNum;
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
if(Math.abs(TrackOrder[j]-temp)<A&&Finished[j]==false) {
A=Math.abs(TrackOrder[j]-temp);
n=j;
}
else continue;
}
Finished[n]=true;
MoveDistance[i]=A;
temp=TrackOrder[n];
A=M;
FindOrder[i]=TrackOrder[n];
}
}
public static void Count() {
int Total=0;
for(int i=0;i<N;i++) {
Total+=MoveDistance[i];
}
AverageDistance=((double)Total)/((double)N);
}
public static void Show() {
System.out.printf("%20s%20s\n","被访问的下一个磁道号","移动距离(磁道数)");
for(int i=0;i<N;i++) {
System.out.printf("%15d%15d\n",FindOrder[i],MoveDistance[i]);
}
System.out.printf("%20s%.2f\n","平均寻道长度:",AverageDistance);
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int y=1;
int s;
Inith();
while(y==1) {
System.out.print("请选择寻道方式:1--FCFS;2--SSTF:");
s = sc.nextInt();
switch(s) {
case 1:
FCFS();
Count();
Show();
break;
case 2:
SSTF();
Count();
Show();
break;
default:
System.out.println("输入有误!");
break;
}
System.out.print("是否继续选择寻道算法?1--是;2--否:");
int p = sc.nextInt();
y=p;
}
sc.close();
System.exit(0);
}
}
阅读全文