cout << "\n"; cout << "磁道号\t移动距离" << "\n"; for(int i = 0; i < v.size(); i ++) { cout << v[i].first << "\t" << v[i].second << "\n"; } cout << "平均寻道长度:" << "\t"; printf("%.1lf", avgfind / n); return 0;
时间: 2023-08-26 22:06:15 浏览: 71
这段代码的功能是输出计算结果。首先输出一个空行,然后输出一个表头:“磁道号 移动距离”,接着用 for 循环遍历 vector v,输出每个元素的第一个和第二个值,即磁道号和该磁道号与 start 的距离。循环结束后,输出平均寻道长度,即 avgfind 除以元素个数 n 的值,保留一位小数。最后,返回值 0 表示程序正常结束。
相关问题
帮我将下面代码转为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;}
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);
}
}
void FCFS() { cout << endl; cout << "\t\t\t\t\tFCFS" << endl; initial(); //按照输入顺序依次访问磁道 moveDistance[0] = abs(trackOrder[0] - startTrack); Sum_Distance = moveDistance[0]; visitOrder[0] = trackOrder[0]; for (int i = 1; i < trackNum; i++) { moveDistance[i] = abs(trackOrder[i] - trackOrder[i - 1]); Sum_Distance += moveDistance[i]; visitOrder[i] = trackOrder[i]; } averageDistance = Sum_Distance * 1.0 / trackNum; display(); }这段代码的数据结构是什么
这段代码的数据结构并不明确,但是可以看出使用了一些数组来存储磁道访问顺序、移动距离等信息。其中,trackOrder数组存储了按照输入顺序依次访问的磁道号;moveDistance数组存储了相邻磁道之间的移动距离;visitOrder数组存储了按照输入顺序依次访问的磁道号,即最终的磁道访问顺序。其他变量如startTrack表示起始磁道号,trackNum表示磁道数目,Sum_Distance表示总移动距离,averageDistance表示平均移动距离等,也都是基本数据类型。
阅读全文