[栈和队列]飞机场调度

一开始题意理解错误,于是就有了下面的字符串版,输出的时候将1转换为0001,下面附上代码

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <assert.h>
  4 typedef int ElemType;
  5 
  6 typedef enum
  7 {
  8     false , true
  9 }_bool;//注意变量类型与变量名之间的区别
 10 typedef struct Node
 11 {
 12     int number ;                      //飞机编号
 13     int waitTime;                      //等待时间
 14     struct Node *next ;             //链式队列中结点元素的指针
 15 } QNode , *QueuePtr;
 16 typedef struct
 17 {
 18     QueuePtr front;
 19     QueuePtr rear;
 20 }LinkQueue;
 21 typedef struct node
 22 {
 23     char runwayName[2];                  //跑道编号
 24     _bool busyFlag;                      //判忙标志位
 25     int occupyTime;                      //当前被占用剩余时间
 26     int busyTime;                        //被占用时间总和
 27     struct node *next;                   //结点元素指针
 28 }runway;
 29 
 30 _bool InitQueue(LinkQueue *Q);
 31 _bool DeleteQueue(LinkQueue *Q,ElemType *airNumber,int *waitTime);
 32 _bool InsertQueue(LinkQueue *Q,ElemType e);
 33 _bool IsQueueEmpty(LinkQueue Q);
 34 
 35 void GetStringfromInt(int x,char *,int int_length);              // 将整形变量转换为4位字符串,如1--“0001”
 36 void InitRunway(runway *,int );
 37 void traverseQueue_addWaitTime(LinkQueue *Q);
 38 int main()
 39 {
 40     int curTime,takeoffNumber,landNumber,takeoffWaitTime,landWaitTime;
 41     int freeNum,runwayAmount,landOccupyTime,takeoffOccupyTime,curLandNumber,curTakeoffNumber;
 42     char ptmpAirNumber[5];
 43     int tmpAirNumber,tmpWaitTime,busytime = 0;
 44     LinkQueue landQueue,takeoffQueue;           //飞机降落、起飞队列
 45     runway *headRunway,*ptrRunway;                               //跑道头指针
 46     curTime = -1; takeoffNumber = 1; landNumber = 5001;
 47     takeoffWaitTime = landWaitTime = freeNum = 0;
 48 
 49     scanf("%d%d%d",&runwayAmount,&landOccupyTime,&takeoffOccupyTime);
 50 
 51     InitQueue(&landQueue);  InitQueue(&takeoffQueue);
 52     headRunway = (runway *)malloc(sizeof(runway));  //开辟头结点
 53     headRunway->next =NULL;
 54     InitRunway(headRunway,runwayAmount);            //对跑道进行初始化
 55 
 56     while(1){
 57         curTime++;
 58         freeNum = 0;
 59         printf("Current Time: %4d\n",curTime);
 60         ptrRunway = headRunway;
 61         while(ptrRunway->next){
 62             ptrRunway = ptrRunway->next;
 63             if(ptrRunway->occupyTime)   ptrRunway->occupyTime--;
 64             if(ptrRunway->occupyTime == 0 ){
 65                 freeNum++;
 66                 if(1 == ptrRunway->busyFlag){
 67                     printf("runway %s is free\n",ptrRunway->runwayName);
 68                     ptrRunway->busyFlag = 0;
 69             }
 70             }
 71         }
 72         //读入该分钟要求降落、起飞飞机数,插入队列
 73         scanf("%d%d",&curLandNumber,&curTakeoffNumber);
 74         getchar();                                          //读入换行符
 75         if(curLandNumber < 0 && curTakeoffNumber < 0){
 76             if(!IsQueueEmpty(landQueue)) traverseQueue_addWaitTime(&landQueue);
 77             if(!IsQueueEmpty(takeoffQueue)) traverseQueue_addWaitTime(&takeoffQueue);
 78             break;//机场关闭
 79         }
 80         while(curLandNumber--){
 81             InsertQueue(&landQueue,landNumber);
 82             landNumber++;
 83         }
 84         while(curTakeoffNumber--){
 85             InsertQueue(&takeoffQueue,takeoffNumber);
 86             takeoffNumber++;
 87         }
 88         while(freeNum--){
 89             if(!IsQueueEmpty(landQueue)){               //若降落队列非空
 90                 DeleteQueue(&landQueue,&tmpAirNumber,&tmpWaitTime);
 91                 landWaitTime += tmpWaitTime ;
 92 
 93                 ptrRunway = headRunway;
 94                 while(ptrRunway->next->busyFlag) ptrRunway = ptrRunway->next;
 95                 printf("airplane %4d is ready to land on runway %s\n",
 96                        tmpAirNumber,ptrRunway->next->runwayName);
 97                 ptrRunway->next->busyTime += landOccupyTime;
 98                 ptrRunway->next->occupyTime = landOccupyTime;
 99                 ptrRunway->next->busyFlag = 1;
100             }
101             else if(!IsQueueEmpty(takeoffQueue)){          //若起飞队列非空
102                 DeleteQueue(&takeoffQueue,&tmpAirNumber,&tmpWaitTime);
103                 GetStringfromInt(tmpAirNumber,ptmpAirNumber,4);
104                 takeoffWaitTime += tmpWaitTime ;
105 
106                 ptrRunway = headRunway;
107                 while(ptrRunway->next->busyFlag) ptrRunway = ptrRunway->next;
108                 printf("airplane %s is ready to takeoff on runway %s\n",
109                        ptmpAirNumber,ptrRunway->next->runwayName);
110                 ptrRunway->next->busyTime += takeoffOccupyTime;
111                 ptrRunway->next->occupyTime = takeoffOccupyTime;
112                 ptrRunway->next->busyFlag = 1;
113             }
114             else  break;
115         }
116        if(!IsQueueEmpty(landQueue)) traverseQueue_addWaitTime(&landQueue);
117        if(!IsQueueEmpty(takeoffQueue)) traverseQueue_addWaitTime(&takeoffQueue);
118     }
119     while((!IsQueueEmpty(landQueue)) || (!IsQueueEmpty(takeoffQueue)) || (freeNum != runwayAmount-1 ) ){
120         curTime++;
121         freeNum = 0;
122         printf("Current Time: %4d\n",curTime);
123         ptrRunway = headRunway;
124         while(ptrRunway->next){
125             ptrRunway = ptrRunway->next;
126             if(ptrRunway->occupyTime)   ptrRunway->occupyTime--;
127             if(ptrRunway->occupyTime == 0 ){
128                 freeNum++;
129                 if(1 == ptrRunway->busyFlag){
130                     printf("runway %s is free\n",ptrRunway->runwayName);
131                     ptrRunway->busyFlag = 0;
132             }
133             }
134         }
135         while(freeNum--){
136             if(!IsQueueEmpty(landQueue)){               //若降落队列非空
137                 DeleteQueue(&landQueue,&tmpAirNumber,&tmpWaitTime);
138                 landWaitTime += tmpWaitTime ;
139 
140                 ptrRunway = headRunway;
141                 while(ptrRunway->next->busyFlag) ptrRunway = ptrRunway->next;
142                 printf("airplane %4d is ready to land on runway %s\n",
143                        tmpAirNumber,ptrRunway->next->runwayName);
144                 ptrRunway->next->busyTime += landOccupyTime;
145                 ptrRunway->next->occupyTime = landOccupyTime;
146                 ptrRunway->next->busyFlag = 1;
147             }
148             else if(!IsQueueEmpty(takeoffQueue)){          //若起飞队列非空
149                 DeleteQueue(&takeoffQueue,&tmpAirNumber,&tmpWaitTime);
150                 GetStringfromInt(tmpAirNumber,ptmpAirNumber,4);
151                 takeoffWaitTime += tmpWaitTime ;
152 
153                 ptrRunway = headRunway;
154                 while(ptrRunway->next->busyFlag) ptrRunway = ptrRunway->next;
155                 printf("airplane %s is ready to takeoff on runway %s\n",
156                        ptmpAirNumber,ptrRunway->next->runwayName);
157                 ptrRunway->next->busyTime += takeoffOccupyTime;
158                 ptrRunway->next->occupyTime = takeoffOccupyTime;
159                 ptrRunway->next->busyFlag = 1;
160             }
161             else  break;
162         }
163         if(!IsQueueEmpty(landQueue)) traverseQueue_addWaitTime(&landQueue);
164         if(!IsQueueEmpty(takeoffQueue)) traverseQueue_addWaitTime(&takeoffQueue);
165     }
166     printf("simulation finished\n");
167     printf("simulation time: %4d\n",curTime);
168     printf("average waiting time of landing: %4.1f\n",(float)landWaitTime/(landNumber  - 5001  ));
169     printf("average waiting time of takeoff: %4.1f\n",(float)takeoffWaitTime/(takeoffNumber -1 ));
170 
171     ptrRunway = headRunway;
172     while(ptrRunway->next){
173         ptrRunway = ptrRunway->next;
174         printf("runway %s busy time: %4d\n",ptrRunway->runwayName,ptrRunway->busyTime);
175         busytime += ptrRunway->busyTime;
176     }
177     printf("runway average busy time percentage: %4.1f%c\n",(float)busytime/runwayAmount*100/curTime,'%');
178 
179     return 0;
180 }
181 void traverseQueue_addWaitTime(LinkQueue *Q)
182 {
183     QueuePtr ptr = Q->front;
184     while(ptr->next != NULL){
185         ptr = ptr->next;
186         ptr->waitTime++;
187     }
188 }
189 void InitRunway(runway *head,int num)
190 {
191     runway *ptr = head;
192     int i;
193     for(i = 1;i <= num;i++){
194         ptr->next = (runway *)malloc(sizeof(runway));
195         if(!ptr->next)  exit(1);
196         ptr = ptr->next;
197         ptr->busyFlag = 0;
198         ptr->busyTime = 0;
199         ptr->occupyTime = 0;
200         GetStringfromInt(i,ptr->runwayName,2);
201         ptr->next = NULL;
202     }
203 }
204 void  GetStringfromInt(int x,char *ptr,int int_length)
205 {
206     char mod;   int i = 0;
207     while(i <= (int_length - 1)){
208         *(ptr + i) = '0';
209         i++;
210     }
211     *(ptr + i) = '\0';
212     i = int_length - 1;
213     do{
214         mod = x % 10 + '0';
215         *(ptr + i) = mod;
216         x = x / 10;
217         i--;
218     }while(x);
219 }
220 _bool InitQueue(LinkQueue *Q)
221 {
222     Q->front = Q->rear =  (QueuePtr)malloc(sizeof(QNode));
223     if(!Q->front)   return false;
224     Q->front->next = NULL;
225     return true;
226 }
227 _bool InsertQueue(LinkQueue *Q,ElemType e)//队尾插入
228 {
229     QueuePtr tmp = (QueuePtr)malloc(sizeof(QNode));
230     if(!tmp) return false;
231 
232     tmp->number = e;
233     tmp->waitTime = 0;
234     tmp->next = NULL;
235     Q->rear->next = tmp;
236     Q->rear = tmp;
237     return true;
238 }
239 _bool DeleteQueue(LinkQueue *Q,ElemType *airNumber,int *waitTime)//队头删除
240 {
241     QueuePtr tmp;
242 
243     if(Q->front == Q->rear) return false;
244 
245     tmp = Q->front->next;
246     *airNumber = tmp->number;
247     *waitTime = tmp->waitTime;
248     Q->front->next = tmp->next;
249     if(Q->rear == tmp )
250         Q->rear = Q->front;
251     free(tmp);
252     return true;
253 }
254 _bool IsQueueEmpty(LinkQueue Q)
255 {
256     assert(Q.front != NULL && Q.rear !=NULL);
257 
258     if(Q.front == Q.rear) return true;
259     else return false;
260 }

后来发现,将int型变量转为char型后再输出,完全没有必要,比如printf("%04d",1);就可以直接输出0001,下面是修改后的代码

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <assert.h>
  4 typedef int ElemType;
  5 
  6 typedef enum
  7 {
  8     false , true
  9 }_bool;//注意变量类型与变量名之间的区别
 10 typedef struct Node
 11 {
 12     int number ;                      //飞机编号
 13     int waitTime;                      //等待时间
 14     struct Node *next ;             //链式队列中结点元素的指针
 15 } QNode , *QueuePtr;
 16 typedef struct
 17 {
 18     QueuePtr front;
 19     QueuePtr rear;
 20 }LinkQueue;
 21 typedef struct node
 22 {
 23     int runwayName;                  //跑道编号
 24     _bool busyFlag;                      //判忙标志位
 25     int occupyTime;                      //当前被占用剩余时间
 26     int busyTime;                        //被占用时间总和
 27     struct node *next;                   //结点元素指针
 28 }runway;
 29 
 30 _bool InitQueue(LinkQueue *Q);
 31 _bool DeleteQueue(LinkQueue *Q,ElemType *airNumber,int *waitTime);
 32 _bool InsertQueue(LinkQueue *Q,ElemType e);
 33 _bool IsQueueEmpty(LinkQueue Q);
 34 
 35 void InitRunway(runway *,int );
 36 void traverseQueue_addWaitTime(LinkQueue *Q);
 37 int main()
 38 {
 39     int curTime,takeoffNumber,landNumber,takeoffWaitTime,landWaitTime;
 40     int freeNum,runwayAmount,landOccupyTime,takeoffOccupyTime,curLandNumber,curTakeoffNumber;
 41     int tmpAirNumber,tmpWaitTime,busytime = 0;
 42     LinkQueue landQueue,takeoffQueue;           //飞机降落、起飞队列
 43     runway *headRunway,*ptrRunway;                               //跑道头指针
 44     curTime = -1; takeoffNumber = 1; landNumber = 5001;
 45     takeoffWaitTime = landWaitTime = freeNum = 0;
 46 
 47     scanf("%d%d%d",&runwayAmount,&landOccupyTime,&takeoffOccupyTime);
 48 
 49     InitQueue(&landQueue);  InitQueue(&takeoffQueue);
 50     headRunway = (runway *)malloc(sizeof(runway));  //开辟头结点
 51     headRunway->next =NULL;
 52     InitRunway(headRunway,runwayAmount);            //对跑道进行初始化
 53 
 54     while(1){
 55         curTime++;
 56         freeNum = 0;
 57         printf("Current Time: %4d\n",curTime);
 58         ptrRunway = headRunway;
 59         while(ptrRunway->next){
 60             ptrRunway = ptrRunway->next;
 61             if(ptrRunway->occupyTime)   ptrRunway->occupyTime--;
 62             if(ptrRunway->occupyTime == 0 ){
 63                 freeNum++;
 64                 if(1 == ptrRunway->busyFlag){
 65                     printf("runway %02d is free\n",ptrRunway->runwayName);
 66                     ptrRunway->busyFlag = 0;
 67             }
 68             }
 69         }
 70         //读入该分钟要求降落、起飞飞机数,插入队列
 71         scanf("%d%d",&curLandNumber,&curTakeoffNumber);
 72         getchar();                                          //读入换行符
 73         if(curLandNumber < 0 && curTakeoffNumber < 0){
 74             if(!IsQueueEmpty(landQueue)) traverseQueue_addWaitTime(&landQueue);
 75             if(!IsQueueEmpty(takeoffQueue)) traverseQueue_addWaitTime(&takeoffQueue);
 76             break;//机场关闭
 77         }
 78         while(curLandNumber--){
 79             InsertQueue(&landQueue,landNumber);
 80             landNumber++;
 81         }
 82         while(curTakeoffNumber--){
 83             InsertQueue(&takeoffQueue,takeoffNumber);
 84             takeoffNumber++;
 85         }
 86         while(freeNum--){
 87             if(!IsQueueEmpty(landQueue)){               //若降落队列非空
 88                 DeleteQueue(&landQueue,&tmpAirNumber,&tmpWaitTime);
 89                 landWaitTime += tmpWaitTime ;
 90 
 91                 ptrRunway = headRunway;
 92                 while(ptrRunway->next->busyFlag) ptrRunway = ptrRunway->next;
 93                 printf("airplane %04d is ready to land on runway %02d\n",
 94                        tmpAirNumber,ptrRunway->next->runwayName);
 95                 ptrRunway->next->busyTime += landOccupyTime;
 96                 ptrRunway->next->occupyTime = landOccupyTime;
 97                 ptrRunway->next->busyFlag = 1;
 98             }
 99             else if(!IsQueueEmpty(takeoffQueue)){          //若起飞队列非空
100                 DeleteQueue(&takeoffQueue,&tmpAirNumber,&tmpWaitTime);
101                 takeoffWaitTime += tmpWaitTime ;
102 
103                 ptrRunway = headRunway;
104                 while(ptrRunway->next->busyFlag) ptrRunway = ptrRunway->next;
105                 printf("airplane %04d is ready to takeoff on runway %02d\n",
106                        tmpAirNumber,ptrRunway->next->runwayName);
107                 ptrRunway->next->busyTime += takeoffOccupyTime;
108                 ptrRunway->next->occupyTime = takeoffOccupyTime;
109                 ptrRunway->next->busyFlag = 1;
110             }
111             else  break;
112         }
113        if(!IsQueueEmpty(landQueue)) traverseQueue_addWaitTime(&landQueue);
114        if(!IsQueueEmpty(takeoffQueue)) traverseQueue_addWaitTime(&takeoffQueue);
115     }
116     while((!IsQueueEmpty(landQueue)) || (!IsQueueEmpty(takeoffQueue)) || (freeNum != runwayAmount-1 ) ){
117         curTime++;
118         freeNum = 0;
119         printf("Current Time: %4d\n",curTime);
120         ptrRunway = headRunway;
121         while(ptrRunway->next){
122             ptrRunway = ptrRunway->next;
123             if(ptrRunway->occupyTime)   ptrRunway->occupyTime--;
124             if(ptrRunway->occupyTime == 0 ){
125                 freeNum++;
126                 if(1 == ptrRunway->busyFlag){
127                     printf("runway %02d is free\n",ptrRunway->runwayName);
128                     ptrRunway->busyFlag = 0;
129             }
130             }
131         }
132         while(freeNum--){
133             if(!IsQueueEmpty(landQueue)){               //若降落队列非空
134                 DeleteQueue(&landQueue,&tmpAirNumber,&tmpWaitTime);
135                 landWaitTime += tmpWaitTime ;
136 
137                 ptrRunway = headRunway;
138                 while(ptrRunway->next->busyFlag) ptrRunway = ptrRunway->next;
139                 printf("airplane %04d is ready to land on runway %02d\n",
140                        tmpAirNumber,ptrRunway->next->runwayName);
141                 ptrRunway->next->busyTime += landOccupyTime;
142                 ptrRunway->next->occupyTime = landOccupyTime;
143                 ptrRunway->next->busyFlag = 1;
144             }
145             else if(!IsQueueEmpty(takeoffQueue)){          //若起飞队列非空
146                 DeleteQueue(&takeoffQueue,&tmpAirNumber,&tmpWaitTime);
147                 takeoffWaitTime += tmpWaitTime ;
148 
149                 ptrRunway = headRunway;
150                 while(ptrRunway->next->busyFlag) ptrRunway = ptrRunway->next;
151                 printf("airplane %04d is ready to takeoff on runway %02d\n",
152                        tmpAirNumber,ptrRunway->next->runwayName);
153                 ptrRunway->next->busyTime += takeoffOccupyTime;
154                 ptrRunway->next->occupyTime = takeoffOccupyTime;
155                 ptrRunway->next->busyFlag = 1;
156             }
157             else  break;
158         }
159         if(!IsQueueEmpty(landQueue)) traverseQueue_addWaitTime(&landQueue);
160         if(!IsQueueEmpty(takeoffQueue)) traverseQueue_addWaitTime(&takeoffQueue);
161     }
162     printf("simulation finished\n");
163     printf("simulation time: %4d\n",curTime);
164     printf("average waiting time of landing: %4.1f\n",(float)landWaitTime/(landNumber  - 5001  ));
165     printf("average waiting time of takeoff: %4.1f\n",(float)takeoffWaitTime/(takeoffNumber -1 ));
166 
167     ptrRunway = headRunway;
168     while(ptrRunway->next){
169         ptrRunway = ptrRunway->next;
170         printf("runway %02d busy time: %4d\n",ptrRunway->runwayName,ptrRunway->busyTime);
171         busytime += ptrRunway->busyTime;
172     }
173     printf("runway average busy time percentage: %4.1f%c\n",(float)busytime/runwayAmount*100/curTime,'%');
174 
175     return 0;
176 }
177 void traverseQueue_addWaitTime(LinkQueue *Q)
178 {
179     QueuePtr ptr = Q->front;
180     while(ptr->next != NULL){
181         ptr = ptr->next;
182         ptr->waitTime++;
183     }
184 }
185 void InitRunway(runway *head,int num)
186 {
187     runway *ptr = head;
188     int i;
189     for(i = 1;i <= num;i++){
190         ptr->next = (runway *)malloc(sizeof(runway));
191         if(!ptr->next)  exit(1);
192         ptr = ptr->next;
193         ptr->busyFlag = 0;
194         ptr->busyTime = 0;
195         ptr->occupyTime = 0;
196         ptr->runwayName = i;
197         ptr->next = NULL;
198     }
199 }
200 _bool InitQueue(LinkQueue *Q)
201 {
202     Q->front = Q->rear =  (QueuePtr)malloc(sizeof(QNode));
203     if(!Q->front)   return false;
204     Q->front->next = NULL;
205     return true;
206 }
207 _bool InsertQueue(LinkQueue *Q,ElemType e)//队尾插入
208 {
209     QueuePtr tmp = (QueuePtr)malloc(sizeof(QNode));
210     if(!tmp) return false;
211 
212     tmp->number = e;
213     tmp->waitTime = 0;
214     tmp->next = NULL;
215     Q->rear->next = tmp;
216     Q->rear = tmp;
217     return true;
218 }
219 _bool DeleteQueue(LinkQueue *Q,ElemType *airNumber,int *waitTime)//队头删除
220 {
221     QueuePtr tmp;
222 
223     if(Q->front == Q->rear) return false;
224 
225     tmp = Q->front->next;
226     *airNumber = tmp->number;
227     *waitTime = tmp->waitTime;
228     Q->front->next = tmp->next;
229     if(Q->rear == tmp )
230         Q->rear = Q->front;
231     free(tmp);
232     return true;
233 }
234 _bool IsQueueEmpty(LinkQueue Q)
235 {
236     assert(Q.front != NULL && Q.rear !=NULL);
237 
238     if(Q.front == Q.rear) return true;
239     else return false;
240 }

提交的时候觉得代码还可以简化,因为机场关闭之前和之后的处理基本一致,因此机场关闭之前和关闭之后的操作完全可以在一块处理,但是合并之后的判断条件一直没做好,有志于追求完美的还可以在简化一下,和大家分享一下!!

posted @ 2014-05-06 15:01  wujichaomx  阅读(299)  评论(0编辑  收藏  举报