#include<iostream>
using namespace std;
struct PCB
{
char jobname;
int Tin;
int Tr;
int Ts;
int Tc;
int T;
float W;
bool finish;
}PCB[10];
void RankTin(int job)
{
for (int i = 0;i < job - 1;i++)
{
for (int j = 0;j < job - i - 1;j++)
{
if (PCB[j].Tin > PCB[j + 1].Tin)
{
struct PCB temp;
temp = PCB[j];PCB[j] = PCB[j + 1];PCB[j + 1] = temp;
}
}
}
}
void RankTr(int job)
{
for (int i = 0;i < job - 1;i++)
{
for (int j = 0;j < job - i - 1;j++)
{
if (PCB[j].Tr > PCB[j + 1].Tr)
{
struct PCB temp;
temp = PCB[j];PCB[j] = PCB[j + 1];PCB[j + 1] = temp;
}
}
}
}
void SJF(int job)
{
int judge, mark = job - 2;
float T1 = 0, W1 = 0;
cout << "Please input jobs' Name & Tin & Tr!" << endl;
for (int i = 0;i < job;i++)
{
cin >> PCB[i].jobname >> PCB[i].Tin >> PCB[i].Tr;
PCB[i].finish = 0;
}
RankTin(job);
PCB[0].finish = 1;
PCB[0].Ts = 0;
PCB[0].Tc = PCB[0].Tr;
judge = PCB[0].Tc;
RankTr(job);
while (mark)
{
for (int i = 0;i < job;i++)
{
if ((PCB[i].finish != 1) && (PCB[i].Tin < judge))
{
PCB[i].Ts = judge;
PCB[i].Tc = PCB[i].Ts + PCB[i].Tr;
PCB[i].finish = 1;
judge = PCB[i].Tc;
}
}
mark--;
}
RankTin(job);
for (int i = 0;i < job;i++)
{
PCB[i].T = PCB[i].Tc - PCB[i].Tin;
PCB[i].W = PCB[i].T / float(PCB[i].Tr);
T1 += PCB[i].T;
W1 += PCB[i].W;
}
T1 = T1 / job;
W1 = W1 / job;
cout << "T1=" << T1 << endl;
cout << "W1=" << W1 << endl;
}
int main()
{
int job;
cout << "Please input the number of jobs!" << endl;
cin >> job;
SJF(job);
cout << "work" << '\t' << "Tin" << '\t' << "Tr" << '\t' << "Ts" << '\t' << "Tc" << '\t' << "T" << '\t' << "W" << endl;
for (int i = 0;i < job;i++)
{
cout << PCB[i].jobname << '\t' << PCB[i].Tin << '\t' << PCB[i].Tr << '\t' << PCB[i].Ts << '\t' << PCB[i].Tc << '\t' << PCB[i].T << '\t' << PCB[i].W << endl;
}
system("pause");
return 0;
}
SJF
最新推荐文章于 2024-03-16 00:11:24 发布