📄 diskarithmetic.cpp
字号:
/*
* 磁盘调度算法
*
*
*
* 2007.1.2
*/
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <iomanip.h>
#include <iostream.h>
#define NUM 100
/****自定义的结构体****/
typedef struct
{
int m_value;
int m_state;
}myArray;
/*
* 自定义结构体的初始化函数
*/
void Initialize(myArray c[], int &l)
{
ER1:cout<< endl << " 请输入要访问的磁道数目: ";
cin>> l;
if ((l < 1) || (l > 100))
{
cout<< endl << " 您输入的有误, 请重新输入. " << endl;
goto ER1;
}
for (int i=0; i<l; i++)
{
cout<< endl << " 请输入第 " << i+1 << " 个要访问的磁道号: ";
cin>> c[i].m_value;
c[i].m_state = 0;
}
}
/*
* Fcfs算法的创建函数
*/
void Fcfs(myArray c[], int l, float &result)
{
int begin, temp[NUM]={0};
float average=0.0;
cout<< endl << " 请输入开始时的磁道号: ";
cin>> begin;
cout<< endl;
for (int i=0; i<l; i++)
{
temp[i] = abs(c[i].m_value - begin);
begin = c[i].m_value;
cout<< " 被访问的下一个磁道号 " << setw(3) << c[i].m_value << " 移动的距离 " << temp[i] << endl;
average += temp[i];
}
average /= l;
result = average;
cout<< endl << " 平均寻道长度: " << average << endl;
}
/*
* Sstf算法用到的排序函数
*/
void Sort(myArray c[], int begin, int l, int &now)
{
int temp, k, h=0, assisant[NUM]={0};
for (int i=0; i<l; i++)
{
if (c[i].m_state == 0)
{
assisant[h] = abs(c[i].m_value - begin);
h++;
}
}
for (i=0; i<h-1; i++)
{
k = i;
for (int j=i+1; j<h; j++)
{
if (assisant[k] < assisant[j])
{
k = j;
}
if (assisant[i] != assisant[k])
{
temp = assisant[i];
assisant[i] = assisant[k];
assisant[k] = temp;
}
}
}
for (i=0; i<l; i++)
{
if (assisant[h-1] == abs(c[i].m_value - begin))
{
now = c[i].m_value;
c[i].m_state = 1;
}
}
}
/*
* Sstf算法的创建函数
*/
void Sstf(myArray c[], int l, float &result)
{
int begin, now, temp[NUM]={0};
float average=0.0;
cout<< endl << " 请输入开始时的磁道号: ";
cin>> begin;
cout<< endl;
for (int i=0; i<l; i++)
{
Sort(c, begin, l, now);
temp[i] = abs(now - begin);
begin = now;
cout<< " 被访问的下一个磁道号 " << setw(3) << now << " 移动的距离 " << temp[i] << endl;
average += temp[i];
}
average /= l;
result = average;
cout<< endl << " 平均寻道长度: " << average << endl;
}
/*
* Scan算法用到的变换方向的函数
*/
void MyDirection(myArray c[], int l, int &begin, float &average)
{
int now, temp[NUM]={0};
for (int i=0; i<l; i++)
{
Sort(c, begin, l, now);
temp[i] = abs(now - begin);
begin = now;
cout<< " 被访问的下一个磁道号 " << setw(3) << now << " 移动的距离 " << temp[i] << endl;
average += temp[i];
}
}
/*
* Scan算法用到的辅助结构体初始化函数
*/
void MyInitialize(myArray c[], int l)
{
for (int i=0; i<l; i++)
{
c[i].m_state = 0;
c[i].m_value = 0;
}
}
/*
* Scan算法的创建函数
*/
void Scan(myArray c[], int l, float &result)
{
int begin, direction, p=0, q=0;
float average=0.0;
myArray max[NUM], min[NUM];
MyInitialize(max, l);
MyInitialize(min, l);
cout<< endl << " 请输入开始时的磁道号: ";
cin>> begin;
ER3:cout<< endl << " 从磁道增加方向开始请按 1, 从磁道递减方向开始请按 2: ";
cin>> direction;
if ((direction < 1) || (direction > 2))
{
cout<< endl << " 您输入的有误, 请重新输入. " << endl;
goto ER3;
}
cout<< endl;
for (int i=0; i<l; i++)
{
if (c[i].m_value >= begin)
{
max[p].m_value = c[i].m_value;
p++;
}
else
{
min[q].m_value = c[i].m_value;
q++;
}
}
if (direction == 1)
{
MyDirection(max, p, begin, average);
MyDirection(min, q, begin, average);
}
else
{
MyDirection(min, q, begin, average);
MyDirection(max, p, begin, average);
}
average /= l;
result = average;
cout<< endl << " 平均寻道长度: " << average << endl;
}
/*
* Cscan算法用到的Fcfs函数
*/
void MyFcfs(myArray c[], int l, int begin, float &average)
{
int k, temper, temp[NUM]={0}, assisant[NUM]={0};
for (int i=0; i<l; i++)
{
assisant[i] = c[i].m_value;
}
for (i=0; i<l-1; i++)
{
k = i;
for (int j=i+1; j<l; j++)
{
if (assisant[k] > assisant[j])
{
k = j;
}
if (assisant[i] != assisant[k])
{
temper = assisant[i];
assisant[i] = assisant[k];
assisant[k] = temper;
}
}
}
for (i=0; i<l; i++)
{
temp[i] = abs(assisant[i] - begin);
begin = assisant[i];
cout<< " 被访问的下一个磁道号 " << setw(3) << assisant[i] << " 移动的距离 " << temp[i] << endl;
average += temp[i];
}
}
/*
* Cscan算法的创建函数
*/
void Cscan(myArray c[], int l, float &result)
{
int begin, p=0, q=0;
float average=0.0;
myArray max[NUM], min[NUM];
MyInitialize(max, l);
MyInitialize(min, l);
cout<< endl << " 请输入开始时的磁道号: ";
cin>> begin;
cout<< endl;
for (int i=0; i<l; i++)
{
if (c[i].m_value >= begin)
{
max[p].m_value = c[i].m_value;
p++;
}
else
{
min[q].m_value = c[i].m_value;
q++;
}
}
MyDirection(max, p, begin, average);
MyFcfs(min, q, begin, average);
average /= l;
result = average;
cout<< endl << " 平均寻道长度: " << average << endl;
}
/*
* 随机例子的创建函数
*/
void MySimple(myArray c[])
{
int length, choice;
float sum;
srand(time(NULL));
length = rand() % 30 + 10;
for (int i=0; i<length; i++)
{
c[i].m_state = 0;
c[i].m_value = rand() % 150 + 1;
}
cout<< endl << " 随机选取了磁道总数为: " << length << endl;
choice = rand() % 4 + 1;
switch (choice)
{
case 1: cout<< endl << " 本例采用Fcfs算法. " << endl;
Fcfs(c, length, sum);
break;
case 2: cout<< endl << " 本例采用Sstf算法. " << endl;
Sstf(c, length, sum);
break;
case 3: cout<< endl << " 本例采用Scan算法. " << endl;
Scan(c, length, sum);
break;
case 4: cout<< endl << " 本例采用Cscan算法. " << endl;
Cscan(c, length, sum);
break;
}
}
/*
* 磁盘调度算法的主函数
*/
int main()
{
cout<< " 磁盘调度算法. " << endl;
int length, choice, go, simple;
float sum[4]={0};
myArray container[NUM];
TO: cout<< endl << " 想看一个随机例子请按1, 想自己创建一个例子请按其他键. ";
cin>> simple;
if (simple == 1)
{
MySimple(container);
}
else
{
Initialize(container, length);
cout<< endl << " 磁盘调度系列算法. " << endl;
cout<< endl << " 1 FCFS算法. "<< endl;
cout<< endl << " 2 SSTF算法. "<< endl;
cout<< endl << " 3 SCAN算法. "<< endl;
cout<< endl << " 4 CSCAN算法. " << endl;
ER2: cout<< endl << " 请选择: ";
cin>> choice;
switch (choice)
{
case 1: Fcfs(container, length, sum[0]);
break;
case 2: Sstf(container, length, sum[1]);
break;
case 3: Scan(container, length, sum[2]);
break;
case 4: Cscan(container, length, sum[3]);
break;
default : cout<< endl << " 您输入的有误, 请重新输入. " << endl;
goto ER2;
}
}
cout<< endl << " 想继续看别的算法请按1, 否则请按其他键: ";
cin>> go;
if (go == 1)
{
goto TO;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -