磁盘调度算法(FCFS&&SSTF)

操作系统磁盘调度算法之FCFS和SSTF篇,我感觉自己写的好垃圾啊!!!

#include 
#include 
using namespace std;
void FCFS(int a[],int n)
{
    int sum = 0;
    int i,j,now;
    float avg;
    cout << "请输入当前的磁道号:";
    cin >> now;//确定当前磁头所在位置
    cout << "磁盘调度顺序为:" << endl;
    for(i = 0; i < n; i++)//按访问顺序输出磁道号
    {
        cout << a[i] <<" ";
    }
    cout << endl;
    sum += abs(a[0] - now);
    cout << "从当前位置到第1个磁道移动的磁道数为:" << sum << endl;
    for(i = 0,j = 1; j < n; i++,j++)
    {
        sum += abs(a[j] - a[i]);//外围磁道与最里面磁道的距离
        cout << "从第" << i+1 << "个磁道到第" << j+1 << "个磁道的移动距离为:" << abs(a[j] - a[i]) << endl;
    }
    cout << endl;
    cout << "移动的总磁道数为:" << sum <1.0 / n;
    cout << "平均寻道长度为:" << avg << endl;
}

void SSTF(int a[],int n)
{
    int temp;
    int now,L,r;
    int j,i;
    int k = 1;
    int sum = 0;
    float AVG;
    //冒泡排序法对磁道号进行排序
    for(i = 0; i < n; i++)
        for(j = i + 1; j < n; j++)
    {
        if(a[i] > a[j])
        {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    cout <<"按递增顺序排好的磁道:" << endl;
    for(i = 0; i < n; i++)
        cout << a[i] << " ";//输出排好的磁道顺序
    cout << endl;
    cout << "请输入当前的磁道号:";
    cin >> now;//确定当前磁头所在位置
    cout << "磁盘调度顺序为:" << endl;
    if(now >= a[n-1])//当前磁头位置大于最外层欲访问磁道
    {
        for(i = n-1; i >= 0; i--)
            cout << a[i] << " " << endl;//则磁盘调用从大到小
        cout << "从当前位置到第1个磁道移动的磁道数为:" << now - a[n-1] << endl;
        for(i = 0,j = 1; j < n; i++,j++)
            cout << "从第" << i+1 << "个磁道到第" << j+1 << "个磁道的移动距离为:" << abs(a[j] - a[i]) << endl;
        sum = now - a[0];//(now - a[n-1]) + (a[n-1) - a[n-2]) + .... + (a[1] -a[0])
    }
    else if(now < a[0])//当前磁头位置小于最外层欲访问磁道
    {
        for(i = 0; i < n; i++)
            cout << a[i] << " " << endl;//则磁盘调用从小到大
        cout << "从当前位置到第1个磁道移动的磁道数为:" << a[0] - now << endl;
        for(i = 0,j = 1; j < n; i++,j++)
            cout << "从第" << i+1 << "个磁道到第" << j+1 << "个磁道的移动距离为:" << abs(a[j] - a[i]) << endl;
        sum = a[n-1] - now;//同上面
    }
    else
    {
        while(a[k] < now)
            k++;//确定当前磁道在已排序的序列中的位置
        L = k -1;//在磁头位置的前一个欲访问磁道
        r = k;//磁头欲访问位置
        while((L >= 0) && (r < n))
        {
            if((now - a[L]) <= (a[r] - now))//选择离磁头近的磁道
            {
                cout << a[L] << " ";
                sum += now - a[L];
                now = a[L];//将磁头位置移到第L个
                L--;//L减一,准备好下一个
            }
            else
            {
                cout << a[r] << " ";
                sum += a[r] - now;
                now = a[r];//将磁头位置移到第r个
                r++;//r增一,准备好下一个
            }
        }
        if(L = -1)//磁头位置里侧的磁道已访问完
        {
            for(j = r; j < n; j++)
                cout << a[j] << " ";
            sum += a[n-1] -a[0];
        }
        if(r == n)
        {
            for(j = k - 1; j > -1; j--)
                cout << a[j] << " ";
            sum += a[n-1] - a[0];
        }
    }
        cout << endl;
        cout << "移动的总磁道数:" << sum <1.0 / n;
        cout << "平均寻道长度为:" << AVG << endl;
}

int main()
{
    int n;//此道的个数
    int s;//功能号
    int i;
    cout << "请输入磁道个数:" << endl;
    cin >> n;
    int *a = new int[n];
    for(i = 0;i < n; i++)
    {
        cout << "请输入第" << i+1 << "个磁道号:";
        cin >> a[i];
    }
    cout << endl;
    while(1)
    {
        cout << endl;
        cout << "1:先来先服务算法:" << endl;
        cout << "2:最短寻道时间算法:" << endl;
        cout << "0:退出" << endl;
        cout << endl;
        cout << "请选择功能号:";
        cin >> s;
        if(s > 2)
        {
            cout << "输入错误!" << endl;
        }
        else
        {
            switch(s)
            {
                case 0:exit(0);break;
                case 1:FCFS(a,n);break;
                case 2:SSTF(a,n);break;
            }
        }
    }
    return 0;
}

磁盘调度算法(FCFS&&SSTF)_第1张图片
磁盘调度算法(FCFS&&SSTF)_第2张图片

你可能感兴趣的:(操作系统实验课)