该算法实现调度完成时间,周转时间,平均带权周转时间等
源代码在线查看: diaodu2.cpp
#include "stdio.h"
#include "stdlib.h"
#define getjcb(type)(type*)malloc(sizeof(type))
#define NULL 0
struct jcb
{
char name[10];
int htime;
int ntime;
char state;
struct jcb *link;
} *ready=NULL,*p;
typedef struct jcb JCB;
int input()
{
int num,i,t=0;
void sort();
printf("\n请输入作业个数:");
scanf("%d",&num);
for(i=1;i {
printf("\n作业 %d",i);
p=(JCB*)malloc(sizeof(JCB));
printf("\n作业名:");
scanf("%s",p->name);
printf("\n作业运行时间:");
scanf("%d",&p->ntime);
p->htime=t; p->state='w'; p->link=NULL;
sort();
t++;
}
return t;
}
void sort()
{
JCB *flag;
if(ready==NULL)
ready=p;
else
{
flag=ready;
while(flag->link!=NULL)
flag=flag->link;
flag->link=p;
}
}
void show()
{
JCB *pr;
printf("\n***正在运行的作业是 :%s ",p->name);
printf("\nname\thtime\tntime\tstate\n");
printf("%s\t",p->name);
printf("%d\t",p->htime);
printf("%d\t",p->ntime);
printf("%c\t",p->state);
printf("\n\n***就绪队列");
for(pr=ready;pr!=NULL;pr=pr->link)
{
printf("\nname\thtime\tntime\tstate\n");
printf("%s\t",pr->name);
printf("%d\t",pr->htime);
printf("%d\t",pr->ntime);
printf("%c\t",pr->state);
}
}
void running()
{
printf("\n作业 [%s]已经完成\n",p->name);
free(p);
}
void main()
{
char c;
int t=0; int tb,tc,ti,wi;
int tis=0,wis=0,i=0;
t=input();
while(ready!=NULL)
{
c=getchar();
++i;
p=ready;
ready=p->link;
p->link=NULL;
p->state='r';
tb=t;
show();
//printf("\npress any key to continue...");
c=getchar();
running();
tc=tb+p->ntime;//完成时间
ti=tc-p->htime;//周转时间
tis+=ti;
wi=ti/p->ntime;//平均带权周转
wis+=wi;
printf("\n作业 %d 开始: \n", t);
printf("\n作业[%d] 完成:\n",t);
printf("\n周转时间:%d\n",ti);
printf("\n带权周转时间: %d\n",wi);
t=t+p->ntime;
//printf("\npress any key to continue...");
c=getchar();
}
printf("\n***所有作业都已经完成");
printf("\n总周转时间:%d\n",tis/i);
printf("\n总带权周转时间:%d\n",wis/i);
//printf("\npress any key to continue...");
c=getchar();
}