#include<iostream>
#include <cmath>
using namespace std;
/*先来先服务调度算法*/
void FIFO(int b[],int n,int num)//b[]磁道请求序列,num磁头当前所在磁道号
{
int i=0,sum=0,x;
x=abs(b[i]-num);
cout<<"==================================================="<<endl;
cout<<"访问序号\t所访磁道\t移动距离"<<endl;
cout<<i+1<<" "<<b[i]<<" "<<x<<endl;
for(i=1;i<n;i++)
{
cout<<i+1<<" "<<b[i]<<" "<<abs(b[i]-b[i-1])<<endl;
sum+=abs(b[i]-b[i-1]);
}
cout<<"移动总距离\t"<<sum+x<<" "<<"平均距离\t"<<(sum+x)*1.00/n<<endl;
cout<<"==================================================="<<endl;
}
/*电梯调度算法*/
void scan(int b[],int n,int num) //n是磁道请求序列的个数(数组元素个数),num当前磁头所在磁头号,
{
int i,j,temp,s1=1,s2=1,k=1,sum1=0,sum2=0,a[20],c[20],option;
a[0]=num; c[0]=num;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(b[i]>b[j]) //将磁道请求序列从小到大排序
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
for(i=0;i<n;i++)
{
if(b[i]<num)
{
a[s1]=b[i]; s1++; //将磁头左边(小)的请求序列入栈
}
else
{
c[s2]=b[i]; s2++; //将磁头右边(大)的请求序列入栈
}
}
cout<<"请输入磁头移动方向:"<<endl;
cout<<"1:由外向内:"<<" "<<"2:由内向外"<<endl;
cin>>option; //option电梯调度方向选择
if(option==2) /*选择移动臂方向向外,则先向外扫描*/
{
cout<<"==================================================="<<endl;
cout<<"访问序号\t所访磁道\t移动距离"<<endl;
for(i=1;i<s2;i++)
{
cout<<k<<" "<<c[i]<<" "<<abs(c[i]-c[i-1])<<endl;
sum1+=abs(c[i]-c[i-1]);
k++;
}
cout<<k<<" "<<a[1]<<" "<<abs(c[s2-1]-a[1])<<endl; //输出磁头左边部分
k++;
for(j=2;j<s1;j++)
{
cout<<k<<" "<<a[j]<<" "<<abs(a[j]-a[j-1])<<endl;
sum2+=abs(a[j]-a[j-1]);
k++;
}
cout<<"移动总距离\t"<<sum1+sum2<<" "<<"平均距离\t"<<(sum1+sum2+abs(a[1]-c[s2-1]))*1.00/n<<endl;
cout<<"==================================================="<<endl;
}
if(option==1) /*选择移动臂方向向内,则先向内扫描*/
{
cout<<"==================================================="<<endl;
cout<<"访问序号\t所访磁道\t移动距离"<<endl;
for(j=1;j<s1;j++)
{
cout<<k<<" "<<a[j]<<" "<<abs(a[j]-a[j-1])<<endl;
sum2+=abs(a[j]-a[j-1]);
k++;
}
cout<<k<<" "<<c[1]<<" "<<abs(a[s1-1]-a[1])<<endl; //输出磁头左边部分
k++;
for(i=2;i<s2;i++)
{
cout<<k<<" "<<c[i]<<" "<<abs(c[i]-c[i-1])<<endl;
sum1+=abs(c[i]-c[i-1]);
k++;
}
cout<<"移动总距离\t"<<sum1+sum2<<" "<<"平均距离\t"<<(sum1+sum2+abs(a[1]-c[s2-1]))*1.00/n<<endl;
cout<<"==================================================="<<endl;
}
}
void main()
{
int c[25]; int i,n,nu,num,x; //nu磁道请求序列的个数,应小于25
cout<<"==================================================="<<endl;
cout<<"=============欢迎进入磁盘调度模拟系统=============="<<endl;
cout<<"==================================================="<<endl;
cout<<"===========请您输入磁道请求序列的个数=============="<<endl;
cin>>n;
cout<<"=====请您输入"<<n<<"个整数<磁道请求序列>,并以空格隔开===="<<endl;
for(i=0;i<n;i++)
cin>>c[i];
cout<<"==============请输入当前磁头所在磁头号============="<<endl;
cin>>nu;
num=nu;
cout<<"==================================================="<<endl;
cout<<"1:先进先出FIFO算法 2:电梯调度Sanf算法 0:退出"<<endl;
cout<<"===================请输入您的选择=================="<<endl;
cin>>x;
while(x!=0)
{
switch(x)
{
case 1:FIFO(c,n,num);break;
case 2 :scan(c,n,num);break;
default :cout<<"=============只能在1,0或者2中进行选择============="<<endl;
}
cout<<"==若继续测试,请再次输入您的选择,否则按0回车退出=========="<<endl;
cin>>x;
}
cout<<"======================成功退出======================"<<endl;
}