从100号磁道开始 | |
被访问的下一个磁道 | 移动距离(磁道数) |
90 | 10 |
58 | 32 |
55 | 3 |
39 | 16 |
38 | 1 |
18 | 20 |
150 | 132 |
160 | 10 |
184 | 24 |
平均寻道长度:27.5 |
#include<iostream>
#include<algorithm>
#include<list>
#include <fstream>
#include<iterator>
#include<vector>
#include <functional>
#include<cmath>
using namespace std;
class SSTF
{
public:
int id;//编号
int dis;//距离
SSTF()
{
dis = 0;
}
friend ostream & operator <<(ostream &o, const SSTF &s);
friend istream & operator >>(istream &i ,SSTF &s);
bool operator ==(const SSTF &s)
{
if (s.id==this->id)
{
return true;
}
return false;
}
bool operator>(const SSTF &s)
{
if (this->id>s.id )
{
return true;
}
return false;
}
bool operator<(const SSTF &s)
{
if (this->id < s.id)
{
return true;
}
return false;
}
SSTF & operator=(const SSTF &s)
{
this->id = s.id;
this->dis = s.dis;
return *this;
}
};
ostream & operator <<(ostream &o, const SSTF &s)
{
o << s.id << "\t" << s.dis;
return o;
}
istream & operator >>(istream &i, SSTF &s)
{
i >> s.id ;
return i;
}
void main()
{
fstream out("data.txt");
list<SSTF>v;
copy(istream_iterator<SSTF>(out),istream_iterator<SSTF>(),back_inserter(v));
copy(v.begin(), v.end(), ostream_iterator<SSTF>(cout));
float num=0.0;
int begin = 100;
v.sort();
cout << "\n结果是:" << endl;
cout << "依次访问顺序是:" << endl;
while (v.size()!=0)
{
transform(v.begin(), v.end(), v.begin(), [begin](SSTF &s)->SSTF {s.dis = abs(begin - s.id);return s; });
list<SSTF>::iterator min = min_element(v.begin(), v.end(), [](const SSTF &s1,const SSTF &s2)->bool {return s1.dis > s2.dis ? false : true; });
begin = min->id;
cout << *min << endl;
num += min->dis;
v.remove(*min);
}
cout <<"平均寻道时间:"<< (num / 9) << endl;
cin.get();
}
data.txt
测试结果: