#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
struct JOB//作业结构体
{
string name; //进程名
float arrivetime;//到达时间
float servicestime;//服务时间
float starttime; //开始时间
float finishtime;//完成时间
float zztime; //周转时间
float dqzztime; //带权周转时间
int sfzx;//是否执行标志位 1已执行 0未执行
float xyb;//记录响应比
};
//获取下一个要执行的工作的服务时间
string search_min(JOB job[], int nowtime)
{
//记录符合条件的作业的中间
JOB job_hrrn[6];
//给中间数组赋值
for (int n = 0; n<6; n++)
{
//符合条件的话赋值
if (job[n].arrivetime <= nowtime && job[n].sfzx == 0)
{
job_hrrn[n].servicestime = job[n].servicestime;
job_hrrn[n].name = job[n].name;
job_hrrn[n].arrivetime = job[n].arrivetime;
float waittime = nowtime - job_hrrn[n].arrivetime;
float d = waittime + job_hrrn[n].servicestime;
job_hrrn[n].xyb = d / job_hrrn[n].servicestime;
//cout<<job_hrrn[n].name<<waittime<<d<<job_hrrn[n].xyb<<endl;
}
//不符合条件赋-1
else
{
job_hrrn[n].servicestime = -1;
job_hrrn[n].name = "-1";
job_hrrn[n].arrivetime = -1;
job_hrrn[n].xyb = -1;
}
}
//声明第三个变量,获取最小服务时间用
float temp_xyb;
string temp_name;
//符合条件的作业的服务时间从小到大排序
for (int i = 0; i<6; i++)
{
for (int j = i + 1; j<6; j++)
{
//去掉不符合条件的作业
if (job_hrrn[i].servicestime != -1)
{
if (job_hrrn[i].xyb>job_hrrn[j].xyb)
{
temp_xyb = job_hrrn[i].xyb;
temp_name = job_hrrn[i].name;
job_hrrn[i].xyb = job_hrrn[j].xyb;
job_hrrn[i].name = job_hrrn[j].name;
job_hrrn[j].xyb = temp_xyb;
job_hrrn[j].name = temp_name;
}
}
}
}
/*for(int w=0;w<6;w++)
{
cout.width(9);
cout<<job_hrrn[w].name;
cout.width(9);
cout<<job_hrrn[w].arrivetime;
cout.width(11);
cout<<job_hrrn[w].servicestime;
cout.width(13);
cout<<job_hrrn[w].xyb<<endl;
}*/
string name;
//最小服务时间赋给num变量
for (int k = 5; k >= 0; k--)
{
//去掉不符合条件的作业
if (job_hrrn[k].xyb != -1)
{
//num=job_servicetime[k].servicestime;
name = job_hrrn[k].name;
break;
}
}
//返回num
return name;
}
int input()//输入作业信息函数
{
//输入操作
//只记录三个字段的结构体
JOB job_th[6];
//循环输入操作
for (int q = 0; q<6; q++)
{
cout << "\n请输入作业名,到达时间,运行时间:";
cin >> job_th[q].name >> job_th[q].arrivetime >> job_th[q].servicestime;
//标志是否已经执行
job_th[q].sfzx = 0;
}
JOB job_si[6];//记录七个字段的结构体
//sfzx字段初值为0(表示未执行)
for (int s = 0; s<6; s++)
{
job_si[s].sfzx = 0;
}
//给第一个作业赋值
job_si[0].name = job_th[0].name;
job_si[0].arrivetime = job_th[0].arrivetime;
job_si[0].servicestime = job_th[0].servicestime;
job_si[0].starttime = job_si[0].arrivetime;
job_si[0].finishtime = job_si[0].arrivetime + job_si[0].servicestime;
job_si[0].zztime = job_si[0].finishtime - job_si[0].arrivetime;
job_si[0].dqzztime = job_si[0].zztime / job_si[0].servicestime;
job_si[0].sfzx = 1;//更新执行状态
job_th[0].sfzx = 1;//更新执行状态
//记录job_si结构体的成员数量
int index = 0;
//算法核心部分
for (int g = 1; g<6; g++)
{
//获取下一个工作名称
string name = search_min(job_th, job_si[index].finishtime);
//string name=search_min(job_th,job_si[0].finishtime);
//cout<<name;
//按工作名称计算相关值
for (int f = 1; f<6; f++)
{
if (job_th[f].name == name)
{
index = index + 1;
job_si[index].name = job_th[f].name;
job_si[index].arrivetime = job_th[f].arrivetime;
job_si[index].servicestime = job_th[f].servicestime;
job_si[index].starttime = job_si[index - 1].finishtime;
job_si[index].finishtime = job_si[index].starttime + job_si[index].servicestime;
job_si[index].zztime = job_si[index].finishtime - job_si[index].arrivetime;
job_si[index].dqzztime = job_si[index].zztime / job_si[index].servicestime;
job_si[index].sfzx = 1;
job_th[f].sfzx = 1;
break;
}
}
}
//按执行顺序输出工作
cout.width(9);
cout << "作业名";
cout.width(9);
cout << "到达时间";
cout.width(11);
cout << "服务时间";
cout.width(13);
cout << "开始时间";
cout.width(10);
cout << "结束时间";
c�
评论0