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