fcfs
#include<stdio.h>
#include<stdlib.h>
#define N 100
typedef struct process
{
int time; //需要时间
char name;
int end;
int freetime; //剩余剩余时间
int arrivetime; //到达时间
int finish; //完成时间
int turn; //周转时间
double average; //平均带权周转时间
}P;
P a[N],b;
int wordtime=0;
void fcfs(int i,int t,int n)
{
a[0].finish=a[0].arrivetime+a[0].freetime;//给予a[0]初值
wordtime=a[0].finish;
a[0].turn=a[0].time;
a[0].average=(float)a[0].turn/a[0].time;
for(i=1;i<n;i++)//执行
{
if(a[i].arrivetime<a[i-1].finish)
{
a[i].finish=a[i-1].finish+a[i].freetime;
a[i].turn=a[i].finish-a[i].arrivetime;
}
else
{
a[i].finish=a[i].arrivetime+a[i].freetime;
a[i].turn=a[i].time;
}
a[i].average=(float)a[i].turn/a[i].time;
wordtime=a[i].finish;
}
}
void main()
{
int i,j,n,sum=0;
double average=0,turn=0;
printf("请输入进程个数: \n");
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
printf("第%d个进程到达时间:",i+1);
scanf("%d",&a[i].arrivetime);
printf("第%d个的需要时间 :",i+1);
scanf("%d",&a[i].time);
printf("\n");
a[i].freetime=a[i].time;//剩余时间赋值
}//输入数据
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j].arrivetime<a[i].arrivetime)
{
b=a[i];
a[i]=a[j];
a[j]=b;
}
else if(a[j].arrivetime==a[i].arrivetime)
{
if(a[i].time>a[j].time)
{
b=a[i];
a[i]=a[j];
a[j]=b;
}
}
}
}//排序
fcfs(i,j,n);
printf("完成时间\t周转时间\t带权周转\n");
for(i=0;i<n;i++)
printf("%d\t\t%d\t\t%.2f\n",a[i].finish,a[i].turn,a[i].average);
for(i=0;i<n;i++)
{
turn=turn+(float)a[i].turn;
average=average+(float)a[i].turn/a[i].time;
}
turn=turn/n;
average=average/n;
printf("平均周转 平均带权周转\n");
printf("%.2f\t\t%.2f\n",turn,average);
}