资源描述
//高响应比优先
#include <stdio.h>
#include <malloc.h>
#define MAX 5
#define M 0
typedef float datatype;
typedef struct node //单链表
{
char name[2];
datatype arrive; //到达时间
datatype service; //服务时间
datatype wait; //等待时间
datatype begin; //开始时间
datatype finish; //结束时间
datatype cycle; //周转时间
datatype right; //带权周转时间
datatype answer; //响应比
struct node *next;
}Lnode;
//输入进程信息,尾插入法将进程节点插入单链表
void input(node *h)
{
Lnode *p;
p=(Lnode*)malloc(sizeof(Lnode));
printf("进程: ");
scanf("%s",p->name);
printf("到达时间: ");
scanf("%f",&p->arrive);
printf("服务时间: ");
scanf("%f",&p->service);
p->next=NULL;
while(h->next!=NULL)
{
h=h->next;
}
p->next=h->next;
h->next=p;
}
//实现高响应比优先
void program(Lnode *&h)
{
int j,k=0,i=1;
datatype max,temp;
Lnode *p,*q;
p=h->next;
p->begin=p->arrive;
p->wait=p->begin-p->arrive; //等待时间开始时间-到达时间
p->finish=p->begin+p->service; //完成时间=开始时间+服务时间
p->cycle=p->service+p->wait; //周转时间=服务时间+等待时间
p->right=p->cycle/p->service; //带权周转时间=周转时间/服务时间
temp=p->finish;
//输出第一个进程的信息
printf("%s\t%3.1f\t%3.1f\t%3.1f\t%3.1f\t %3.1f \t%3.1f\t%3.1f\n",p->name,p->arrive,p->service,p->wait,p->begin,p->finish,p->cycle,p->right);
//查找响应比最高进程,从第二个进程开始
while(i<MAX)
{
j=0;
max=M;
p=h->next;
p=p->next;
while(p!=NULL)
{
if(p->arrive<temp) //如果在上一个进程结束前程序已经到达
{
p->begin=temp; //开始时间=上一个进程的结束时间
p->wait=p->begin-p->arrive;
p->answer=(p->wait+p->service)/p->service; //响应比=(等待时间+服务时间)/服务时间
if(p->answer>max)
{
max=p->answer;
p=p->next;
j++;
}
else break;
}
else p=p->next;
}
p=h->next; //p指向首节点
//找出该节点(进程),并输出该进程信息
while (k<j-1 && p!=NULL)
{
k++;
p=p->next;
}
q=p->next;
q->begin=temp;
q->wait=q->begin-q->arrive;
q->finish=q->begin+q->service;
q->cycle=q->service+q->wait;
q->right=q->cycle/q->service;
printf("%s\t%3.1f\t%3.1f\t%3.1f\t%3.1f\t %3.1f \t%3.1f\t%3.1f\n",q->name,q->arrive,q->service,q->wait,q->begin,q->finish,q->cycle,q->right);
temp=q->finish; //该进程的结束时间赋给temp
p->next=q->next; //删除该节点
free(q); //释放该节点
i++;
}
}
void main()
{
Lnode *h,*p;
h=(Lnode*)malloc(sizeof(Lnode));
h->next=NULL;
char *b[8]={"进程","达到时间","服务时间","等待时间","开始时刻","结束时刻","周转时间","带权周转时间"};
int i=0;
printf("输入进程信息:\n");
while(i<MAX)
{
input(h);
i++;
}
printf("\n");
p=h->next;
while(p!=NULL) //输出单链表
{
printf("进程:%s 到达时间:%3.1f 服务时间:%3.1f\n",p->name,p->arrive,p->service);
p=p->next;
}
printf("\n");
printf("%s %s %s %s %s %s %s %s\n",b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7]);
program(h);
}
展开阅读全文