-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.c
494 lines (494 loc) · 8.52 KB
/
code.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
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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
char i=':';
char chEmpty[]="Empty";
struct time
{
int iHrs,iMins;
};
static int Num=0;
struct node
{
int bus_no;
int iBusNum;
int iBusCode;
char chSeat[8][4][20];
char chDrivers_Name[20];
struct time stDeparture_Time, stArrival_Time;
char chGoesTo[20];
char chGoesFrom[20];
float fFare;
int iTicketSold;
struct node *next;
}*p;
//Searching a Bus its bus number
void srch(int num)
{
struct node *r;
r=p;
if(num==1)
{
printf("\nBus code -> %d\tDriver's name -> %s",r->bus_no,r-
>chDrivers_Name);
printf("\nArrival time -> %d%c%d\tDeparture time -> %d%c%d",r-
>stArrival_Time.iHrs,i,r->stArrival_Time.iMins,r->stDeparture_Time.iHrs,i,r-
>stDeparture_Time.iMins);
printf("\nFrom -> %s \t To -> %s",r->chGoesFrom,r->chGoesTo);
printf("\nFare -> %f \t Tickets sold -> %d",r->fFare,r->iTicketSold);
int i1,i2,index=0;
for(i1=0;i1<8;i1++)
{
printf("\n");
for(i2=0;i2<4;i2++)
{
index++;
printf("%d. %s\t ",index,r->chSeat[i1][i2]);
}
}
}
else if(num<=cunt(p))
{
int i;
for(i=1;i<num;i++)
{
r=r->next;
}
printf("\nBus code -> %d\tDriver's name -> %s",r->bus_no,r-
>chDrivers_Name);
printf("\nArrival time -> %d%c%d\tDeparture time -> %d%c%d",r-
>stArrival_Time.iHrs,i,r->stArrival_Time.iMins,r->stDeparture_Time.iHrs,i,r-
>stDeparture_Time.iMins);
printf("\nFrom -> %s \t To -> %s",r->chGoesFrom,r->chGoesTo);
printf("\nFare -> %f \t Tickets sold -> %d",r->fFare,r->iTicketSold);
int i1,i2,index=0;
for(i1=0;i1<8;i1++)
{
printf("\n");
for(i2=0;i2<4;i2++)
{
index++;
printf("%d. %s\t ",index,r->chSeat[i1][i2]);
}
}
}
else
{
printf("\n Wrong Bus number input (Remember, not Bus code, Bus
NUMBER!)\n");
}
}
//counting total number of buses
int cunt(struct node *q)
{
struct node *temp;
temp=q;
int tot=0;
while(temp!=NULL)
{
tot++;
temp=temp->next;
}
return (tot);
}
//deleting a bus entry by its Bus code by its value
void Delete(int num)
{
struct node *temp,*r;
r=p;
while(r!=NULL)
{
if(r->bus_no==num)
{
if(r==p)
{
p=r->next;
free(r);
return;
}
else
{
temp->next=r->next;
free(r);
return;
}
}
else
{
temp=r;
r=r->next;
}
}
printf("No such Bus code found.\n");
}
void insert(int x)
{
struct node *temp,*m;
m=p;
temp=(struct node *)malloc(sizeof(struct node));
temp->bus_no=x;
Num++;
temp->iBusNum=Num;
dname:
printf("Enter driver's name : ");
fflush(stdin);
gets(temp->chDrivers_Name);
if(strlen(temp->chDrivers_Name)>20)
{
printf("Maximum 20 characters are allowed");
goto dname;
}
aTime:
printf("Enter arrival time : ");
fflush(stdin);
scanf("%d%c%d",&temp->stArrival_Time.iHrs,&i,&temp-
>stArrival_Time.iMins);
if(temp->stArrival_Time.iHrs==0)
{
printf("\nInvalid time.");
goto aTime;
}
if((i!=':')||(temp->stArrival_Time.iHrs>=24)||(temp-
>stArrival_Time.iHrs<0)||(temp->stArrival_Time.iMins>=60)||(temp-
>stArrival_Time.iMins<0))
{
printf("\nInvalid time !\n\t\tOnly format HH:MM accepted\n");
goto aTime;
}
dTime:
printf("Enter departure time : ");
fflush(stdin);
scanf("%d%c%d",&temp->stDeparture_Time.iHrs,&i,&temp-
>stDeparture_Time.iMins);
if(temp->stDeparture_Time.iHrs==0)
{
printf("\nInvalid time.");
goto dTime;
}
if((i!=':')||(temp->stDeparture_Time.iHrs>=24)||(temp-
>stDeparture_Time.iHrs<0)||(temp->stDeparture_Time.iMins>=60)||(temp-
>stDeparture_Time.iMins<0))
{
printf("\nInvalid time !\n\t\tOnly format HH:MM accepted\n");
goto dTime;
}
to:
printf("Enter destination : ");
fflush(stdin);
gets(temp->chGoesTo);
if(strlen(temp->chGoesTo)>20)
{
printf("\nOnly 20 characters or less");
goto to;
}
from:
printf("Enter starting place : ");
fflush(stdin);
gets(temp->chGoesFrom);
if(strlen(temp->chGoesFrom)>20)
{
printf("\nOnly 20 characters or less");
goto from;
}
printf("Enter fare of one ticket : ");
fflush(stdin);
scanf("%f",&temp->fFare);
temp->iTicketSold=0;
int i1,i2;
for(i1=0;i1<8;i1++)
{
for(i2=0;i2<4;i2++)
{
strcpy(temp->chSeat[i1][i2],chEmpty);
}
}
if(p==NULL)
{
p=temp;
p->next=NULL;
}
else
{
while(m->next!=NULL)
m=m->next;
m->next=temp;
m=temp;
m->next=NULL;
}
}
void Print(struct node *q)
{
struct node *r;
r=q;
int cnt=1;
while(r!=NULL)
{
line();
printf("\n\nBus code -> %d\tBus number -> %d \tDriver's name -> %s",r-
>bus_no,cnt,r->chDrivers_Name);
printf("\nArrival time -> %d%c%d\tDeparture time -> %d%c%d",r-
>stArrival_Time.iHrs,i,r->stArrival_Time.iMins,r->stDeparture_Time.iHrs,i,r-
>stDeparture_Time.iMins);
printf("\nFrom -> %s \t To -> %s",r->chGoesFrom,r->chGoesTo);
printf("\nFare -> %f \t Tickets sold -> %d",r->fFare,r->iTicketSold);
cnt++;
int i1,i2,index=0;
for(i1=0;i1<8;i1++)
{
printf("\n");
for(i2=0;i2<4;i2++)
{
index++;
printf("%d. %s\t ",index,r->chSeat[i1][i2]);
}
}
r=r->next;
}
printf("\n");
}
void reservation(struct node *q)
{
struct node *r;
r=q;
int num,count=0,Seat;
res:
printf("Enter bus number : ");
fflush(stdin);
scanf("%d",&num);
if(num>cunt(q)||(num<1))
{
printf("Sorry, no bus found with the given Bus number.\n");
goto res;
}
while(count<num-1)
{
r=r->next;
count++;
}
seat:
printf("Enter seat number : ");
fflush(stdin);
scanf("%d",&Seat);
char Name[20];
if(Seat>32)
{
printf("Only 32 seats are there.\n");
goto seat;
}
else if(Seat<1)
{
printf("Invalid input.\n");
goto seat;
}
else if(strcmp(r->chSeat[Seat/4][Seat%4-1],"Empty")==0)
{
printf("Enter the passenger's name : ");
name:
fflush(stdin);
gets(Name);
if(strlen(Name)>20)
{
printf("Max 20 characters allowed\n");
goto name;
}
strcpy(r->chSeat[Seat/4][Seat%4-1],Name);
printf("\nSeat successfully reserved!\nDo you want to continue
reservation ? : ");
r->iTicketSold++;
char ch;
ch=getchar();
if(ch=='y'||ch=='Y')
{
reservation(q);
}
else
main();
}
else
{
printf("Seat already booked!\n");
goto seat;
}
}
void cancel()
{
if(Num==0)
{
printf("Bus list is empty.\n");
main();
}
struct node *r;
r=p;
enter:
printf("Enter Bus number : ");
int bus,ind=1;
fflush(stdin);
scanf("%d",&bus);
if(bus<1||bus>cunt(p))
{
printf("\nInvalid input.\n");
goto enter;
}
else
{
while(ind<bus)
{
r=r->next;
ind++;
}
}
printf("Enter seat number : ");
int sea;
scanf("%d",&sea);
if(strcmp(r->chSeat[sea/4][sea%4-1],"Empty")==0)
{
printf("The seat is already empty !\n");
goto enter;
}
else
{
strcpy(r->chSeat[sea/4][sea%4-1],"Empty");
printf("Successfully canceled reservation.\n");
r->iTicketSold--;
main();
}
}
void daily_profit()
{
struct node *r,*s;
r=p;
s=p;
int cnt=0;
if(cunt(p)==0)
{
printf("No buses are available\n");
main();
}
while(s!=NULL)
{
if(s->iTicketSold!=0)
{
cnt++;
}
s=s->next;
}
if(cnt==0)
{
printf("No booking is done yet.\n");
getch();
main();
}
float Total=0;
while(r!=NULL)
{
Total=Total+(r->iTicketSold*r->fFare);
r=r->next;
}
printf("The total income of the buses is : %.2f\n",Total);
main();
}
void line()
{
int i;
printf("\n");
for(i=0;i<60;i++)
{
printf("*");
}
printf("\n");
}
//The main function
void main()
{
int A;
while(1)
{
line();
printf("\n0.EXIT\n1.INSERTING NEW BUS.");
printf("\n2.REMOVING BUS AND ALL ITS COMPONENTS FROM
LIST BY BUS code");
printf("\n3.PRINTING DETAILS OF ALL BUSES");
printf("\n4.TOTAL NUMBER OF BUSES\n5.SEARCHING BUS BY ITS
BUS NUMBER");
printf("\n6.Reservation of seats\n7.Cancel a reservation\n8.Daily profit.");
printf("\n\nEnter your choice here : ");
scanf("%d",&A);
switch(A)
{
case 1:
{
int x,n,i;
line();
printf("Enter bus code : ");
scanf("%d",&x);
insert(x);
break;
}
case 2:
{
line();
printf("\n Enter Bus code : ");
int num;
scanf("%d",&num);
Delete(num);
Print(p);
}
case 3:
{
line();
Print(p);
break;
}
case 4:
{
line();
printf("Total number of Buses are %d\n",cunt(p));
break;
}
case 5:
{
int num;
line();
printf("Enter Bus number : ");
scanf("%d",&num);
if(num>cunt(p)||num<1)
{
printf("Invalid number !! \n");
main();
}
srch(num);
break;
}
case 6:
{
line();
reservation(p);
break;
}
case 7:
{
line();
cancel();
break;
}
case 8:
{
line();
daily_profit();
break;
}
case 0:
{
line();
exit(1);
break;
}
default:
{
printf("\n\nWrong choice...");
break;
}
}
}
}