SJF

在这里插入图片描述

#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;
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值