#include<stdio.h>
#include<stdlib.h>
#define N 100
typedef struct process
{
int num;
int time;
int start;
int end;
int begin;
int freetime;
int arrivetime;
int finish;
int turn;
double reaction;
double average;
}P;
P a[N],b;
int wordtime=0;
int decide(n)//判断算法
{
int i,t=0;
for(i=0;i<n;i++)
{
if(a[i].end==1)
{
t=1;
}
else
{
t=0;
break;
}
}
if(t==0)
return 0;
else
return 1;
}
void rr(int i,int j,int n,int x){
while(decide(n)==0)
{
//j=1;
for(i=0;i<n;i++)
{
if(a[i].end==1)
{
}
else if(a[i].end==0)
{
if(a[i].arrivetime<=wordtime)
{
if(a[i].freetime>0)
{
a[i].freetime--;
wordtime++;
}
if(a[i].freetime==0)
{
a[i].end=1;
a[i].finish=wordtime;
}
}
else {
for(j=i;j<n;j++)
{
if(a[j].arrivetime<=wordtime&&a[j].end==0)
{
x=0;
i=j-1;
break;
}
else if(j==i-1)
{
x=1;
break;
}
else
x=1;
if(j==n-1&&i!=0)
j=-1;
}
if(x==1)
wordtime++;
}
}
}
}
}
void main()
{
int n=5,i,t,sum=0,num=2,j,x=1;
double average=0,turn=0;
printf("请输入进程个数:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("
");
printf("请输入第%d个进程到达时间:",i+1);
scanf("%d",&a[i].arrivetime);
printf("请输入第%d个进程需要时间:",i+1);
scanf("%d",&a[i].time);
printf("
");
a[i].start=a[i].arrivetime;
a[i].freetime=a[i].time;//剩余时间赋值
a[i].num=i+1;
a[i].start=a[i].end=0;
}//输入数据
for(i=0;i<n;i++)
{
for(t=i+1;t<n;t++)
{
if(a[t].arrivetime<a[i].arrivetime)
{
b=a[i];
a[i]=a[t];
a[t]=b;
}
else if(a[t].arrivetime==a[i].arrivetime)
{
if(a[i].time>a[t].time)
{
b=a[i];
a[i]=a[t];
a[t]=b;
}
}
}
}//排序
rr(i,j,n,x);
for(i=0;i<n;i++)
{
a[i].turn=a[i].finish-a[i].arrivetime;
a[i].average=(float)a[i].turn/a[i].time;
}
printf("进程号 完成时间 周转时间 带权周转时间
");
for(i=0;i<n;i++)
printf("%d %d %d %.2f
",a[i].num,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("平均周转时间 平均带权周转时间
");
printf("%.2f %.2f
",turn,average);
}