#include<iostream>
#include<cmath>
#include<stdio.h>
#include<stdlib.h>
#define size 9
#define N 3
using namespace std;
//排序函数
void sort(int input[],int num)
{
int t;
for(int j=0;j<num-1;j++)
for(int i=0;i<num-1-j;i++)
if(input[i]>input[i+1]){
t=input[i];
input[i]=input[i+1];
input[i+1]=t;
}
}
void FCFS(int input[],int num)
{
cout<<"输入当前所在磁道号:";
int dangqian;
float sum=0; //记录总访问磁道数
float ave; //平均访问磁道数
cin>>dangqian;
cout<<"访问磁道顺序为: ";
for(int i=0;i<num;i++)
{
cout<<input[i]<<" ";
sum=sum+abs(input[i]-dangqian);
dangqian=input[i];
}
ave=sum/num;
cout<<"\n移动的磁道总数:"<<sum;
cout<<"\n平均访问磁道数为: "<<ave<<endl;
cout<<"\n";
}
void SSTF(int input[],int num)
{
sort(input,size); //对输入的磁道号排序
cout<<"输入当前所在磁道号:";
int dangqian;
float sum=0; //记录总访问磁道数
float ave; //平均访问磁道数
cin>>dangqian;
cout<<"访问磁道顺序为: ";
//如果当前大于最大的磁道号
if(dangqian>=input[num-1])
{
for(int i=num-1;i>=0;i--)
{
cout<<input[i]<<" ";
sum=sum+abs(input[i]-dangqian);
dangqian=input[i];
}
}
//如果当前小于最大的磁道号
else if(dangqian<=input[0])
{
for(int i=0;i<num;i++)
{
cout<<input[i]<<" ";
sum=sum+abs(input[i]-dangqian);
dangqian=input[i];
}
}
//如果在最大和最小之间
else
{
int k=0;
int low,high;
while(dangqian>input[k])
{
k++;
}
low=k-1;
high=k;
while(low>=0 || high <num)
{
if((abs(dangqian-input[low])<=abs(input[high]-dangqian)) && low>=0) //如果前一个较近取前一个
{
cout<<input[low]<<" ";
sum=sum+abs(input[low]-dangqian);
dangqian=input[low];
low--;
}
else
{
cout<<input[high]<<" ";
sum=sum+abs(input[high]-dangqian);
dangqian=input[high];
high++;
}
}
}
ave=sum/num;
cout<<"\n移动的磁道总数:"<<sum;
cout<<"\n平均访问磁道数为: "<<ave<<endl;
cout<<"\n";
}
void SCAN(int input[],int num)
{
sort(input,size);
cout<<"输入当前所在磁道号:";
int dangqian;
float sum=0; //记录总访问磁道数
float ave; //平均访问磁道数
cin>>dangqian;
cout<<"选择扫描方向: \n";
cout<<"0、 磁道号增加\n";
cout<<"1、 磁道号减小\n";
cout<<"请输入:";
int k;
cin>>k;
int x=0;
while(dangqian>input[x])
x++;
int temp=x;
cout<<"访问磁道顺序为: ";
if(k==0)//磁道号增大方向
{
while(x<size)
{
cout<<input[x]<<" ";
sum=sum+abs(input[x]-dangqian);
dangqian=input[x];
x++;
}
int wo=temp;
while(wo>0)
{
cout<<input[wo-1]<<" ";
sum=sum+abs(input[wo-1]-dangqian);
dangqian=input[wo-1];
wo--;
}
}
else if(k==1)//磁道号减小方向
{
while(x>0)
{
cout<<input[x-1]<<" ";
sum=sum+abs(input[x-1]-dangqian);
dangqian=input[x-1];
x--;
}
int wow=temp;
while(wow<size)
{
cout<<input[wow]<<" ";
sum=sum+abs(input[wow]-dangqian);
dangqian=input[wow];
wow++;
}
}
else
{
cout<<"输入错误! \n";
exit(0);
}
ave=sum/num;
cout<<"\n移动的磁道总数:"<<sum;
cout<<"\n平均访问磁道数为: "<<ave<<endl;
cout<<"\n";
}
void CSCAN(int input[],int num)
{
sort(input,size);
cout<<"输入当前所在磁道号:";
int dangqian;
float sum=0; //记录总访问磁道数
float ave; //平均访问磁道数
cin>>dangqian;
cout<<"选择扫描方向: \n";
cout<<"0、 磁道号增加\n";
cout<<"1、 磁道号减小\n";
cout<<"请输入:";
int k;
cin>>k;
int x=0;
while(dangqian>input[x])
x++;
int temp=x;
cout<<"访问磁道顺序为: ";
if(k==0)//磁道号增大方向
{
while(x<size)
{
cout<<input[x]<<" ";
sum=sum+abs(input[x]-dangqian);
dangqian=input[x];
x++;
}
int wo=0;
while(wo<temp)
{
cout<<input[wo]<<" ";
sum=sum+abs(input[wo]-dangqian);
dangqian=input[wo];
wo++;
}
}
else if(k==1)//磁道号减小方向
{
while(x>0)
{
cout<<input[x-1]<<" ";
sum=sum+abs(input[x-1]-dangqian);
dangqian=input[x-1];
x--;
}
int wow=size-1;
while(wow>=temp)
{
cout<<input[wow]<<" ";
sum=sum+abs(input[wow]-dangqian);
dangqian=input[wow];
wow--;
}
}
else
{
cout<<"输入错误! \n";
exit(0);
}
ave=sum/num;
cout<<"\n移动的磁道总数:"<<sum;
cout<<"\n平均访问磁道数为: "<<ave<<endl;
cout<<"\n";
}
void NStepSCAN(int input[],int num)
{
//赋值
int n=num/N;
int a[n],i,j,k=0,m=0;
for(i=0;i<N;i++)
{
for(j=0;j<n;j++)
{
a[j]=input[m];
m++;
}
sort(a,n);
for(j=0;j<n;j++)
{
input[k]=a[j];
k++;
}
}
//调度算法
cout<<"输入当前所在磁道号:";
int dangqian,temp,x;
float sum=0; //记录总访问磁道数
float ave; //平均访问磁道数
cin>>dangqian;
cout<<"选择扫描方向: \n";
cout<<"0、 磁道号增加\n";
cout<<"1、 磁道号减小\n";
cout<<"请输入:";
cin>>k;
for(i=0;i<N;i++)
{
x=0+i*n-1;
for(j=0+i*n,m=0;m<n;j++,m++)
{
if(dangqian>input[j])
{
x=j;
}
}
x++;
temp=x;
cout<<"访问磁道顺序为: ";
if(k==0)//磁道号增加方向
{
while(x<n*(i+1))
{
cout<<input[x]<<" ";
sum=sum+abs(input[x]-dangqian);
dangqian=input[x];
x++;
}
int wo=temp;
while(wo>0+n*i)
{
cout<<input[wo-1]<<" ";
sum=sum+abs(input[wo-1]-dangqian);
dangqian=input[wo-1];
wo--;
}
}
else if(k==1)//磁道号减小方向
{
x--;
while(x>=0+n*i)
{
cout<<input[x]<<" ";
sum=sum+abs(input[x]-dangqian);
dangqian=input[x];
x--;
}
int wo=temp;
while(wo<n*(i+1))
{
cout<<input[wo]<<" ";
sum=sum+abs(input[wo]-dangqian);
dangqian=input[wo];
wo++;
}
}
else
{
cout<<"输入错误! \n";
exit(0);
}
cout<<endl;
}
ave=sum/num;
cout<<"\n移动的磁道总数:"<<sum;
cout<<"\n平均访问磁道数为: "<<ave<<endl;
cout<<"\n";
}
int main()
{
int track[200]; //磁道的范围为 0~199
int C[size]={55,58,39,18,90,160,150,38,184}; //输入的磁道号
int i=0;
//磁道初始化
while(i<200)
{
track[i]=i;
i++;
}
int suanfa; //算法选择
while(true){
cout<<"选择算法: "<<endl;
cout<<"1、 FCFS 算法\n";
cout<<"2、 SSTF 算法\n";
cout<<"3、 SCAN 算法\n";
cout<<"4、 CSCAN 算法\n";
cout<<"5、 NStepSCAN 算法\n";
cout<<"6、 退出!\n";
cout<<"请输入:";
scanf("%d",&suanfa);
if(suanfa==1)
FCFS(C,size);
else if(suanfa==2)
SSTF(C,size);
else if(suanfa==3)
SCAN(C,size);
else if(suanfa==4)
CSCAN(C,size);
else if(suanfa==5)
NStepSCAN(C,size);
else
break;
}
}
- 1
- 2
前往页