一开始题意理解错误,于是就有了下面的字符串版,输出的时候将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
",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
",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
",
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
",
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
",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
",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
",
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
",
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
");
167 printf("simulation time: %4d
",curTime);
168 printf("average waiting time of landing: %4.1f
",(float)landWaitTime/(landNumber - 5001 ));
169 printf("average waiting time of takeoff: %4.1f
",(float)takeoffWaitTime/(takeoffNumber -1 ));
170
171 ptrRunway = headRunway;
172 while(ptrRunway->next){
173 ptrRunway = ptrRunway->next;
174 printf("runway %s busy time: %4d
",ptrRunway->runwayName,ptrRunway->busyTime);
175 busytime += ptrRunway->busyTime;
176 }
177 printf("runway average busy time percentage: %4.1f%c
",(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) = '