大家请看下面这个程序:
我在定义结构体 Event 时,本想定义一指向其本身的指针,由于手误,写成了eve,后来把 event_insert(EVENT *head,EVENT e) 函数中
while 语句条件中的 tmp->next->occuTime 改成了tmp->occuTime 便没有错了,只有三个警告,这是怎么回事,请那位高手指点?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUMMIT 0
#define FINISH 1
#define INTERV 10
#define CloseTime 480
#define NeedMaxCpuTime 8
typedef struct Event{
int Type;
int occuTime;
struct eve* next; //eve 是整个程序没有定义的东西,编译居然能通过
}EVENT;
/*typedef struct event{
int Type;
int occuTime;
struct event* next;
}EVENT;*/
typedef struct job {
int cpuTime;
int submitTime;
struct job *next;
}JOB;
typedef struct {
JOB *front;
JOB *rear;
int length;
} QUEUE;
EVENT event;
EVENT *eventHead;
JOB job;
QUEUE *jobp;
int waitTime;
int jobCount;
void event_insert(EVENT *head,EVENT e)
{
EVENT *p,*tmp;
p=(EVENT*)malloc(sizeof(EVENT));
*p=e;
p->next=NULL;
tmp=head;
while(tmp->next!=NULL && tmp->occuTime<=p->occuTime)
tmp=tmp->next;
/*改前:
while(tmp->next!=NULL && tmp->next->occuTime<=p->occuTime)
tmp=tmp->next;*/
p->next=tmp->next;
tmp->next=p;
}
void event_delete(EVENT *head,EVENT *enptr)
{
EVENT *p;
p=head->next;
if(p!=NULL){
*enptr=*p;
head->next=p->next;
free(p);
}
}
void Q_append(QUEUE *Q,JOB e)
{
JOB *p;
p=(JOB *)malloc(sizeof(JOB));
*p=e; p->next=NULL;
if(Q->front==NULL)
Q->front=Q->rear=p;
else
{
Q->rear->next=p;
Q->rear=p;
}
Q->length++;
}
void Q_delete(QUEUE *Q,JOB *jbptr)
{
JOB *jp;
jp=Q->front;
if(jp!=NULL)
{
Q->front=jp->next;
Q->length--;
*jbptr=*jp;
free(jp);
}
}
void initial( )
{
long t;
eventHead=(EVENT *)malloc(sizeof(EVENT));
eventHead->next=NULL;
jobp=(QUEUE*)malloc(sizeof(QUEUE));
jobp->length=0;
jobp->front=jobp->rear=NULL;
event.Type=SUMMIT;
event.occuTime=0;
event_insert(eventHead,event);
waitTime=0;jobCount=0; t=time(NULL);srand(t);
}
void submit( )
{
int interval;
EVENT nextEn;
interval=rand()%INTERV+1;
nextEn.occuTime=event.occuTime+interval;
nextEn.Type=SUMMIT;
if(nextEn.occuTime<CloseTime)
event_insert(eventHead,nextEn);
job.cpuTime=rand()%NeedMaxCpuTime+1;
job.submitTime=event.occuTime;
Q_append(jobp,job);
if(jobp->length==1)
{
nextEn.Type=1;
nextEn.occuTime=job.submitTime+job.cpuTime;
event_insert(eventHead,nextEn);
}
}
void finish()
{
EVENT nextEn;
Q_delete(jobp,&job);
waitTime+=event.occuTime-job.submitTime;
jobCount++;
if(jobp->length!=0)
{
nextEn.Type=FINISH;
nextEn.occuTime=event.occuTime+jobp->front->cpuTime;
event_insert(eventHead,nextEn);
}
}
void main( )
{
initial();
while(eventHead->next!=NULL)
{
event_delete(eventHead,&event);
switch(event.Type){
case 0: submit();
break;
case 1: finish();
break;
}
}
printf("作业数=%d\n",jobCount);
printf("平均周转时间=%f\n", (float)waitTime/jobCount);
}