#include <iostream>
#include <algorithm>
using namespace std;
#define MAXSIZE 5 // 作业数
int number; // 用户输入的进程数量
struct process
{
    string pid;        //作业名(作业号)
    double come_time;  //到达时
    double run_time;   //运行时
    double begin_time; //开始时
    double over_time;  //完成时
    double round_time; //周转时
    double avg_time;   //带权周转时
    double HRR;        //响应比
} pc[MAXSIZE];         //作业数

bool CmpByComeTime(process p1, process p2) // 按到达时间正序排序
{
    /*************begin******************/
return p1.come_time < p2.come_time;

    /*************end******************/
}
bool CmpByPid(process p1, process p2) // 按id号正序排序
{
    /*************begin******************/
return p1.pid < p2.pid;

    /*************end******************/
}
void get_beginAndOver_time() // 计算作业的开始时间与完成时间
{
    /*************begin******************/
for(int i=0;i<number;i++){
        if(i==0){
           pc[i].begin_time = pc[i].come_time; 
        }
        else{
        pc[i].begin_time = pc[i-1].over_time;
        }
        pc[i].over_time = pc[i].begin_time + pc[i].run_time;
    }
    /*************end******************/
}
void get_roundAndAvg_time() // 计算作业的周转时间与带权周转时间
{
    /*************begin******************/
for (int i = 0; i < number; ++i){
    pc[i].round_time = pc[i].over_time - pc[i].come_time; 
    pc[i].avg_time = pc[i].round_time * 1.0 / pc[i].run_time; 
}

    /*************end******************/
}


void printResult() // 打印输出作业的各个时间值
{
    cout << "执行顺序:"; // << endl
    for (int i = 0; i < number; ++i)
    {
        /* code */
        cout << pc[i].pid << " ";
    }
    cout << endl;
    cout << "作业名" << '\t' << "到达时" << '\t' << "运行时" << '\t'
         << "开始时" << '\t' << "完成时" << '\t' << "周转时" << '\t'
         << "带权周转时" << '\t' << endl;
    sort(pc, pc + number, CmpByPid);
    double sum_round_time = 0.0;
    double avg_sum_round_time = 0.0; // 平均周转时间
    double sum_avg_time = 0.0;
    double avg_sum_avg_time = 0.0; // 平均带权周转时间
    for (int i = 0; i < number; ++i)
    {
        sum_round_time += pc[i].round_time;
        sum_avg_time += pc[i].avg_time;
        cout << pc[i].pid << '\t' << pc[i].come_time << '\t'
             << pc[i].run_time << '\t' << pc[i].begin_time << '\t'
             << pc[i].over_time << '\t' << pc[i].round_time << '\t'
             << pc[i].avg_time << endl;
    }
    avg_sum_round_time = sum_round_time * 1.0 / number;
    avg_sum_avg_time = sum_avg_time * 1.0 / number;
    cout << "平均周转时间: " << avg_sum_round_time << endl
         << "平均带权周转时间: " << avg_sum_avg_time << endl;
}

bool CmpByRunTime(process p1, process p2) // 按运行时长正序排序
{

    /*************begin******************/
    return p1.run_time<p2.run_time;


    /*************end******************/
}

void SJF() // SJF(short job first):根据作业的运行时间从小到大依次执行
{
    /*************begin******************/
    // 1 先按到达时排序
sort(pc,pc+number,CmpByComeTime);

    // 2 再按运行时间排序,如果到达时间相同,则按照运行时间从小到大排序
    for (int i = 0; i < number - 1; ++i) {
        for (int j = i + 1; j < number; ++j) {
            if (pc[i].come_time == pc[j].come_time && pc[i].run_time > pc[j].run_time) {
                swap(pc[i], pc[j]);
            }
        }
    }

    // 3 计算作业的开始时间与完成时间  
    get_beginAndOver_time();

    // 4 计算作业的周转时间与带权周转时间  
    get_roundAndAvg_time();
    
    
    /*************begin******************/
}


int main() // 入口函数
{
    cout << "请输入进程个数:";
    cin >> number;
    cout << endl;
    cout << "请分别输入进程的名称、到达时间、服务时间:" << endl;
    for (int i = 0; i < number; i++)
    {
        cin >> pc[i].pid >> pc[i].come_time >> pc[i].run_time;
    }
    cout << endl;
    SJF();
    cout << "the results of SJF are:" << endl;
    printResult();
    cout << endl;
    system("pause");
    return 0;
}

代码修改说明:

  1. 优化排序逻辑: 在SJF()函数中,使用双层循环进行排序,并在到达时间相同的情况下,按照运行时间从小到大进行排序。
  2. 注释说明: 代码中增加了详细的注释,解释了各个代码块的功能和逻辑。
  3. 清晰的代码结构: 代码结构清晰,逻辑易于理解。

测试示例:

假设用户输入以下进程信息:

| 进程名 | 到达时间 | 运行时间 | |---|---|---| | P1 | 0 | 3 | | P2 | 1 | 2 | | P3 | 2 | 1 |

运行程序后,会输出以下结果:

执行顺序: P1 P2 P3

作业名 到达时 运行时 开始时 完成时 周转时 带权周转时

P1 0 3 0 3 3 1

P2 1 2 3 5 4 2

P3 2 1 5 6 4 4

平均周转时间: 3.66667

平均带权周转时间: 2.33333

结论:

通过优化后的代码,SJF算法能够更有效地按照运行时间进行排序,并在到达时间相同的情况下,优先执行运行时间更短的作业,从而提高系统的效率。

C++ SJF算法实现优化:更有效的运行时间排序

原文地址: https://www.cveoy.top/t/topic/n3kP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录