磁盘寻道算法 电梯调度算法 C++实现
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
compute ( vector< int > v, int x, int d)
{ double w= 1 ; sort ( v. begin ( ) , v. end ( ) ) ; if ( d== 1 ) { cout<< endl<< " 磁头从" << x<< "磁道开始,向磁道号" << endl; cout<< " 增加方向访问" << endl; cout<< " 被访问的 移动距离 " << endl; cout<< " 下一个磁道号 (磁道数) " << endl; int i; for ( i= 0 ; i< v. size ( ) ; i++ ) { if ( x< v[ i] ) break ; } int g= i, s= 0 ; ; int m= v[ i] - x, x1= x; for ( int j= 0 ; j< v. size ( ) ; j++ ) { m= abs ( v[ i] - x1) ; cout<< " " << v[ i] << " " << m<< endl; w+ = m; x1= v[ i] ; if ( i== v. size ( ) - 1 ) { i= g; s= 1 ; } if ( s== 0 ) { i++ ; } else { i-- ; } } } else { cout<< endl<< " 磁头从" << x<< "磁道开始,向磁道号" << endl; cout<< " 减少方向访问" << endl; cout<< " 被访问的 移动距离 " << endl; cout<< " 下一个磁道号 (磁道数) " << endl; int i; for ( i= 0 ; i< v. size ( ) ; i++ ) { if ( x< v[ i+ 1 ] ) break ; } int g= i, s= 0 ; ; int m= v[ i] - x, x1= x; for ( int j= 0 ; j< v. size ( ) ; j++ ) { m= abs ( v[ i] - x1) ; cout<< " " << v[ i] << " " << m<< endl; w+ = m; x1= v[ i] ; if ( i== 0 ) { i= g; s= 1 ; } if ( s== 0 ) { i-- ; } else { i++ ; } } } cout<< " 平均寻道长度:" << w/ v. size ( ) << endl;
}
int main ( )
{ vector< int > v; int n, x, d; cout<< "请输入进程的数量:" << endl; cin>> n; cout<< "请输入各进程所在的磁道号(1-200):" << endl; for ( int i= 0 ; i< n; i++ ) { int o; cin>> o; v. push_back ( o) ; } cout<< "请输入当前磁头所在磁道(1-200):" << endl; cin>> x; cout<< "请输入当前磁头的寻道范方向:" << endl; cout<< "0-----磁道号递减方向" << endl; cout<< "1-----磁道号递增方向" << endl; cin>> d; compute ( v, x, d) ;
}