#include
#include
#include
#include
#define QUENELEN 6
#define QUENENUM 3
#define MAXTIME 360
struct cCar
{
int num;
int waitTime;
int serverTime;
int arriveTime;
int leaveTime;
int quene;
struct cCar * next;
} ;
struct cQuene
{
x05int quene;
int queneNum;
int num[QUENELEN];
int waitTime[QUENELEN];
int serverTime[QUENELEN];
};
void initCar(struct cCar * s)
{
s->num = 0;
s->waitTime = 0;
s->serverTime = 0;
s->arriveTime = 0;
s->leaveTime = 0;
s->quene = 0;
s->next = NULL;
}
void printQuene(struct cQuene * s)
{
x05int i;
x05printf("%dt%dn",s->queneNum ,s->quene );
x05for(i=0;iserverTime [i]);
x05printf("n");
x05for(i=0;iwaitTime [i]);
x05printf("n");
x05for(i=0;inum [i]);
x05printf("n");
x05printf("n");
}
void initQuene(struct cQuene * s,int n)
{
int i;
x05s->quene = n;
s->queneNum = 0;
for(i = 0; i < QUENELEN; i++)
{
s->num[i] = 0;
s->waitTime[i] = 0;
s->serverTime[i] = 0;
}
}
int curTime=0,Num=1;
struct cCar firstCar;
struct cQuene Quene[QUENENUM];
void initQuene()
{
x05int i;
x05for(i=0; iserverTime[i];
return count;
}
void updataQuene(struct cQuene * s)
{
int i;
x05struct cCar *p = &firstCar;
s->serverTime[0]--; //洗车时间减1
if(s->serverTime[0] next)
x05x05x05{
x05x05x05x05if(p->num == s->num [0]) break;
x05x05x05x05p = p ->next ;
x05x05x05}
x05x05}
x05x05if(p)
x05x05{
x05x05x05p->leaveTime = curTime+1; //记录离开时间
x05x05x05p->waitTime = s->waitTime[0]; //记录等待时间
x05x05}
x05x05else printf("没有找到编号为%d的车",s->num [0]);
for(i = 0; i < s->queneNum; i++) //队列移动
{
s->num[i] = s->num[i+1];
s->waitTime[i] = s->waitTime[i+1];
s->serverTime[i] = s->serverTime[i+1];
}
s->num[i] = 0;
s->waitTime[i] = 0;
s->serverTime[i] = 0;
if(s->queneNum>0) s->queneNum--; //队列数减1
}
x05for(i = 1; i queneNum; i++) s->waitTime[i]++; //等待时间加1
}
void updataQuene()
{
x05int i;
x05for(i=0;i 0) updataQuene(&Quene[i]);
x05x05//printQuene(&Quene[i]);
x05}
}
void addCar()
{
x05const int serverTime[3] = {10,15,25};
x05struct cCar * p = &firstCar;
x05int i,j,queneWait[QUENENUM],index[QUENENUM],minpos,tmp;
x05while(p->next) p = p ->next ; //最后一条记录
x05p->next = (struct cCar *)malloc(sizeof(struct cCar)); //添加新车
x05p = p ->next;
x05p->arriveTime = curTime; //车到达时间
x05p->num = Num++; //到达序号
x05p->next = NULL;
x05p->serverTime = serverTime[rand() % 3]; //服务时间
x05p->leaveTime = curTime;
x05p->waitTime = 0;
x05p->quene = -1;
x05
x05for(i=0; iquene = index[i];
x05x05x05x05break;
x05x05x05}
x05x05}
x05}
}
void main()
{
x05int i,num=0,count=0;
x05int wait = rand() % 30;
x05struct cCar * p = &firstCar,*p2;
x05srand(time(NULL));
x05initQuene();
x05while(curTimenext ;
x05printf("编号t队列号t到达t离开t服务t等待n");
x05while(p)
x05{
x05x05printf("%dt%dt%dt%dt%dt%dn",p->num ,p->quene ,p->arriveTime ,p->leaveTime ,p->serverTime ,p->waitTime );
x05x05if( p->arriveTime != p->leaveTime)
x05x05{
x05x05x05num ++;
x05x05x05count += p->waitTime;
x05x05}
x05x05p = p->next ;
x05}
x05printf("平均等待时间%fn",(double) count / (num+1));
x05p = firstCar.next ;
x05while(p )//释放空间
x05{
x05x05p2 = p;
x05x05p = p->next ;
x05x05free(p2);
x05}
}