# C++模拟作业调度算法（先来先服务算法、短作业优先和响应比高者优先算法）

### 代码：

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
struct zy//每个作业
{
int gettime;//到达时间
int runtime;//运行时间
int k;//标记有没有被用过（用于第二个算法）
int y;//优先级（第三个算法）
};
bool cmp(zy a, zy b)//按照到达时间进行排序
{
return a.gettime < b.gettime;
}
bool cmp2(zy a, zy b)//按照运行时间进行排序
{
return a.runtime < b.runtime;
}
bool cmp3(zy a, zy b)//按照优先级排序
{
return a.y > b.y;
}
int main()
{
/*输入数据*/
int n;
cout << "请输入有几组:";
cin >> n;
zy z[100];
cout << "输入每个作业的到达时间和运行时间" << endl;
for (int i = 0; i < n; i++)
{
cin >> z[i].gettime >> z[i].runtime;
z[i].k = 0;
}

int overtime[100];//结束时间
int starttime[100];//开始时间
int zhouzhuan[100];//周转时间
double dq[100];//带权周转时间

/*先来先服务算法*/
sort(z, z + n, cmp);
for (int i = 0; i < n; i++)
{
if(i==0)starttime[i] = z[i].gettime;
else starttime[i] = z[i].gettime + overtime[i - 1];
overtime[i] = starttime[i] + z[i].runtime;
zhouzhuan[i] = overtime[i] - z[i].gettime;
dq[i] = zhouzhuan[i]*1.0 / z[i].runtime;
}
double T=0, W=0;
for (int i = 0; i < n; i++)
{
T = T + zhouzhuan[i];
W = W + dq[i];
}
T = T / n*1.0;
W = W / n*1.0;
cout << "先来先服务算法的平均时间是： " << T << " 带权平均周转时间是：" << W << endl;

/*短作业优先算法*/
memset(overtime, 0, sizeof(overtime));
memset(starttime, 0, sizeof(starttime));
memset(zhouzhuan, 0, sizeof(zhouzhuan));
memset(dq, 0, sizeof(dq));
T = 0, W = 0;
sort(z, z + n, cmp);
for (int i = 0; i < n; i++)
{
if (i == 0) {
starttime[i] = z[i].gettime;
overtime[i] = starttime[i] + z[i].runtime;
zhouzhuan[i] = overtime[i] - z[i].gettime;
dq[i] = zhouzhuan[i]*1.0 / z[i].runtime;
z[i].k = 1;
}
else {
for (int j = 0; j < n; j++)//找最短时间的作业
{
if (z[j].k == 0 && z[j].gettime <= overtime[i - 1]) {//如果没有使用过且在结束时间内
starttime[i] = z[j].gettime;
overtime[i] = starttime[i] + z[j].runtime;
zhouzhuan[i] = overtime[i] - z[i].gettime;
dq[i] = zhouzhuan[i]*1.0 / z[j].runtime;
z[j].k = 1;
break;
}
}
}
sort(z, z + n, cmp2);
}
for (int i = 0; i < n; i++)
{
T = T + zhouzhuan[i];
W = W + dq[i];
}
T = T / n*1.0;
W = W / n*1.0;
cout << "短作业优先算法的平均时间是： " << T << " 带权平均周转时间是：" << W << endl;

/*高响应比优先算法*/
memset(overtime, 0, sizeof(overtime));
memset(starttime, 0, sizeof(starttime));
memset(zhouzhuan, 0, sizeof(zhouzhuan));
memset(dq, 0, sizeof(dq));
T = 0, W = 0;
sort(z, z + n, cmp);
for (int i = 0; i < n; i++)
{
if (i == 0) {
starttime[i] = z[i].gettime;
overtime[i] = starttime[i] + z[i].runtime;
zhouzhuan[i] = overtime[i] - z[i].gettime;
dq[i] = zhouzhuan[i] * 1.0 / z[i].runtime;
}
else {
int g;//标记最后一个在上一个结束时间内的作业
for (int j = i; j < n; j++)
{
if (z[j].gettime <= overtime[i])g = j;
else break;
}
if (g - i == 1) {//只有一个作业可执行
starttime[i] = z[i].gettime + overtime[i - 1];
overtime[i] = starttime[i] + z[i].runtime;
zhouzhuan[i] = overtime[i] - z[i].gettime;
dq[i] = zhouzhuan[i] * 1.0 / z[i].runtime;
}
else {//需要计算优先级
for (int j = i; j <= g; j++)
{
z[j].y = 1 + (overtime[i - 1] - z[j].gettime) / z[j].runtime;
}
sort(z+i, z + g, cmp3);
starttime[i] = z[i].gettime + overtime[i - 1];
overtime[i] = starttime[i] + z[i].runtime;
zhouzhuan[i] = overtime[i] - z[i].gettime;
dq[i] = zhouzhuan[i] * 1.0 / z[i].runtime;
}
}
}
for (int i = 0; i < n; i++)
{
T = T + zhouzhuan[i];
W = W + dq[i];
}
T = T / n*1.0;
W = W / n*1.0;
cout << "高响应比优先算法的平均时间是： " << T << " 带权平均周转时间是：" << W << endl;
system("pause");
return 0;
}

05-22
12-01
03-25 7168
06-05 406
05-25 4355
08-09 2437
11-27 2216
01-04 823