-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlight.h
312 lines (258 loc) · 10.5 KB
/
Flight.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#pragma once
#include"Date.h"
#include"Plane.h"
#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;
#define MAX_FLIGHT_ID 15
#define MAX_PLANE_ID 15
#define MAX_ARRIVE 30
#define MAX_PASSENGERID 12
//==================== STRUCT ==========================
const string status_to_string[] = {"Da huy", "Con ve", "Het ve", "Hoan thanh"};
enum FLIGHT_STATUS{HUYCHUYEN, CONVE, HETVE, HOANTAT};
struct Flight{
char flightID[MAX_FLIGHT_ID+1]; // key
char planeID[MAX_PLANE_ID+1];
Date date;
char arrive[MAX_ARRIVE+1];
int status = CONVE;
//ticket[day][dong] (ticket[column][row])
//ve [i][j] chua dat => ticket[i][j] = "none"
char ***ticket;
};
//ds lien ket don tang dan theo key
struct FlightNode{
Flight flight;
FlightNode *next = NULL;
};
typedef FlightNode* PTR_FLIGHT;
//================= FUNTION PROTOTYPE ==================
PTR_FLIGHT newFlightNode();
PTR_FLIGHT newFlightNode(Flight flight);
char*** newTicket(int num_column, int num_row);
int CountFlight(PTR_FLIGHT First);
void InsertOrder(PTR_FLIGHT &First, Flight flight);
PTR_FLIGHT SerchFlight(PTR_FLIGHT First, char *flightID);
string get_column_name(int column);
string get_ticket_name(int column, int row);
bool load_flight_from_file(string file_path, PTR_FLIGHT &flight_list, DSMayBay &plane_list);
bool save_flight_to_file(string file_path, PTR_FLIGHT flight_list, DSMayBay &plane_list);
void deleteTicket(char ***ticket, int num_column, int num_row);
void freeing_flight_memory(PTR_FLIGHT &flight_list, DSMayBay &plane_list);
//================= FUNCTION DEFINITION ================
PTR_FLIGHT newFlightNode(){
PTR_FLIGHT newnode = new FlightNode;
newnode->next = NULL;
return newnode;
}
PTR_FLIGHT newFlightNode(Flight flight){
PTR_FLIGHT newnode = new FlightNode;
newnode->flight = flight;
newnode->next = NULL;
return newnode;
}
char*** newTicket(int num_column, int num_row){
char ***ticket = new char**[num_column];
for(int i = 0; i < num_column; i++){
ticket[i] = new char*[num_row];
for(int j = 0; j < num_row; j++){
ticket[i][j] = new char[MAX_PASSENGERID + 1];
strcpy(ticket[i][j], "none");
}
}
return ticket;
}
void deleteTicket(char ***ticket, int num_column, int num_row){
for(int i = 0; i < num_column; i++){
for(int j = 0; j < num_row; j++){
delete [] ticket[i][j];
}
delete [] ticket[i];
}
delete [] ticket;
}
int CountFlight(PTR_FLIGHT First){
int count = 0;
PTR_FLIGHT p = First;
while(p != NULL){
p = p->next;
count ++;
}
return count;
}
void InsertOrder(PTR_FLIGHT &First, Flight flight){
PTR_FLIGHT newnode = newFlightNode(flight),
prev_node = NULL,
next_node = First;
//Vi danh sach tang dan theo flight ID nen stop khi next_node == NULL || next_node.flightID >= newnode.flightID
while( next_node != NULL && (strcmp(next_node->flight.flightID, newnode->flight.flightID) < 0)){
prev_node = next_node;
next_node = next_node->next;
}
//Insert
if(next_node == First){
//prev_node == NULL
newnode->next = First;
First = newnode;
}else{
//prev_node != NULL
prev_node->next = newnode;
newnode->next = next_node;
}
}
PTR_FLIGHT SerchFlight(PTR_FLIGHT First, char *flightID){
PTR_FLIGHT p = First;
//list tang dan theo flightID nen stop khi (p ==NULL || p.id >= flight ID)
while(p != NULL && (strcmp(p->flight.flightID, flightID) < 0) ){
p = p->next;
}
if(p == NULL) return NULL;
if(strcmp(p->flight.flightID, flightID) == 0) return p;
return NULL;
}
//{0, 1, 2, ... 25} => 'A', 'B', 'C', ... 'Z'
// 26, 27, ... => 'AA', 'AB' , ...
string get_column_name(int column){
string s = "";
while(column >= 0){
s = char('A' + column%26) + s;
column = (column/26) - 1;
}
return s;
}
string get_ticket_name(int column, int row){
return get_column_name(column) + to_string(row+1);
}
bool load_flight_from_file(string file_path, PTR_FLIGHT &flight_list, DSMayBay &plane_list){
cout << "==============================================================================================" << endl
<< "\t\t\tLoad danh sach chuyen bay tu file: " << file_path << endl;
fstream file(file_path, ios::in | ios::binary);
if(!file){
cout << "\tKhong mo duoc file" << endl
<< "\t\t\tLoad danh sach chuyen bay that bai !" << endl
<< "==============================================================================================" << endl << endl;
return false;
}
//Vi trong file da co san thu tu nen khong can insert order
//De bo qua truong hop insert first, dung node fake_first => insert last
PTR_FLIGHT fake_first = newFlightNode();
PTR_FLIGHT lastnode = fake_first;
Flight record; //bien doc file
int index, num_col, num_row, //index: chi so may bay tuong ung;
count = 0, //dem so chuyen bay doc duoc
i, j; //bien vong lap for
Date now = today();
while(file.read(reinterpret_cast<char*>(&record), sizeof(Flight))){
//check du lieu co khop voi planelist khong
index = SerchPlane(plane_list, record.planeID);
if(index == -1){
cout << "Loi: Khong tim thay may bay ma so: " << record.planeID
<< " tuong ung voi chuyen bay: " << record.flightID << endl
<< "\t\t\tLoad danh sach chuyen bay that bai !" << endl
<< "==============================================================================================" << endl << endl;
return false;
}
//Neu da qua thoi gian khoi hanh && status == CONVE || status == HETVE thi status = HOANTAT
if((record.date < now) && (record.status == CONVE || record.status == HETVE))
record.status = HOANTAT;
//doc danh sach ve
num_col = plane_list.n[index]->SoDay;
num_row = plane_list.n[index]->SoDong;
record.ticket = new char**[num_col];
for(i = 0; i < num_col; i++){
record.ticket[i] = new char*[num_row];
for(j = 0; j < num_row; j++){
record.ticket[i][j] = new char[MAX_PASSENGERID + 1];
file.read(record.ticket[i][j], sizeof(char)*(MAX_PASSENGERID + 1));
}
}
//insert
PTR_FLIGHT newnode = newFlightNode(record);
lastnode->next = newnode;
lastnode = lastnode->next;
//in ra man hinh de check
// cout << newnode->flight.flightID << "-"
// << newnode->flight.date.to_string() << "-"
// << newnode->flight.planeID << "-"
// << newnode->flight.status << endl;
count ++;
}
flight_list = fake_first->next;
delete fake_first;
cout << "\t\t\tLoad danh sach chuyen bay thanh cong!(load duoc: " << count << " chuyen bay)" << endl
<< "==============================================================================================" << endl << endl;
file.close();
return true;
}
bool save_flight_to_file(string file_path, PTR_FLIGHT flight_list, DSMayBay &plane_list){
cout << "==============================================================================================" << endl
<< "\t\t\tLuu danh sach chuyen bay vao file: " << file_path << endl;
fstream file(file_path, ios::out | ios::binary);
if(!file){
cout << "\tKhong mo duoc file!" << endl
<< "\t\t\tLuu danh sach chuyen bay thai bai!" << endl
<< "==============================================================================================" << endl << endl;
return false;
}
PTR_FLIGHT p = flight_list;
int count = 0,
index, num_col, num_row,
i, j;
while(p != NULL){
index = SerchPlane(plane_list, p->flight.planeID);
if(index == -1){
cout << "Loi: Khong tim thay may bay ma so: " << p->flight.planeID
<< " tuong ung voi chuyen bay: " << p->flight.flightID << endl
<< "\t\t\tLuu danh sach chuyen bay thai bai!" << endl
<< "==============================================================================================" << endl << endl;
return false;
}
//Luu struct flight
file.write(reinterpret_cast<char*>(&(p->flight)), sizeof(Flight));
//Luu ticket[][]
num_col = plane_list.n[index]->SoDay;
num_row = plane_list.n[index]->SoDong;
for(i = 0; i < num_col; i++)
for(j = 0; j < num_row; j++)
file.write(p->flight.ticket[i][j], sizeof(char)*(MAX_PASSENGERID + 1));
//In ra man hinh de check
// cout << p->flight.flightID << "-"
// << p->flight.date.to_string() << "-"
// << p->flight.planeID << "-"
// << p->flight.status << endl;
count ++;
p = p->next;
}
cout << "\t\t\tLuu danh sach chuyen bay thanh cong!(Luu duoc " << count << " chuyen bay)" << endl
<< "==============================================================================================" << endl << endl;
file.close();
return true;
}
void freeing_flight_memory(PTR_FLIGHT &flight_list, DSMayBay &plane_list){
cout << "==============================================================================================" << endl
<< "\t\t\tGiai phong bo nho danh sach chuyen bay" << endl;
PTR_FLIGHT p = flight_list,
q;
int index,//luu chi so may bay tuong ung
i, j;//bien vong lap
while(p != NULL){
q = p;
p = p->next;
//Giai phong bo nho danh sach ve
index = SerchPlane(plane_list, q->flight.planeID);
if(index == -1){
cout << "Loi khi giai phong bo nho danh sach ve: "
<< "Khong tim thay may bay ma so: " << q->flight.planeID
<< " tuong ung voi chuyen bay: " << q->flight.flightID << endl
<< "\tGiai phong bo nho danh sach ve cua chuyen bay " << q->flight.flightID << " that bai!" << endl << endl;
}else{
deleteTicket(q->flight.ticket, plane_list.n[index]->SoDay, plane_list.n[index]->SoDong);
}
//Giai phong bo nho node
delete q;
}
cout << "\t\t\tGiai phong bo nho danh sach chuyen bay hoan tat!" << endl
<< "==============================================================================================" << endl << endl;
}