-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairlineUtils.c
306 lines (265 loc) · 8.29 KB
/
airlineUtils.c
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
#include "airLinesUtils.h"
#define INITIAL_FLIGHTS_SIZE 10
#ifndef ENLARGE_FACTOR
#define ENLARGE_FACTOR 2
#endif// !ENLARGE_FACTOR
AirLine* ManuallyCreateAirline() {
printf("hey, let's create airline ");
printf("Insert Companie's name:\n");
char* cName = getStrExactLength();
int cNumOfPlanes;
printf("Insert number of planes:\n");
scanf("%d", &cNumOfPlanes);
return AutomaticllyCreateAirline(cName, cNumOfPlanes);
}
AirLine* AutomaticllyCreateAirline(char* cName, int cNumOfPlanes) {
AirLine* pAL = NULL;
pAL = (AirLine*)malloc(sizeof(AirLine));
if (pAL == NULL) return NULL;
pAL->name = cName;
pAL->lastIndexFilledInFlightsPtrArray = -1;
pAL->numOfPlanes = cNumOfPlanes;
pAL->allFlights = arraylist_create();
//pAL->FlightArrCapacity = INITIAL_FLIGHTS_SIZE;
pAL->orderBy = 0; // mark not sorted
return pAL;
}
void addFlightToAirLine(AirLine* a1, const Flight* f1) {
arraylist_add(a1->allFlights, (void*)f1);
a1->lastIndexFilledInFlightsPtrArray++;
a1->orderBy = 0; // mark not sorted
}
void addCopyOflightToAirLine(AirLine* a1, const Flight* f1) {
Flight* newFlight = (Flight*)malloc(sizeof(Flight)); // allocates space inside copyFlight
if (!newFlight) return;
copyFlight(f1, newFlight);
arraylist_add(a1->allFlights, (void*)newFlight);
a1->lastIndexFilledInFlightsPtrArray++;
a1->orderBy = 0; // mark not sorted
}
int countCompanyFlightsFromAtoB(const AirLine* a1, char* flyFromA, char* flyToB) {
int retVal = countFlightsFromAtoB(a1->allFlights, flyFromA, flyToB);
if (retVal == -1)
return 0;
return retVal;
}
void printAirlineDetails(const AirLine* ourAirLine) {
arraylist* flightsPtrArr = ourAirLine->allFlights;
if (arraylist_size(flightsPtrArr) == 0)
{
return;
}
Flight* ptrToFlight;
for (size_t i = 0; i < arraylist_size(flightsPtrArr); i++) {
ptrToFlight = (Flight*)arraylist_get(flightsPtrArr, i);
printFlightDetails(ptrToFlight);
}
}
void writeAirlineToBinaryFile(const AirLine* a1) {//if send NULL -> create new file
FILE* f1 = fopen("company.bin", "wb");
// W-> num of planes
fwrite(&a1->numOfPlanes, sizeof(int), 1, f1);
// W-> name
int len1;
len1 = strlen(a1->name) + 1;// +1 for '\0'
fwrite(&len1, sizeof(int), 1, f1); // W-> length of name (for allocation purposes when reading later)
fwrite(a1->name, sizeof(char), len1, f1);
//W-> number of flights
int numOfFlights = a1->lastIndexFilledInFlightsPtrArray + 1;
fwrite(&numOfFlights, sizeof(int), 1, f1);
// W-> sorting ENUM
int enumValue = (int)a1->orderBy; // casting to int (maybe shoudln't? TODO..
fwrite(&enumValue, sizeof(int), 1, f1);
Flight* tFlightPtr = NULL;
//write all flights
for (int i = 0; i < numOfFlights; i++)
{
tFlightPtr = (Flight*)arraylist_get(a1->allFlights, i);
writeFlightToBinaryFile(tFlightPtr, f1);
}
// see how made the sorts, only then know how to write
//TODO
fclose(f1); // else - the function opend the file, needs to close it
}
////// ________________________________________________________________________
void readAirlineFromBinaryFile(AirLine* a1, FILE* f1, Flight* flightsArray, AirportManager* airportDB) {
if (!f1)
return;
// read num of planes
fread(&a1->numOfPlanes, sizeof(int), 1, f1);
// read name
int len1;
fread(&len1, sizeof(int), 1, f1);
a1->name = (char*)malloc(sizeof(char) * len1);
fread(a1->name, len1, 1, f1);
//read number of flighs
int flightsToAdd = 0;
fread(&flightsToAdd, sizeof(int), 1, f1);
// read sorting enum
fread(&a1->orderBy, sizeof(int), 1, f1);
flightsArray = (Flight*)malloc(sizeof(Flight) * flightsToAdd);
if (flightsArray == NULL)
return;
for (int i = 0; i < flightsToAdd; i++)
{
readFlightFromBinaryFile(f1, &flightsArray[i], airportDB);
addFlightToAirLine(a1, &flightsArray[i]);
}
}
void sortFlights(AirLine* a1) {
// void* ===== Flight*
int flag = 0;
char userInput;
# ifdef _USE_GENERAL_VOID_FUNCTIONS
generalArrayFunction(a1->allFlights->body, a1->lastIndexFilledInFlightsPtrArray + 1, sizeof(Flight*),
printFlightDetails_void);
#endif
// Matching data types and pointers for qsort calls
//Keta dohe lifaneha:
Flight* flightsArr = (Flight*)a1->allFlights->body; // from void** ---> Flight*
void* flightsArr_withCostume = (void*)flightsArr; // Flight* ---> void*
int arrSize = a1->lastIndexFilledInFlightsPtrArray + 1;
//______________________
do {
printsSortMenu();
userInput = getCharNOnewLine();
switch (userInput) {
case '1':
qsort(flightsArr_withCostume, arrSize, sizeof(Flight*), compareHour);
a1->orderBy = 1;
break;
case '2':
qsort(flightsArr_withCostume, arrSize, sizeof(Flight*), compareDate);
a1->orderBy = 2;
break;
case '3':
qsort(flightsArr_withCostume, arrSize, sizeof(Flight*), compareSpeed);
a1->orderBy = 3;
default:
printf("bad input, insert again\n");
}
} while (userInput != '3' && userInput != '2' && userInput != '1');
}
void printsSortMenu() {
printf("Choose sort catagory:\n");
printf("1. sort by Hour:\n");
printf("2. sort by Date:\n");
printf("3. sort by Speed:\n");
}
//comperators
int compareHour(const void* hr1, const void* hr2) {
Flight* f2 = *(Flight**)hr2;
Flight* f1 = *(Flight**)hr1;
const int n1 = f1->hourOfTakeoff;
const int n2 = f2->hourOfTakeoff;
if (n1 < n2) return -1;
else if (n1 > n2) return 1;
else return 0;
}
int compareDate(const void* date1, const void* date2)
{
Flight* f1 = *(Flight**)date1;
Flight* f2 = *(Flight**)date2;
int year1, month1, day1, year2, month2, day2;
DateToInt(f1->dateOfTakeoff, &day1, &month1, &year1);
DateToInt(f2->dateOfTakeoff, &day2, &month2, &year2);
if (year1 < year2)
return -1;
else if (year1 > year2)
return 1;
else
{ // same year
if (month1 < month2)
return -1;
else if (month1 > month2)
return 1;
else
{ // same month
if (day1 < day2)
return -1;
else if (day1 > day2)
return 1;
else return 0; // same date
}
}
}
int compareSpeed(const void* speed1, const void* speed2)
{
Flight* f1 = *(Flight**)speed1;
Flight* f2 = *(Flight**)speed2;
float n1, n2;
n1 = f1->fligthVelocity;
n2 = f2->fligthVelocity;
if (n1 < n2) return -1;
else if (n1 > n2) return 1;
else return 0;
}
Flight* findFlightInArray(const AirLine* a1) {
if (a1->orderBy == 0)
return NULL; // array is not sorted
// void* ====> Flight*
// Matching data types and pointers for qsort calls
Flight* flightsArr = (Flight*)a1->allFlights->body; // from void** ---> Flight*
void* flightsArr_withCostume = (void*)flightsArr; // Flight* ---> void*
int arrSize = a1->lastIndexFilledInFlightsPtrArray + 1;
//______________________
void* demoWithCostume = NULL;
Flight* found = NULL;
void* result = NULL;
// if not allocating here, wouldn't be able to read later
Flight* demoFlight = (Flight*)malloc(sizeof(Flight) * 1);
int sortedBy = a1->orderBy;
switch (sortedBy) {
case 1:
createDemoFlight_Hour(demoFlight);
demoWithCostume = (void*)demoFlight;
result = bsearch(&demoWithCostume, flightsArr_withCostume, arrSize, sizeof(Flight*), compareHour);
break;
case 2:
createDemoFlight_Date(demoFlight);
demoWithCostume = (void*)demoFlight;
result = bsearch(&demoWithCostume, flightsArr_withCostume, arrSize, sizeof(Flight*), compareDate);
break;
case 3:
createDemoFlight_Speed(demoFlight);
demoWithCostume = (void*)demoFlight;
result = bsearch(&demoWithCostume, flightsArr_withCostume, arrSize, sizeof(Flight*), compareSpeed);
}
free(demoFlight);
if (result != NULL)
found = *(Flight**)result;
return found;
}
void createDemoFlight_Hour(Flight* f1) {
int hourOfTakeOff = 0;
while (1) {
printf("insert time of takeOff [00-23]:\n");
scanf("%d", &hourOfTakeOff);
if (hourOfTakeOff >= 0 && hourOfTakeOff <= 23) {
makeFictitiousFlight("01/01/1900", hourOfTakeOff, 000, f1);
return;
}
}
}
void createDemoFlight_Date(Flight* f1) {
char* date = getDate();
makeFictitiousFlight(date, 0, 000, f1);
}
void createDemoFlight_Speed(Flight* f1) {
float fSpeed;
printf("insert flight's speed:\n");
scanf("%f", &fSpeed);
makeFictitiousFlight("01/01/1900", 0, fSpeed, f1);
}
void freeAirline(AirLine* a1) {
#ifdef _USE_GENERAL_VOID_FUNCTIONS
generalArrayFunction(a1->allFlights->body, (a1->lastIndexFilledInFlightsPtrArray + 1),
sizeof(Flight*), freeFlight_Void);
#else
// v1 - Works well (changing to generic function)
while (a1->allFlights->size > 0) {
freeFlight((Flight*)arraylist_pop(a1->allFlights));
}
arraylist_destroy(a1->allFlights);
#endif // _USE_GENERAL_VOID_FUNCTIONS
}