📄 dazuoye.cpp
字号:
#define MAXQUEUE 5
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <error.h>
#include <stdlib.h>
typedef enum action {ARRIVE,LAND} Action;
typedef struct plane{
int id; /*identification numbeer of airplane*/
int tm; /*time of arrival in queue*/
} Plane;
typedef Plane QueueEntry;
typedef struct queue{
int count; /*numberj of airplanes in the queue*/
int front; /*front of the queue*/
int rear; /*rear of the queue*/
QueueEntry entry[MAXQUEUE];
} Queue;
void NewPlane(Plane *p, int *nplanes, int curtime, Action kind);
void Refuse(Plane P, int *nrefuse, Action kind);
void Land(Plane p, int curtime, int *nland, int *landwait);
void Fly (Plane p, int curtime, int *ntakeoff, int *takeoffwait);
void Start (int *endtime,double *expectarrive, double *expectdepart);
void Idle (int curtime, int *idletime);
void Conclude(int nplanes, int nland,int ntakeoff, int nrefuse,
int landwait, int takeoffwait, int idletime, int endtime,
Queue *pt, Queue *pl);
void Randomize(void); /*Generate a pseudo-random integer*/
int PoissonRandom (double expectedvalue); /*Simulate the distribution of Poisson */
bool UserSayYes(void);
/*The series functions of Queue*/
void CreateQueue(Queue *q);
int Queuefull (Queue *q);
int Queueempty (Queue *q);
int Queuesize (Queue *q);
void Add(QueueEntry x, Queue *q);
void Remove(QueueEntry *x, Queue *q);
int main (void) /*the main program*/
{
Queue landing, takeoff;
Queue *pl=&landing;
Queue *pt=&takeoff;
Plane plane;
int curtime; /*current time, one unit=time for take off or landing*/
int endtime; /*total number of time units to run*/
double expectarrive; /*number of planes arriving in one unit*/
double expectdepart; /*number of planes newly ready to take off*/
int i; /*loop control variable*/
int idletime; /*number of units when runway is idle*/
int landwait; /*total waiting time for planes landed*/
int nland; /*number of planes landed*/
int nplanes; /*number of planes processed so far*/
int nrefuse; /*number of planes refused use of airport*/
int ntakeoff; /*number of planes taken off*/
int pri; /*pseudo-random integer*/
int takeoffwait; /*total waiting time for takeoff*/
CreateQueue(pl);
CreateQueue(pt);
nplanes=nland=ntakeoff=nrefuse=0;
landwait=takeoffwait=idletime=0;
Start(&endtime,&expectarrive,&expectdepart);
for (curtime=1; curtime<=endtime; curtime++)
{
pri=PoissonRandom(expectarrive);
for (i=1; i<=pri; i++) /*Add to landing queue*/
{
NewPlane(&plane,&nplanes,curtime,ARRIVE);
if (Queuefull(pl))
Refuse(plane,&nrefuse,ARRIVE);
else
Add(plane,pl);
}
pri=PoissonRandom(expectdepart);
for (i=1; i<=pri; i++) /*Add to takeoff queue*/
{
NewPlane(&plane,&nplanes,curtime,LAND);
if (Queuefull(pt))
Refuse(plane,&nrefuse,LAND); //If the queue of landing is full ,refuse the request.
else
Add(plane,pt);
}
if (!Queueempty(pl)) /*Bring planes to land*/
{
Remove(&plane,pl);
Land(plane,curtime,&nland,&landwait);
}
else if(!Queueempty(pt)) /*Allow planes to take off*/
{
Remove(&plane,pt);
Fly(plane,curtime,&ntakeoff,&takeoffwait);
}
else
Idle(curtime,&idletime);
}
Conclude(nplanes,nland,ntakeoff,nrefuse,landwait,
takeoffwait,idletime,endtime,pt,pl);
return 0;
}
/*Start: Print messages and initialize the parameters.
Pre: none
Post: Ask users for responses and initializes all variables,
specified as parameters.
Uses: Uses say yes*/
void Start (int *endtime,double *expectarrive, double *expectdepart)
{
bool ok;
printf("This program simulates an airport with only one runway.\n"
"One plane can land or take off in each unit of time.\n"
"Up to %d planes can be waiting to land or take off"
"at any time\n", MAXQUEUE);
printf("How many units of time will be the simulation run?\n");
scanf("%d",endtime);
Randomize(); /*Initialize random number generaton*/
do
{
printf("Expected number of arrivals in each unit time"
"?\n");
scanf("%lf",expectarrive);
printf("Expected number of departures in each unit time?\n");
scanf("%lf",expectdepart);
if(*expectarrive<0.0 || *expectdepart<0.0)
{
printf("These numbers must be nonnegative.\n");
ok=1;
}
else if(*expectarrive+*expectdepart>1.0)
{
printf("The airport will become satarated."
"Read new members?");
ok=!UserSayYes(); /*if user say yes ,repeat loop*/
}
else
ok=1;
}while(ok==0);
}
/*NewPlane: Make a new record for a plane,and update nplanes.
Pre: None
Post: Makes a new structure for a plane and updates nplanes.*/
void NewPlane (Plane *p, int *nplanes, int curtime, Action kind)
{
(*nplanes)++;
p->id=*nplanes;
switch (kind) //Judge the plane is ready to land or take off.
{
case ARRIVE:
printf("Plane %3d ready to land.\n", *nplanes);
break;
case LAND :
printf("Plane %3d ready to take off.\n", *nplanes);
break;
}
}
/*Refuse: Process a plane when the queue is full.
Pro: None.
Post: Process a plane waiting to use the runway,but the queue is full.*/
void Refuse (Plane p, int *nrefuse, Action kind)
{
switch (kind)
{
case ARRIVE:
printf("Plane %3d directed to another airport.\n", p.id);
break;
case LAND :
printf("Plane %3d told to try later.\n", p.id);
break;
}
(*nrefuse)++;
}
/*Land: Process a plane that is actually landing.
Pre: None.
Post: Process a Plane p that is actually landing*/
void Land(Plane p, int curtime, int *nland, int *landwait)
{
int wait;
wait=curtime-p.tm;
printf("%3d plane %3d landed; in queue %3d units.\n",curtime, p.id, wait);
(*nland)++;
*landwait+=wait;
}
/*Fly: Process a plane that is actually taking off.
Pre: None
Post: Process a plane that is actually taking off. */
void Fly (Plane p, int curtime, int *ntakeoff, int *takeoffwait)
{
int wait;
wait=curtime-p.tm;
printf("%3d plane %3d took off; in queue %3d units.\n",curtime, p.id, wait);
(*ntakeoff)++;
*takeoffwait+=wait; //Add all the takeoff time together,and get the total waiting time.
}
/*Idle: Updates variables for idle runway.
Pre: None.
Post: Updates variables for a time unit when the runway is idle.*/
void Idle (int curtime, int *idletime)
{
printf("%2d : Runway is idle.\n",curtime);
(*idletime)++;
}
/*Conclude: Write out statistics and conclude the simulation.
Pre: None
Post: Write out all the statistics and conclude the simulation.*/
void Conclude(int nplanes, int nland,int ntakeoff, int nrefuse,
int landwait, int takeoffwait, int idletime, int endtime,
Queue *pt, Queue *pl)
{
printf("Simulation hsa concluded after %d units.\n", endtime);
printf("Total number of planes processed: %3d\n", nplanes);
printf("Number of planes landed: %3d\n", nland);
printf("Number of planes taken off: %3d\n", ntakeoff);
printf("Number of planes refused use: %3d\n", nrefuse);
printf("Number of planes left ready to land: %3d\n", Queuesize(pl));
printf("Number of planes left ready to take off: %3d\n", Queuesize(pt));
if (endtime>0)
printf("Percentage of time runway idle: %6.2f\n", ((double)idletime/endtime)*100.0);
if (nland>0)
printf("Average wait time to land: %6.2f\n", (double)landwait/nland);
if (ntakeoff>0)
printf("Average wait time to take off: %6.2f\n", (double)takeoffwait/ntakeoff);
}
/*Randomize: Set starting point for preudorandom integer*/
void Randomize(void)
{
srand((unsigned int)time(NULL)%10000);
}
/*PoissonRandom: Generate a preudorandom integer according to the Poisson distribution
Pre: None.
Post: Generate a preudorandom integer according to the Poisson distribution with the
value given as the parameter*/
int PoissonRandom (double expectedvalue)
{
int n=0; /*for loop*/
double limit;
double x;
limit=exp(-expectedvalue);
x=rand()/(double)32767;
while (x>limit)
{
n++;
x*=rand()/(double)32767;
}
return n;
}
/*Uses: Ture if the user wants to continue execution
Pre: None.
Post: Return Ture if the user wants to continue execution,
FAlSE if the user input either 'n' or 'N'*/
bool UserSayYes(void)
{
int c;
printf("Go on with the execution?(y,n)\n");
do
{
while ((c=getchar())=='\n');
;
if (c=='y' || c=='Y')
return 1;
else if (c=='n' || c=='N')
return 0;
else
printf("You must give me a response by inputing 'y' or 'n'\n");
}while(1);
}
/*Add: Add an entry to the queue.
Pre: The queue has been created and is not full
Post: The entry x will be stored into the queue as its last entry*/
void Add(QueueEntry x, Queue *q)
{
if (Queuefull(q))
perror("Can not append a entry to a full queue");
else
{
q->count++;
q->rear=(q->rear+1)%MAXQUEUE;
q->entry[q->rear]=x;
}
}
/*Remove: Remove the first entry in the queue.
Pre: The queue has been created and is not empty
Post: The first entry of the queue will be moved out of the queue and return as the
value of x.*/
void Remove(QueueEntry *x, Queue *q)
{
if (Queueempty(q))
perror("Can not serve from a queue that is empty");
else
{
q->count--;
*x=q->entry[q->rear];
q->front=(q->front+1)%MAXQUEUE;
}
}
/*CreateQueue: Create the queue.
Pre: None.
Post: Create a queue and initialize the queue to be empty.*/
void CreateQueue (Queue *q)
{
q->count=0;
q->front=0;
q->rear=-1;
}
/*Queuefull: Return 1 if the queue is really full;
else return 0 instead.
Pre: The queue has been created.
Post: Return 1 if the queue is really full;
else return 0instead.*/
int Queuefull (Queue *q)
{
if (q->count==MAXQUEUE)
return 1;
else
return 0;
}
/*Queueempty: Return 1 if the queue is really empty;
else return 0 instead.
Pre: The queue has been created.
Post: Return 1 if the queue is really empty;
else return 0instead.*/
int Queueempty (Queue *q)
{
if (q->count==0)
return 1;
else
return 0;
}
/*Queuesize: Return the number of entries in the queue.
Pre: The queue has been created.
Post: Return the number of entries in the queue.*/
int Queuesize (Queue *q)
{
return q->count;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -