事先说明:
本代码为原创,转载请附原博地址。
代码可以运行,但是运算结果并不知道对错(感觉第三个有问题),凌晨两点写的,大脑并不清醒,欢迎交流修改。
如果我的老师查重的时候看到这篇,请你看一下交作业时间和博客时间来确定这是我本人写的~
理论部分:
简单地说一下吧,毕竟书上应该都有。
先来先服务算法,就非常简单地按照作业到达先后进行计算。
短作业优先算法,是在作业到达的基础上,如果有多个作业都已经到达,选择运行时间短的作业先计算。
响应比高者优先算法,也是在作业到达的基础上,如果有多个作业都已经到达,计算他们的优先级,优先级高的作业先计算。
优先级=1+(等待时间 / 要求服务时间)
代码:
#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;
}