#include<iostream>
#include<iomanip>
using namespace std;
#define MaxNumber 100
int TrackOrder[MaxNumber];
int ServeOrder[MaxNumber];
int Order[MaxNumber];
int MoveDistance[MaxNumber];
int Start;
int OrderNum;
int tempNum;
double AverageDistance;
bool direction;
void GetDirection();
void FCFS();
void SSTF();
void SCAN();
void CSCAN();
void Input();
void Output();
int main()
{
int choice;
cout<<"------------------------------ 磁道调度算法 -----------------------------"<<endl;
Input();
while(true)
{
cout<<"请输入选择:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"--------------------- 1-FCFS算法 ---------------------"<<endl;
FCFS();break;
case 2:
cout<<"--------------------- 2-SSTF算法 ---------------------"<<endl;
SSTF();break;
case 3:
cout<<"--------------------- 3-SCAN算法 ---------------------"<<endl;
SCAN();break;
case 4:
cout<<"--------------------- 4-循环SCAN算法 ---------------------"<<endl;
CSCAN();break;
case 5:
return 0;
case 0:
cout<<"--------------------- 0-重新输入模拟信息 ---------------------"<<endl;
Input();
break;
default :
cout<<"输入错误!"<<endl;
}
if(choice==0)
{
break;
}
else
{
Output();
}
}
return 0;
}
void Input()
{
int temp;
cout<<"请输入磁盘请求总个数:";
cin>>OrderNum;
cout<<"请输入磁盘请求序列:";
for(int i=0;i<OrderNum;i++)
{
cin>>TrackOrder[i];
ServeOrder[i]=TrackOrder[i];
Order[i]=ServeOrder[i];
}
cout<<"请输入开始磁道号:";
cin>>Start;
//排序
for(i=0;i<OrderNum;i++)
for(int j=i;j<OrderNum;j++)
{
if(Order[i]>Order[j])
{
temp=Order[i];
Order[i]=Order[j];
Order[j]=temp;
}
}
cout<<"-----------------------------------------"<<endl;
cout<<"选择磁盘扫描算法:"<<endl;
cout<<"1-FCFS"<<endl;
cout<<"2-SSTF"<<endl;
cout<<"3-SCAN"<<endl;
cout<<"4-循环SCAN"<<endl;
cout<<"5-退出"<<endl;
cout<<"0-重新输入磁盘扫描信息"<<endl;
cout<<endl;
}
void Output()
{
//计算距离
MoveDistance[0]=Start-ServeOrder[0];
if(MoveDistance[0]<0)
MoveDistance[0] = -MoveDistance[0];
for(int i=1;i<OrderNum;i++)
{
MoveDistance[i]=ServeOrder[i]-ServeOrder[i-1];
if(MoveDistance[i]<0)
MoveDistance[i] = -MoveDistance[i];
}
//计算平局输出
AverageDistance=0;
cout<<setw(6)<<" "<<"|"<<setw(6)<<" "<<endl;
cout<<setw(6)<<"磁道号"<<"|"<<setw(6)<<"寻道长度"<<endl;
cout<<"------|------"<<endl;
for(i=0;i<OrderNum;i++)
{
cout<<setw(4)<<ServeOrder[i]<<" |"<<setw(6)<<MoveDistance[i]<<endl;
AverageDistance=AverageDistance+MoveDistance[i];
}
AverageDistance=(double)AverageDistance/(double)OrderNum;
cout<<"平均寻道长度为:"<<setprecision(1)<<setiosflags(ios::fixed)<<AverageDistance<<endl<<endl;
}
void GetDirection()
{
int D;
while(true)
{
//cout<<"输入磁道的方向(1-增加、0-减少):";
//cin>>D;
D=1;//上诉两个句子改为本句
if(D==0)
{
direction=false;
break;
}
else if(D==1)
{
direction=true;
break;
}
else
{
cout<<"输入错误!"<<endl;
}
}
//找到第一个位置
for(int i=0;i<OrderNum;i++)
{
if(Order[i]<=Start && Order[i+1]>=Start)
{
tempNum=i;
break;
}
}
}
void FCFS()
{
for(int i=0;i<OrderNum;i++)
{
ServeOrder[i]=TrackOrder[i];
}
}
void SSTF()
{
int OldOrder[MaxNumber];
int temp=Start;
int LeftNum=OrderNum;
int LDistance,rDistance;
//找到第一个位置
for(int i=0;i<OrderNum;i++)
{
if(Order[i]<=Start && Order[i+1]>=Start)
{
tempNum=i;
break;
}
}
for(i=0;i<OrderNum;i++)
OldOrder[i]=Order[i];
int serve=0;
while(LeftNum>0)
{
//位置在0
if(tempNum==0)
{
for(int j=0;j<LeftNum;j++)
{
ServeOrder[serve]=OldOrder[j];
serve++;
}
break;
}
else if(tempNum==LeftNum)//位置在末尾
{
for(int j=LeftNum;j>=0;j--)
{
ServeOrder[serve]=OldOrder[j];
serve++;
}
break;
}
else//位置在中间
{
LDistance=temp-OldOrder[tempNum];
rDistance=OldOrder[tempNum+1]-temp;
if(LDistance<rDistance)
{
ServeOrder[serve]=OldOrder[tempNum];
temp=OldOrder[tempNum];
tempNum--;
}
else
{
ServeOrder[serve]=OldOrder[tempNum+1];
temp=OldOrder[tempNum+1];
}
for(int j=tempNum+1;j<LeftNum;j++)
{
OldOrder[j]=OldOrder[j+1];
}
LeftNum--;
serve++;
}
}
}
void SCAN()
{
int serve=0;
GetDirection();
if(direction)
{
for(int i=tempNum+1;i<OrderNum;i++)
{
ServeOrder[serve]=Order[i];
serve++;
}
for(i=tempNum;i>=0;i--)
{
ServeOrder[serve]=Order[i];
serve++;
}
}
else
{
for(int i=tempNum;i>=0;i--)
{
ServeOrder[serve]=Order[i];
serve++;
}
for(i=tempNum+1;i<OrderNum;i++)
{
ServeOrder[serve]=Order[i];
serve++;
}
}
}
void CSCAN()
{
int serve=0;
GetDirection();
if(direction)
{
for(int i=tempNum+1;i<OrderNum;i++)
{
ServeOrder[serve]=Order[i];
serve++;
}
for(i=0;i<=tempNum;i++)
{
ServeOrder[serve]=Order[i];
serve++;
}
}
else
{
for(int i=tempNum;i>=0;i--)
{
ServeOrder[serve]=Order[i];
serve++;
}
for(i=OrderNum;i>tempNum;i--)
{
ServeOrder[serve]=Order[i];
serve++;
}
}
}