#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未执行
};
//获取下一个要执行的工作的服务时间
string search_min(JOB job[], int nowtime)
{
//记录符合条件的作业的服务时间
JOB job_servicetime[6];
//给服务时间数组赋值
for (int n = 0; n<5; n++)
{
//符合条件的话赋值
if (job[n].arrivetime <= nowtime && job[n].sfzx == 0)
{
job_servicetime[n].servicestime = job[n].servicestime;
job_servicetime[n].name = job[n].name;
}
//不符合条件赋-1
else
{
job_servicetime[n].servicestime = -1;
job_servicetime[n].name = " ";
}
}
//声明第三个变量,获取最小服务时间用
int temp_time;
string temp_name;
//符合条件的作业的服务时间从小到大排序
for (int i = 0; i<5; i++)
{
for (int j = i + 1; j<5; j++)
{
//去掉不符合条件的作业
if (job_servicetime[i].servicestime != -1)
{
if (job_servicetime[i].servicestime>job_servicetime[j].servicestime)
{
temp_time = job_servicetime[i].servicestime;
temp_name = job_servicetime[i].name;
job_servicetime[i].servicestime = job_servicetime[j].servicestime;
job_servicetime[i].name = job_servicetime[j].name;
job_servicetime[j].servicestime = temp_time;
job_servicetime[j].name = temp_name;
}
}
}
}
string name;
//最小服务时间赋给num变量
for (int k = 0; k<5; k++)
{
//去掉不符合条件的作业
if (job_servicetime[k].servicestime != -1)
{
//num=job_servicetime[k].servicestime;
name = job_servicetime[k].name;
break;
}
}
//返回num
return name;
}
int input()//输入作业信息函数
{
//输入操作
//只记录三个字段的结构体
JOB job_th[6];
//循环输入操作
for (int q = 0; q<5; q++)
{
cout << "请输入作业名,到达时间,运行时间:";
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<5; 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<5; g++)
{
//获取下一个工作名称
string name = search_min(job_th, job_si[index].finishtime);
//按工作名称计算相关值
for (int f = 1; f<5; 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 << "完成时间";
cout.width(10);
cout << "周转时间";
cout.width(9);
cout << "带权周转时间" << endl;
for (int i = 0; i<5; i++)
{
cout.width(9);
cout << job_si[i].name;
cout.width(9);
cout << job_si[i].arrivetime;
cout.width(11);
cout << job_si[i].servicestime;
cout.width(13);
cout << job_si[i].starttime;
cout.width(10);
cout << job_si[i].finishtime;
cout.width(10);
cout << job_si[i].zztime;
cout.width(9);
cout << job_si[i].dqzztime << endl�
评论0