-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
533 lines (511 loc) · 14.4 KB
/
main.cpp
File metadata and controls
533 lines (511 loc) · 14.4 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include <iostream>
#include <bits/stdc++.h>
#include<windows.h>
#include<iomanip>
#include <conio.h>
using namespace std;
void switchcase(int);
void dijkstra(int, int, int);
void searchFlight();
void book(string s, string d, int def, int fno);
void Delete();
void display();
void line();
void line2();
int no_of_paths = 0;
void Sleep(
DWORD dwMilliseconds);
long int flightNo = 672832;
string cities[14] = {"Mumbai", "Delhi", "Gandhinagar", "Kolkata", "Shahpur", "Vishakapatnam", "Lucknow", "Madurai", "Manipur", "Jaipur", "Ooty", "Chandigarh", "Patna", "Banglore"};
int adjMatrixTime[14][14] = {{0, 2, 5, 0, 0, 0, 0, 0, 8, 3, 0, 0, 0, 0},
{2, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0},
{0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9},
{0, 0, 0, 0, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0},
{11, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 7, 8, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 6, 0, 0, 0, 5, 0, 7, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0},
{0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 10, 0, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 4},
{0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 4, 0}};
int adjMatrixCost[14][14] = {{0, 40, 100, 0, 0, 0, 0, 0, 45, 60, 0, 0, 0, 0},
{55, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0},
{0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80},
{0, 0, 0, 0, 15, 65, 0, 0, 0, 0, 0, 0, 0, 0},
{10, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 50, 75, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0},
{0, 25, 0, 0, 0, 30, 0, 10, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0},
{0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 60, 0, 45, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 50},
{0, 0, 0, 0, 0, 35, 0, 0, 0, 0, 0, 0, 15, 0}};
int visited[14];
int path[14];
int path_index = 0;
vector<vector<int>> pathcost(14);
struct node
{
string s, d, name, pass, age, date;
int ticket_id;
struct node *next;
} *booked = NULL;
#define I 32767
int getMinDist(int distance[], int visited[])
{
int V = 14;
int min = I, i, vertex = 0;
for (i = 0; i < V; i++)
{
if (visited[i] == 0 && distance[i] <= min)
{
min = distance[i];
vertex = i;
}
}
return vertex;
}
int getLoop(int visited[])
{
int V = 14;
for (int i = 0; i < V; i++)
{
if (visited[i] == 0)
return 1;
}
return 0;
}
void switchcase(int i)
{
if (i == 1)
searchFlight();
else if (i == 2)
book("a", "b", 0, 0);
else if (i == 3)
Delete();
else if (i == 4)
display();
else if (i == 5)
return;
else
cout << "Enter some valid choice " << endl;
}
void dijkstra(int adjMatrix[14][14], int startVertex, int destination)
{
int V = 14;
int source = startVertex;
int i;
vector<vector<int>> paths(14);
int prev[14] = {0};
int distance[V];
int visited[V];
for (i = 0; i < V; i++)
{
if (adjMatrix[startVertex][i] != 0)
{
distance[i] = adjMatrix[startVertex][i];
prev[i]=source;
}
else
distance[i] = I;
visited[i] = 0;
}
distance[startVertex] = 0;
visited[startVertex] = 1;
prev[source]=-1;
while (getLoop(visited))
{
startVertex = getMinDist(distance, visited);
visited[startVertex] = 1;
for (i = 0; i < V; i++)
{
if (i == startVertex || i == source)
continue;
if (adjMatrix[startVertex][i] != 0 && visited[i] == 0)
{
if (distance[startVertex] + adjMatrix[startVertex][i] < distance[i])
{
distance[i] = distance[startVertex] + adjMatrix[startVertex][i];
prev[i] = startVertex;
}
}
}
}
for (i = 0; i < V; i++)
{
paths[i].push_back(i);
if(i!=source)
{
int d = prev[i];
while (d != -1)
{
paths[i].push_back(d);
d = prev[d];
}
//paths[i].push_back(source);
}
}
i = destination;
int u = paths[i].size() - 1, flag = 0;
cout << cities[paths[i][u]];
for (u = paths[i].size() - 2; u >= 0; u--)
{
flag = 1;
cout << " -> " << cities[paths[i][u]];
}
if (!flag)
{
cout << "No flights available.";
}
cout << endl;
}
void printAllPathsUntil(int adj[14][14], int u, int d)
{
visited[u] = 1;
path[path_index] = u;
path_index++;
int pathcost = 0, pathTime = 0;
if (u == d)
{
if(path_index<=7)
{
flightNo++;
for (int i = 0; i < path_index - 1; i++)
{
pathcost += adjMatrixCost[path[i]][path[i + 1]];
pathTime += adjMatrixTime[path[i]][path[i + 1]];
}
cout<<setw(5)<<flightNo<<setw(15)<<pathTime<<" hrs"<<setw(15)<<"Rs."<<pathcost
<<setw(20);
for (int i = 0; i < path_index - 1; i++)
{
cout << cities[path[i]] << " -> ";
}
cout << cities[path[path_index - 1]] << endl;
line();
no_of_paths++;
cout << endl;
}
}
else
{
for (int i = 0; i < 14; ++i)
{
if (!visited[i] && adj[u][i])
printAllPathsUntil(adj, i, d);
}
}
path_index--;
visited[u] = 0;
}
void printAllPaths(int adj[14][14], int s, int d)
{
for (int i = 0; i < 14; i++)
visited[i] = 0;
Sleep(1000);
printAllPathsUntil(adj, s, d);
}
void searchFlight()
{
int i, j, l = 0;
int source, destination;
do
{
cout << " * Enter source city : " <<endl;
for (i = 0; i < 14; i++)
{
for (j = i; j < i + 3; j++)
{
cout<<" ";
if (j <= 13)
{
cout <<"Press " << j + 1 << "." << cities[j] << "\t\t";
}
else
{
break;
}
}
i = j - 1;
cout << endl;
}
cout << "Enter your choice : ";
cin >> source;
source--;
if (source < 0 || source > 14)
{
cout << "PLEASE ENTER A VALID OPTION" << endl;
}
} while (source < 0 || source > 14);
do
{
cout <<endl<< "* Enter destination city : " <<endl;
for (i = 0; i < 14; i++)
{
for (j = i; j < i + 3; j++)
{
cout<<" ";
if (j <= 13)
{
cout << "Press " << j + 1 << "." << cities[j] << "\t\t";
}
else
{
break;
}
}
i = j - 1;
cout << endl;
}
cout << "Enter your choice : ";
cin >> destination;
destination--;
if (destination < 0 || destination > 14)
{
cout << "PLEASE ENTER A VALID OPTION" << endl;
}
if (destination == source)
{
cout << "PLEASE ENTER A VALID DESTINATION OTHER THAN THE SOURCE" << endl;
}
} while ((destination < 0 || destination >= 14) || destination == source);
cout << " Searching all flights ";
for(int wait=0;wait<5;wait++)
{
cout<<"./";
Sleep(500);
}
cout<<"\n\n";
cout<<setw(55)<<"~ FLIGHT TABLE ~\n";
line();
cout<<setw(5)<<"FlightNo"<<setw(15)<<"Time"<<setw(25)<<"Cost of ticket"
<<setw(20)<<"Route"<<endl;
line();
printAllPaths(adjMatrixCost, source, destination);
cout<<"+------------------------------------+"<<endl;
cout<<"| The most time efficient route is : | ";
dijkstra(adjMatrixTime, source, destination);
cout<<"+------------------------------------+"<<endl;
cout<<"+-------------------------------------+"<<endl;
cout<<"| The most pocket friendly route is : | ";
dijkstra(adjMatrixCost, source, destination);
cout<<"+-------------------------------------+"<<endl;
cout << endl;
do
{
cout<<"\t\t\t\t ~ MENU ~ "<<endl;
cout<<"\t _____";
cout << "\n\n\t\t * Press 1.Search Flight\n\t\t * Press 2.Book Flight\n\t\t * Press 3.Exit" << endl;
cout<<"\t _____";
cout <<endl<<endl<<"Enter your choice : ";
cin >> i;
if (i < 1 || i > 3)
{
cout << "PLEASE ENTER A VALID OPTION" << endl;
}
} while (i <= 0 || i >= 4);
if (i == 1)
{
for (j = 0; j < pathcost.size(); j++)
{
pathcost[j].clear();
}
no_of_paths = 0;
searchFlight();
}
if (i == 2)
{
long int fno;
book(cities[source], cities[destination], 1, fno);
}
if (i == 3)
{
}
}
void display()
{
struct node *p;
if (booked != NULL)
{
cout<<setw(45)<<"~ BOOKED TICKETS ~\n";
line2();
cout<<setw(5)<<"Date"<<setw(20)<<"TicketID"<<setw(15)<<"Name"<<setw(20)<<"Route"<<endl;
line2();
}
else
{
cout << " * Oops!No tickets booked till now !" << endl;
}
p = booked;
while (p != NULL)
{
cout<<setw(5)<<p->date<<setw(15)<<p->ticket_id<<setw(18)<<p->name<<setw(15)<<p->s<<" -> "<<p->d<<endl;
line2();
p = p->next;
}
cout << endl;
}
void Delete()
{
int id;
cout <<" ~ Welcome aboard Flier ! ~" << endl;
cout << " * Enter ticket id to be canceled :";
cin >> id;
struct node *p = booked, *q;
while (p != NULL)
{
q = p;
if (p->ticket_id == id)
{
if (p == booked)
{
booked = booked->next;
delete p;
cout << " The given ticket was canceled ! " << endl;
cout<<" *Thank You "<<endl;
return;
}
else
{
q->next = p->next;
cout << " The given ticket was canceled ! " << endl;
cout<<" *Thank You "<<endl;
delete p;
return;
}
}
}
cout << " --> No such ticket with the given id found ! " << endl;
}
void book(string s, string d, int def, int fno)
{
string name, pass, age, date;
struct node *t, *p;
time_t now = time(0);
cout << " ~ Welcome aboard Flier ! ~" << endl;
cout << " * Enter your name: ";
cin >> name;
fflush(stdin);
cout << " * Enter passport id: ";
cin >> pass;
fflush(stdin);
cout << " * Enter your age : ";
cin >> age;
cout << " * Enter date of travel: ";
cin >> date;
if (def != 1)
{
cout << " * Enter source city : ";
cin >> s;
cout << " * Enter destination city: ";
cin >> d;
}
cout << " * Enter flight No : ";
cin >> fno;
fflush(stdin);
if (booked == NULL)
{
t = new node;
t->s = s;
t->d = d;
t->name = name;
t->age = age;
t->date = date;
t->pass = pass;
t->ticket_id = now;
t->next = NULL;
booked = t;
cout<<"Booking your flight ";
for(int wait=0;wait<5;wait++)
{
cout<<"..";
Sleep(500);
}
cout <<endl<< " Your seat is booked for flight " << fno << " from " << s << " to " << d << " with TicketId " << now << " on " << date <<"." <<endl;
cout << " Thank you !!" << endl;
cout<<endl;
}
else
{
p = booked;
struct node *q;
while (p)
{
q=p;
p = p->next;
}
t = new node;
t->s = s;
t->d = d;
t->name = name;
t->age = age;
t->date = date;
t->ticket_id = now;
t->pass = pass;
t->next = NULL;
q->next=t;
cout<<"Booking your flight ";
for(int wait=0;wait<5;wait++)
{
cout<<"..";
Sleep(500);
}
cout <<endl<< " Your seat is booked for flight " << fno << " from " << s << " to " << d << " with TicketID " << now << " on " << date <<"."<< endl;
cout << " Thank you !!" << endl;
cout<<endl;
}
}
void line()
{
for(int i=1;i<70;i++)
cout<<"--";
cout<<"+";
cout<<"\n";
}
void line2()
{
for(int i=1;i<50;i++)
cout<<"--";
cout<<"+";
cout<<"\n";
}
int main()
{
int i = 0;
cout<<"\a";
Sleep(2000);
cout << "\n\n\tWelcome To Flight Reservation System" << endl<< endl;
cout << " *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
cout << " Book your Flight tickets at affordable prices!" << endl;
cout << " *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
do
{
cout<<"\n\t\t ~ MAIN MENU ~ "<<endl;
cout<<" _____";
cout << "\n\n\t * Press 1.Search Flight\n\t * Press 2.Book Flight\n\t * Press 3.Cancel Flight\n\t * Press 4.View your Bookings\n\t * Press 5.Exit" << endl;
cout<<" _____";
cout <<endl<<endl<<"Enter your choice : ";
cin >> i;
if (i < 1 || i > 5)
{
cout << "PLEASE ENTER A VALID OPTION" << endl;
}
switchcase(i);
} while (i != 5);
cout<<"\t\t\t\t Exiting ";
for(int wait=0;wait<5;wait++)
{
cout<<"..";
Sleep(500);
}
cout<<"\n\t\t\t\t Thank You, Have a Nice day ! :D"<<endl;
return 0;
}
// dijkstra's algorithm to compute the path with lowest cost and lowest time.
// depth first search for computing all paths of the flight.
// linkedlist to store the data of the tickets that have been used.