c语言编写FCFS和SJF

#include 
#define MAXNUM 100
int id[MAXNUM];
double send[MAXNUM];//提交时间
double process[MAXNUM];//运行时间
double start[MAXNUM];//开始时间
double finsh[MAXNUM];//完成时间
double zz[MAXNUM];//周转时间
double dz[MAXNUM];//带权周转时间
double p = 0;//平均周转时间
double d = 0;//平均带权周转时间
static int n;//进程数
void input()//输入进程数
{
	printf("请输入进程数:");
	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		id[i] = i + 1;
		send[i] = 0;
		process[i] = 0;
		finsh[i] = 0;
		zz[i] = 0;
		dz[i] = 0;
	}
}
void inputSend()//输入提交时间和运行时间
{
	printf("请输入到达时间:");
	for (int i = 0; i < n; i++)
	{
		scanf("%lf", &send[i]);
	}
	printf("请输入服务时间:");
	for (int i = 0; i < n; i++)
	{
		scanf("%lf", &process[i]);
	}
}
void display()//输出数据
{
	printf("******************************************************************\n ");
	printf("你输入的数据:\n");
	printf("进程数:%d\n", n);
	printf("进程   提交时间   运行时间\n");
	for (int i = 0; i < n; i++) {
		printf("  %-2d      %5.2f      %5.2f\n", id[i], send[i], process[i]);
	}
	printf("******************************************************************\n ");
}
void displayResult()//输出数据
{
	printf("进程   提交时间   运行时间   开始时间   结束时间   周转时间   带权周转时间\n");
	for (int j = 1; j <= n; j++) {
		for (int i = 0; i < n; i++) {
			if (id[i] == j) {
				printf("  %-2d      %5.2f      %5.2f      %5.2f      %5.2f      %5.2f         %5.2f\n", id[i], send[i], process[i], start[i], finsh[i], zz[i], dz[i]);
			}
		}
	}
	printf("平均周转时间: %5.2f  \n", p);
	printf("平均带权周转时间: %5.2f  \n", d);
}
void SortST()//按提交时间排序
{
	double temp1 = 0, temp2 = 0, temp3 = 0;

	for (int i = 0; i < n; i++) {//进行排序,将三个有初值的数组进行排序
		for (int j = i; j < n; j++) {
			if (send[i] > send[j]) {
				temp1 = send[i];
				send[i] = send[j];
				send[j] = temp1;
				temp2 = process[i];
				process[i] = process[j];
				process[j] = temp2;
				temp3 = id[i];
				id[i] = id[j];
				id[j] = temp3;

			}
		}
	}
}
void SortSPT() {//按提交时间排序,并使第一个满足短作业优先
	double temp1 = 0, temp2 = 0, temp3 = 0;

	for (int i = 0; i < n; i++) {//进行排序,将三个有初值的数组进行排序
		for (int j = i; j < n; j++) {
			if (send[i] >= send[j]) {
				if (send[i] > send[j]) {//按提交时间排序
					temp1 = send[i];
					send[i] = send[j];
					send[j] = temp1;
					temp2 = process[i];
					process[i] = process[j];
					process[j] = temp2;
					temp3 = id[i];
					id[i] = id[j];
					id[j] = temp3;
				}
				else {
					if (process[i]>process[j]) {//提交时间相同则按进程大小排序
						temp1 = send[i];
						send[i] = send[j];
						send[j] = temp1;
						temp2 = process[i];
						process[i] = process[j];
						process[j] = temp2;
						temp3 = id[i];
						id[i] = id[j];
						id[j] = temp3;
					}
				}
			}
		}
	}
}
void SortPT(int i)//按运行时间排序
{
	int m = i;
	double temp1 = 0, temp2 = 0, temp3 = 0;
	for (i; i < n; i++) {
		for (int j = i; j < n; j++) {
			if (process[i] > process[j]) {
				temp1 = send[j];
				send[j] = send[i];
				send[i] = temp1;
				temp2 = process[j];
				process[j] = process[i];
				process[i] = temp2;
				temp3 = id[j];
				id[j] = id[i];
				id[i] = temp3;
			}
		}
	}
}
void Fcfs() {//先来先服务FCFS
	start[0] = send[0];
	finsh[0] = start[0] + process[0];
	zz[0] = finsh[0] - send[0];
	p = p + zz[0];
	dz[0] = zz[0] / process[0];
	d = d + dz[0];
	for (int i = 1; i < n; i++)
	{
		start[i] = finsh[i - 1];
		if (start[i]= send[i])//查找在已短作业排序后,提交时间小于上一个完成时间的进程
		{
			//若上一进程结束时,m进程未到,则找下一合适进程并于m交换
			double temp1 = 0, temp2 = 0, temp3 = 0;
			temp1 = send[i];
			send[i] = send[m];
			send[m] = temp1;
			temp2 = process[i];
			process[i] = process[m];
			process[m] = temp2;
			temp3 = id[i];
			id[i] = id[m];
			id[m] = temp3;
			break;//找到一个就结束
		}
	}
}
void SJF() //短作业优先
{
	start[0] = send[0];
	finsh[0] = start[0] + process[0];
	zz[0] = finsh[0] - send[0];
	p = p + zz[0];
	dz[0] = zz[0] / process[0];
	d = d + dz[0];
	for (int i = 1; i < n; i++)
	{

		SortPT(i);//因为查找下一个是进行了交换,所以循环排序
		FindS(i);//查找下一个合适进程
		start[i] = finsh[i - 1];
		finsh[i] = start[i] + process[i];
		zz[i] = finsh[i] - send[i];
		p = p + zz[i];
		dz[i] = zz[i] / process[i];
		d = d + dz[i];
	}
	p = p / n;
	d = d / n;
}
void Choose(){
	printf("请选择算法:\n ");
	printf("1.先来先服务FCFS       2.短作业优先SJF\n");
	int m = 0;
	scanf("%d", &m);
	if (m == 1)
	{
		SortST();
		Fcfs();
	}
	else if (m == 2)
	{
		SortSPT();
		SJF();
	}
	else
	{
		printf("您输入有误!!!\n "); \
			printf("******************************************************************\n ");
		Choose();
	}
}

int main()
{
	input();
	inputSend();
	display();
	Choose();
	displayResult();
	getchar();
	getchar();
	return 0;
}

你可能感兴趣的:(c语言编写FCFS和SJF)