#define MAXNUM 100
typedef struct Node1
{int name;
int time;
int gettime;
struct Node1 *next;
}Lnode1;
void waitlest(Lnode1 *p)
{Lnode1 *q,*head;
int a,wt,st,ft;
wt=0;st=0;ft=0;
printf("任务 所需CPU时间(s) 等待时间 结束时间 开始时间\n");
head=p;
while(head->next)
{q=head;
p=q->next;
while(p->next)
{ q=q->next;
p=p->next;
if(q->timetime)
{a=q->name;
q->name=p->name;
p->name=a;
a=q->time;
q->time=p->time;
p->time=a;
}
}
printf(" %d ",p->name);
printf("%10d",p->time);
wt=ft;
st=wt;
ft=ft+p->time;
printf("%10d",wt);
printf("%10d",ft);
printf("%10d",st);
printf("\n");
q->next=NULL;
free(p);
}
}
void shijiancha(Lnode1 *p)
{Lnode1 *first,*head,*q,*r;
int a,n,t;
printf("任务 \n");
first=p;
p=first->next;
n=p->gettime;
q=(Lnode1*)malloc(sizeof(struct Node1));
head=q;
while(p)
{r=(Lnode1*)malloc(sizeof(struct Node1));
q->next=r;
q=q->next;
q->next=NULL;
q->name=p->name;
q->time=p->time;
q->gettime=p->gettime;
r=head->next;
if(r->next)
{if(r->next->next)
{q=r->next;
while(q->next)
{
if(r->time>q->time)
{a=r->name;
r->name=q->name;
q->name=a;
a=r->time;
r->time=q->time;
q->time=a;
}
q=q->next;
r=r->next;
}//end while q n
}//end if r n n
}//end if r n
if(head->next->next)
{t=p->gettime-n;
n=p->gettime;
while(t!=0)
{if(t>head->next->time)
{t=t-head->next->time;
printf(" %d ",head->next->name);
printf("\n");
head->next=head->next->next;
}
else {if(tnext->time)
{head->next->time=head->next->time-t;
t=0;
}
else
{t=0;
printf(" %d ",head->next->name);
printf("\n");
head->next=head->next->next;
}
}
}//end while t
}//end if h n n
p=p->next;
}//end while p
r=head->next;
q=r->next;
while(q)
{
if(r->time>q->time)
{a=r->name;
r->name=q->name;
q->name=a;
a=r->time;
r->time=q->time;
q->time=a;
}
q=q->next;
r=r->next;
}
r=head->next;
while(r)
{ printf(" %d ",r->name);
printf("\n");
r=r->next;
}
}//end shijiancha
void main()
{int t,a,b,n;
Lnode1 *p,*q,*first;
p=(Lnode1*)malloc(sizeof(struct Node1));
first=p;
a=0;
printf("输入1,选择忽略提交任务的时间差;输入2,选择考虑提交任务的时
间差:");
scanf("%d",&b);
do
{ q=(Lnode1*)malloc(sizeof(struct Node1));
p->next=q;
p=p->next;
printf("请输入任务名:");
scanf("%d",&n);
p->name=n;
if(b==2)
{ printf("请输入任务提交时刻:");
scanf("%d",&t);
p->gettime=t;
}
else p->gettime=0;
printf("请输入任务所需时间(s):");
scanf("%d",&t);
p->time=t;
printf("是否继续添加任务?输入1继续提交,输入0结束提交 ");
scanf("%d",&a);
}while(a!=0);
p->next=NULL;
if(b==1)
{ printf("输入1:按提交顺序执行;输入2:按平均等待时间最短");
scanf("%d",&a);
if(a==1)
{ p=first->next;
printf("任务 所需CPU时间(s)\n");
while(p)
{
printf(" %d ",p->name);
printf(" %d",p->time);
printf("\n");
p=p->next;
}
}
else waitlest(first);
}
else shijiancha(first);
}