用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

c++ 作业调度模拟

2013-03-11 作者: 小蜜锋举报

[c++]代码库

#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<string.h>

//本程序包括:FIFO,运算时间短的作业优先,优先数调度算法,响应比最高者优先调度算法

const int MAXJOB = 50; //定义最大作业

//定义数据结构体
typedef struct node {
    int number;
    int reach_time;
    int reach_hour;
    int reach_minite;
    int need_time;

    int privilege;

    float excellent;

    int start_time;
    int wait_time;

    int visited;
} job;

job jobs[MAXJOB];
int quantity;

//初始化函数
void initial() {
    int i;

    for(i = 0; i < MAXJOB; i++) {
        jobs[i].number = 0;
        jobs[i].reach_time = 0;
        jobs[i].reach_hour = 0;
        jobs[i].reach_minite = 0;

        jobs[i].privilege = 0;

        jobs[i].excellent = 0;

        jobs[i].start_time = 0;
        jobs[i].wait_time = 0;

        jobs[i].visited = 0;
    }
    quantity = 0;
}

//重置作业数据函数
void reset() {
    int i;

    for(i = 0; i < MAXJOB; i++) {
        jobs[i].start_time = 0;
        jobs[i].wait_time = 0;

        jobs[i].visited = 0;
    }

}

//读入作业数据函数
void readData() {
    FILE *fp;
    char fname[20];
    int i;

    cout << "请输入作业数据文件名:";
    strcpy(fname, "8job.txt");
    cin >> fname;

    if((fp = fopen(fname, "r")) == NULL) {
        cout << "错误,文件打不开,请检查文件名:)" << endl;
    } else {
        while(!feof(fp)) {
            fscanf(fp, "%d %d %d %d", &jobs[quantity].number, &jobs[quantity].reach_time, &jobs[quantity].need_time, &jo
                   bs[quantity].privilege);
            jobs[quantity].reach_hour = jobs[quantity].reach_time / 100;
            jobs[quantity].reach_minite = jobs[quantity].reach_time % 100;
            quantity++;
        }
        //输出初始作业数据
        cout << "输出初始作业数据" << endl;
        cout << "---------------------------------------------------------------" << endl;
        cout.setf(2);
        cout << setw(10) << "作业号" << setw(12) << "到达时间" << setw(14) << "所需时间(分)" << setw(14) << "优先级(0>1)" << endl;

        for(i = 0; i < quantity; i++) {
            cout << setw(10) << jobs[i].number << setw(12) << jobs[i].reach_time << setw(14) << jobs[i].need_time << setw(14) << jobs[i].privil
                 ege << endl;
        }
    }

}

//FIFO算法
void FIFO() {
    int i;
    int current_hour;
    int current_minute;

    int total_time = 0;

    //输出作业流
    cout << endl;
    cout << endl << "FIFO算法作业流" << endl;
    cout << "---------------------------------------------------------------" << endl;
    cout.setf(2);
    cout << setw(10) << "作业号" << setw(12) << "到达时间" << setw(12) << "开始时间" << setw(14) << "周转时间(分)" << endl;

    current_hour = jobs[0].reach_hour;
    current_minute = jobs[0].reach_minite;
    for(i = 0; i < quantity; i++) {
        jobs[i].start_time = current_hour * 100 + current_minute;
        jobs[i].wait_time = (current_hour - jobs[i].reach_hour) * 60 + (current_minute - jobs[i].reach_minite) + jobs[i
                                                                                                                     ].need_time;

        cout << setw(10) << jobs[i].number << setw(12) << jobs[i].reach_time << setw(12) << jobs[i].start_time << setw(14) << jobs[i].wait_
             time << endl;

        current_hour = current_hour + (jobs[i].need_time + current_minute) / 60;
        current_minute = (jobs[i].need_time + current_minute) % 60;

        total_time += jobs[i].wait_time;
    }

    cout << endl << "总周转时间:" << total_time << " 平均周转时间:" << total_time / quantity << endl;
}


//运算时间短的作业优先算法
void shorter() {
    int i, j, p;
    int current_hour;
    int current_minute;
    int current_need_time;

    int total_time = 0;

    //输出作业流
    cout << endl;
    cout << endl << "时间短作业优先算法作业流(开始调度时刻为最后一个作业到达系统的时间)" << endl;
    cout << "------------------------------------------------------------------------" << endl;
    cout.setf(2);
    cout << setw(10) << "作业号" << setw(12) << "到达时间" << setw(14) << "所需时间(分)" << setw(12) << "开始时间" << setw(14) << "周转时间(分)" << endl;

    current_hour = jobs[quantity - 1].reach_hour;
    current_minute = jobs[quantity - 1].reach_minite;
    for(i = 0; i < quantity; i++) {
        current_need_time = 30000;
        for(j = 0; j < quantity; j++) {
            if((jobs[j].visited == 0) && (jobs[j].need_time < current_need_time)) {
                p = j;
                current_need_time = jobs[j].need_time;
            }
        }

        jobs[p].start_time = current_hour * 100 + current_minute;
        jobs[p].wait_time = (current_hour - jobs[p].reach_hour) * 60 + (current_minute - jobs[p].reach_minite) + jobs[p
                                                                                                                     ].need_time;

        cout << setw(10) << jobs[p].number << setw(12) << jobs[p].reach_time << setw(14) << jobs[p].need_time << setw(12) << jobs[p].start_
             time << setw(14) << jobs[p].wait_time << endl;

        current_hour = current_hour + (jobs[p].need_time + current_minute) / 60;
        current_minute = (jobs[p].need_time + current_minute) % 60;

        jobs[p].visited = 1;

        total_time += jobs[p].wait_time;
    }
    cout << endl << "总周转时间:" << total_time << " 平均周转时间:" << total_time / quantity << endl;
}

//优先数调度算法
void privilege() {
    int i, j, p;
    int current_hour;
    int current_minute;
    int current_privilege;

    int total_time = 0;

    //输出作业流
    cout << endl;
    cout << endl << "优先数调度算法作业流(开始调度时刻为最后一个作业到达系统的时间)" << endl;
    cout << "------------------------------------------------------------------------" << endl;
    cout.setf(2);
    cout << setw(10) << "作业号" << setw(12) << "到达时间" << setw(14) << "优先级(0>1)" << setw(12) << "开始时间" << setw(14) << "周转时间(分)" << endl;

    current_hour = jobs[quantity - 1].reach_hour;
    current_minute = jobs[quantity - 1].reach_minite;
    for(i = 0; i < quantity; i++) {
        current_privilege = 30000;
        for(j = 0; j < quantity; j++) {
            if((jobs[j].visited == 0) && (jobs[j].privilege < current_privilege)) {
                p = j;
                current_privilege = jobs[j].privilege;
            }
        }
        jobs[p].start_time = current_hour * 100 + current_minute;
        jobs[p].wait_time = (current_hour - jobs[p].reach_hour) * 60 + (current_minute - jobs[p].reach_minite) + jobs[p
                                                                                                                     ].need_time;

        cout << setw(10) << jobs[p].number << setw(12) << jobs[p].reach_time << setw(14) << jobs[p].privilege << setw(12) << jobs[p].start_
             time << setw(14) << jobs[p].wait_time << endl;

        current_hour = current_hour + (jobs[p].need_time + current_minute) / 60;
        current_minute = (jobs[p].need_time + current_minute) % 60;

        jobs[p].visited = 1;

        total_time += jobs[p].wait_time;
    }
    cout << endl << "总周转时间:" << total_time << " 平均周转时间:" << total_time / quantity << endl;
}

//响应比最高者优先调度算法
void excellent() {
    int i, j, p;
    int current_hour;
    int current_minute;
    float current_excellent;

    int total_time = 0;
    //输出作业流
    cout << endl;
    cout << endl << "响应比高者优先调度算法作业流(开始调度时刻为最后一个作业到达系统的时间)" << endl;
    cout << "------------------------------------------------------------------------" << endl;
    cout.setf(2);
    cout << setw(10) << "作业号" << setw(12) << "到达时间" << setw(12) << "开始时间" << setw(14) << "周转时间(分)" << endl;

    current_hour = jobs[quantity - 1].reach_hour;
    current_minute = jobs[quantity - 1].reach_minite;
    for(i = 0; i < quantity; i++) {
        current_excellent = -1;
        for(j = 0; j < quantity; j++) {
            if(jobs[j].visited == 0) {
                jobs[j].wait_time = (current_hour - jobs[j].reach_hour) * 60 + (current_minute - jobs[j].reach_minite);
                jobs[j].excellent = (float)(jobs[j].wait_time / jobs[j].need_time);
            }
        }

        for(j = 0; j < quantity; j++) {
            if((jobs[j].visited == 0) && (jobs[j].excellent > current_excellent)) {
                p = j;
                current_excellent = jobs[j].excellent;
            }
        }
        jobs[p].start_time = current_hour * 100 + current_minute;
        jobs[p].wait_time = (current_hour - jobs[p].reach_hour) * 60 + (current_minute - jobs[p].reach_minite) + jobs[p
                                                                                                                     ].need_time;

        cout << setw(10) << jobs[p].number << setw(12) << jobs[p].reach_time << setw(12) << jobs[p].start_time << setw(14) << jobs[p].wait_
             time << endl;

        current_hour = current_hour + (jobs[p].need_time + current_minute) / 60;
        current_minute = (jobs[p].need_time + current_minute) % 60;

        jobs[p].visited = 1;

        total_time += jobs[p].wait_time;
    }
    cout << endl << "总周转时间:" << total_time << " 平均周转时间:" << total_time / quantity << endl;

}

//显示版权信息函数
void version() {
    cout << endl << endl;

    cout << " ┏━━━━━━━━━━━━━━━━━━━━━━━┓" << endl;
    cout << " ┃     作业调度模拟系统        ┃" << endl;
    cout << " ┠───────────────────────┨" << endl;
    cout << " ┃   (c)All Right Reserved Neo       ┃" << endl;
    cout << " ┃      sony006@163.com          ┃" << endl;
    cout << " ┃     version 2004 build 1122      ┃" << endl;
    cout << " ┗━━━━━━━━━━━━━━━━━━━━━━━┛" << endl;

    cout << endl << endl;
}

void main() {
    version();
    initial();

    readData();

    FIFO();

    shorter();
    reset();

    privilege();
    reset();

    excellent();
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...