磁盤驅動調度

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
void display(int path[],int length)
{
	int sum=0;
	cout << "路徑爲:" << endl;
	cout << "PATH: ";
	for (int i = 0;i < length-1;i++)
	{
		sum += abs(path[i + 1] - path[i]);
		cout << path[i] << "--> ";
	}
	cout << path[length - 1] << endl;
	cout << "SumPath: " << sum;
	cout << endl;

}
void elevator(int s[],int length)
{
	int Path=0;
	int curPoint=s[0];
	int *path = new int[10];
	int *less = new int[10];
	int *more = new int[10];
	int Lcount = 0;
	int Mcount = 0;
	int Pcount = 1;
	path[0] = s[0];
	for (int i = 1;i < length;i++)
	{
		if (s[i] < curPoint)
		{
			less[Lcount] = s[i];
			Lcount++;
		}
		else 
		{
			more[Mcount] = s[i];
			Mcount++;
		}
	}
	sort(less,less+Lcount);
	sort(more,more+Mcount);
	for (int i = 0;i <Mcount;i++)
	{
		path[Pcount++] = more[i];
	}
	for (int i = Lcount-1;i >= 0;i--)
	{
		path[Pcount++] = less[i];
	}
	cout << "***********************ELEVATOR*****************************" << endl;
	display(path, Pcount);
}

void SSTF(int s[],int length)
{
	int *path = new int[10];
	int *pathFlag = new int[10];
	int curPoint = 0;
	int Pcount = 1;
	int num = 9;
	int a;
	path[0] = s[0];
	for (int i = 0;i < 10;i++)
	{
		pathFlag[i] = 0;
	}
	pathFlag[0] = 1;
	while (num--)
	{
		int min = 10000000;
       for (int i = 1;i < length;i++)
	   {
		  if ((min > abs(s[i] - s[curPoint]))&&curPoint!=i&&pathFlag[i]!=1)
		  {
			min = abs(s[i] - s[curPoint]);
			a = i;
		  }
	   }
	   curPoint = a;
	   pathFlag[curPoint] = 1;
	   path[Pcount++] = s[curPoint];

    }
	cout << "***********************SSFT*****************************" << endl;
	display(path, length);

}

int main()
{
   int S[10]= { 143,86,147,91,177,94,150,102,175,130 };
   int length = (int)(sizeof(S) / sizeof(*S));
   //cout << length;
  //elevator(S, length);
   elevator(S, length);
   cout << endl;
   SSTF(S, length);
   /*  SSTF(S,length);
   elevator(S,length);
   */
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章