forked from kishanrajput23/Awesome-Project-Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parking Management
749 lines (602 loc) · 15 KB
/
Parking Management
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/* Vehicle parking program */
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define CAR 1
#define SCOOTER 2
#include <stdlib.h>
/* to store vehicle number, and its
row-col position in an array */
struct vehicle
{
int num ;
int row ;
int col ;
int type ;
struct tm *at;
} ;
struct vehicle *car[2][10] ;
struct vehicle *scooter[2][10] ;
int parkinfo[4][10] ; /* a 2-D array to store number of vehicle parked */
int vehcount ; /* to store total count of vehicles */
int carcount ; /* stores total count of cars */
int scootercount ; /* stores total count of scooters */
/* creates a 2-D array of car and scooter class */
void display( ) ;
void changecol ( struct vehicle * ) ;
struct vehicle * add ( int, int, int, int ) ;
void del ( struct vehicle * ) ;
void getfreerowcol ( int, int * ) ;
void getrcbyinfo ( int, int, int * ) ;
void show() ;
int randint();
int backupw();
int backupr();
struct vehicle * addonstart ( int, int, int, int ) ;
void total_bill(int ,struct tm *ct1,struct tm *ct2);
void get_arrival_time(int);
/* decrements the col. number by one
this fun. is called when the data is
shifted one place to left */
void changecol ( struct vehicle *v )
{
v -> col--;
}
struct tm* datetime()
{
time_t ts;
struct tm *ct;
ts = time(NULL);
ct = localtime(&ts);
/* scan the year, month and year from the input string */
return ct ;
}
int insertrecord(int veh,int type,int row,int col, struct tm *ct )
{
FILE *fptr;
fptr = fopen("arival.dat", "a");
if (fptr == NULL)
{
printf("File does not exists \n");
return 0 ;
}
fprintf(fptr,"\n");
fprintf(fptr,"%d ",veh);
fprintf(fptr,"%d ",type);
fprintf(fptr,"%d ",row);
fprintf(fptr,"%d ",col);
fprintf(fptr,"%d/%d/%d ",
ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900);
fprintf(fptr,"%d:%d:%d ",
ct->tm_hour, ct->tm_min, ct->tm_sec);
fclose(fptr);
}
int insertrecord2(int veh, struct tm *ct )
{
FILE *fptr;
fptr = fopen("depart.dat", "a");
if (fptr == NULL)
{
printf("File does not exists \n");
return 0 ;
}
fprintf(fptr,"\n");
fprintf(fptr,"%d ",veh);
fprintf(fptr,"%d/%d/%d ",
ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900);
fprintf(fptr,"%d:%d:%d ",
ct->tm_hour, ct->tm_min, ct->tm_sec);
fclose(fptr);
}
void get_arrival_time(int num)
{
int veh ,type,row,col ;
int mon,day,year,hour,min,sec;
int mon2,day2,year2,hour2,min2,sec2;
int mon1,day1,year1,hour1,min1,sec1;
int mon3,day3,year3,hour3,min3,sec3;
FILE *fptr;
fptr = fopen("arival.dat", "r");
if (fptr == NULL)
{
printf("File does not exists \n");
return 0 ;
}
while(!feof(fptr))
{
fscanf(fptr,"\n");
fscanf(fptr,"%d ",&veh);
fscanf(fptr,"%d ",&type);
fscanf(fptr,"%d ",&row);
fscanf(fptr,"%d ",&col);
fscanf(fptr,"%d/%d/%d ",
&day, &mon, &year);
fscanf(fptr,"%d:%d:%d ",
&hour, &min, &sec);
if(veh == num){
day2 = day;
mon2 = mon;
year2 = year;
hour2 = hour;
min2 = min;
sec2 = sec ;
}
}
fclose(fptr);
// geting time for departure of vehicle
FILE *fptr1;
fptr1 = fopen("depart.dat", "r");
if (fptr1 == NULL)
{
printf("File does not exists \n");
return 0 ;
}
while(!feof(fptr1))
{
fscanf(fptr1,"\n");
fscanf(fptr1,"%d ",&veh);
fscanf(fptr1,"%d/%d/%d ",
&day1, &mon1, &year1);
fscanf(fptr1,"%d:%d:%d ",
&hour1, &min1, &sec1);
if(veh == num){
day3 = day1;
mon3 = mon1;
year3 = year1;
hour3 = hour1;
min3 = min1;
sec3 = sec1 ;
}
}
fclose(fptr1);
// algo to start calculating times
//times
int tsec1,tsec2,tsec3;
tsec1 = sec3;
tsec1 += min3*60;
tsec1 += (hour3*60)*60;
tsec2 = sec2;
tsec2 += min2*60;
tsec2 += (hour2*60)*60;
tsec3 = tsec1 - tsec2 ;
int secnd , minute ,hours;
int secc , temp1,temp2,temp3,temp4 ;
secnd = tsec3 % 60 ;
temp1 = tsec3 - secnd ;
temp2 = temp1/60 ;
minute = temp2 % 60 ;
temp4 = temp2 - minute ;
hours = temp4 /60 ;
printf("you have parked your vehicle for %d/%d/%d",hours,minute,secnd);
}
int randint()
{
int r ;
srand(time(NULL));
r = rand() % 20;
return r ;
}
void finesheet(int veh,int type,int row,int col, struct tm *ct)
{
FILE *fptr;
fptr = fopen("finesheet.dat", "a");
if (fptr == NULL)
{
printf("File does not exists \n");
return 0 ;
}
fprintf(fptr,"\n");
fprintf(fptr,"%d ",veh);
fprintf(fptr,"%d ",type);
fprintf(fptr,"%d ",row);
fprintf(fptr,"%d ",col);
fprintf(fptr,"%d/%d/%d ",
ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900);
fprintf(fptr,"%d:%d:%d ",
ct->tm_hour, ct->tm_min, ct->tm_sec);
fprintf(fptr,"%d ",50);
fclose(fptr);
}
int historyrec(int val)
{
FILE *fptr;
if(val==1)
fptr = fopen("arival.dat", "r");
else if(val ==2)
fptr = fopen("depart.dat", "r");
else if(val==3)
fptr = fopen("finesheet.dat", "r");
else
{printf("invalid input");
return 0 ;}
if (fptr == NULL)
{
printf("File does not exists \n");
return 0 ;
}
char strg;
strg = getc(fptr);
while(!feof(fptr))
{
printf("%c",strg);
strg = getc(fptr);
}
return 0 ;
}
int backupw()
{
int r,c ;
FILE *fptr;
fptr = fopen("backupwr.dat", "w");
if (fptr == NULL)
{
printf("File does not exists \n");
return 0 ;
}
for(r=0;r<4;r++)
for(c=0;c<10;c++)
{
fprintf(fptr,"\n");
fprintf(fptr,"%d ",parkinfo[r][c]);
fprintf(fptr,"%d ",r);
fprintf(fptr,"%d ",c);
fprintf(fptr,"%d ",vehcount);
fprintf(fptr,"%d ",carcount);
fprintf(fptr,"%d ",scootercount);
}
fclose(fptr);
}
int backupr()
{
int r,c ;
int park[4][10];
FILE *fptr;
fptr = fopen("backupwr.dat", "r");
if (fptr == NULL)
{
printf("File does not exists \n");
return 0 ;
}
int rr ,cc ,veh,carrr,scoot,numb;
for(r=0;r<4;r++)
for(c=0;c<10;c++)
{
fscanf(fptr,"\n");
fscanf(fptr,"%d ",&park[r][c]);
numb = park[r][c];
fscanf(fptr,"%d ",&rr);
fscanf(fptr,"%d ",&cc);
fscanf(fptr,"%d ",&veh);
fscanf(fptr,"%d ",&carrr);
fscanf(fptr,"%d ",&scoot);
if(numb!=0)
{
if ( r == 0 || r == 1 )
car[r][c] = addonstart(1,numb,r,c);
else
scooter[r][c] = addonstart(2,numb,r,c);
}
}
fclose(fptr);
}
/* adds a data of vehicle */
struct vehicle * add ( int t, int num, int row, int col )
{
struct vehicle *v ;
int r ;
v = ( struct vehicle * ) malloc ( sizeof ( struct vehicle ) ) ;
v -> type = t ;
v -> row = row ;
v -> col = col ;
if ( t == CAR )
carcount++ ;
else
scootercount++ ;
vehcount++ ;
parkinfo[row][col] = num ;
v->at = datetime();
insertrecord(num,t,row,col,datetime());
backupw(t);
r = randint();
if(r < 5 )
{
printf("\a");
printf("\a");
finesheet(num,t,row,col,datetime());
}
return v ;
}
struct vehicle * addonstart ( int t, int num, int row, int col )
{
struct vehicle *v ;
int r ;
v = ( struct vehicle * ) malloc ( sizeof ( struct vehicle ) ) ;
v -> type = t ;
v -> row = row ;
v -> col = col ;
if ( t == CAR )
carcount++ ;
else
scootercount++ ;
vehcount++ ;
parkinfo[row][col] = num ;
//v->at = datetime();
//insertrecord(num,t,row,col,datetime());
// backupw(t);
// r = randint();
// if(r < 5 )
/*{
printf("\a");
printf("\a");
finesheet(num,t,row,col,datetime());
}*/
return v ;
}
/* deletes the data of the specified
car from the array, if found */
void del ( struct vehicle *v )
{
int c ;
//for ( c = v -> col ; c < 9 ; c++ )
// parkinfo[v -> row][c] = parkinfo[v -> row][c+1] ;
parkinfo[v -> row][v -> col] = 0 ;
if ( v -> type == CAR )
carcount-- ;
else
scootercount-- ;
vehcount-- ;
backupw();
}
/* get the row-col position for the vehicle to be parked */
void getfreerowcol ( int type, int *arr )
{
int r, c, fromrow = 0, torow = 2 ;
if ( type == SCOOTER )
{
fromrow += 2 ;
torow += 2 ;
}
for ( r = fromrow ; r < torow ; r++ )
{
for ( c = 0 ; c < 10 ; c++ )
{
if ( parkinfo[r][c] == 0 )
{
arr[0] = r ;
arr[1] = c ;
return ;
}
}
}
if ( r == 2 || r == 4 )
{
arr[0] = -1 ;
arr[1] = -1 ;
}
}
/* get the row-col position for the vehicle with specified number */
void getrcbyinfo ( int type, int num, int *arr )
{
int r, c, fromrow = 0, torow = 2 ;
if ( type == SCOOTER )
{
fromrow += 2 ;
torow += 2 ;
}
for ( r = fromrow ; r < torow ; r++ )
{
for ( c = 0 ; c < 10 ; c++ )
{
if ( parkinfo[r][c] == num )
{
arr[0] = r ;
arr[1] = c ;
return ;
}
}
}
if ( r == 2 || r == 4 )
{
arr[0] = -1 ;
arr[1] = -1 ;
}
}
/* displays list of vehicles parked */
void display( )
{
int r, c ;
printf ( "Cars ->\n" ) ;
for ( r = 0 ; r < 4 ; r++ )
{
if ( r == 2 )
printf ( "Scooters ->\n" ) ;
for ( c = 0 ; c < 10 ; c++ )
printf ( "%d\t", parkinfo[r][c] ) ;
printf ( "\n" ) ;
}
}
int main( )
{
int choice, type, number, row = 0, col = 0 ;
int i, tarr[2] ;
int finish = 1 ;
backupr();
system ( "cls" ) ;
/* displays menu and calls corresponding functions */
while ( finish )
{
system ( "cls" ) ;
printf ( "\nCar Parking\n" ) ;
printf ( "1. Arrival of a vehicle\n" ) ;
printf ( "2. Total no. of vehicles parked\n" ) ;
printf ( "3. Total no. of cars parked\n" ) ;
printf ( "4. Total no. of scooters parked\n" ) ;
printf ( "5. Display order in which vehicles are parked\n" ) ;
printf ( "6. Departure of vehicle\n" ) ;
printf ( "7. Chek History\n" ) ;
printf ( "8. Exit\n" ) ;
scanf ( "%d", &choice ) ;
switch ( choice )
{
case 1 :
system ( "cls" ) ;
printf ( "\nAdd: \n" ) ;
type = 0 ;
/* check for vehicle type */
while ( type != CAR && type != SCOOTER )
{
printf ( "Enter vehicle type (1 for Car / 2 for Scooter ): \n" ) ;
scanf ( "%d", &type ) ;
if ( type != CAR && type != SCOOTER )
printf ( "\nInvalid vehicle type.\n" ) ;
}
printf ( "Enter vehicle number: " ) ;
scanf ( "%d", &number ) ;
/* add cars' data */
if ( type == CAR || type == SCOOTER )
{
getfreerowcol ( type, tarr ) ;
if ( tarr[0] != -1 && tarr[1] != -1 )
{
row = tarr[0] ;
col = tarr[1] ;
if ( type == CAR )
car[row][col] = add ( type, number, row, col ) ;
else
scooter[row - 2][col] = add ( type, number, row, col ) ; ;
}
else
{
if ( type == CAR )
printf ( "\nNo parking slot free to park a car\n" ) ;
else
printf ( "\nNo parking slot free to park a scooter\n" ) ;
}
}
else
{
printf ( "Invalid type\n" ) ;
break ;
}
printf ( "\nPress any key to continue..." ) ;
getch( ) ;
break ;
case 2 :
system ( "cls" ) ;
printf ( "Total vehicles parked: %d\n", vehcount ) ;
printf ( "\nPress any key to continue..." ) ;
getch( ) ;
break ;
case 3 :
system ( "cls" ) ;
printf ( "Total cars parked: %d\n", carcount ) ;
printf ( "\nPress any key to continue..." ) ;
getch( ) ;
break ;
case 4 :
system ( "cls" ) ;
printf ( "Total scooters parked: %d\n", scootercount ) ;
printf ( "\nPress any key to continue..." ) ;
getch( ) ;
break ;
case 5 :
system ( "cls" ) ;
printf ( "Display\n" ) ;
display( ) ;
printf ( "\nPress any key to continue..." ) ;
getch( ) ;
break ;
case 6 : //problem area ................................................
system ( "cls" ) ;
printf ( "Departure\n" ) ;
type = 0 ;
/* check for vehicle type */
while ( type != CAR && type != SCOOTER )
{
printf ( "Enter vehicle type (1 for Car / 2 for Scooter ): \n" ) ;
scanf ( "%d", &type ) ;
if ( type != CAR && type != SCOOTER )
printf ( "\nInvalid vehicle type.\n" ) ;
}
printf ( "Enter number: " ) ;
scanf ( "%d", &number ) ;
if ( type == CAR || type == SCOOTER )
{
getrcbyinfo ( type, number, tarr ) ;
if ( tarr[0] != -1 && tarr[1] != -1 )
{
insertrecord2(number,datetime());
get_arrival_time(number);
col = tarr [1] ;
/* if the vehicle is car */
if ( type == CAR )
{
row = tarr [0] ;
del ( car [row][col] ) ;
for ( i = col ; i < 9 ; i++ )
{
car[row][i] = car[row][i + 1] ;
// changecol ( car[row][col] ) ;
}
free ( car[row][i] ) ;
car[row][i] = NULL ;
}
/* if a vehicle is scooter */
else
{
row = tarr[0] - 2 ;
if ( ! ( row < 0 ) )
{
del ( scooter[row][col] ) ;
for ( i = col ; i < 9 ; i++ )
{
scooter[row][i] = scooter[row][i + 1] ;
//changecol ( scooter[row][col] ) ;
}
scooter[row][i] = NULL ;
}
}
}
else
{
if ( type == CAR )
printf ( "\nInvalid car number, or a car with such number has not been parked here.\n" ) ;
else
printf ( "\nInvalid scooter number, or a scooter with such number has not been parked here.\n" ) ;
}
}
printf ( "\nPress any key to continue..." ) ;
getch( ) ;
break ;
case 8 :
system ( "cls" ) ;
for ( row = 0 ; row < 2 ; row++ )
{
for ( col = 0 ; col < 10 ; col++ )
{
if ( car[row][col] -> num != 0 )
free ( car[row][col] ) ;
if ( scooter[row][col] -> num != 0 )
free ( scooter[row+2][col] ) ;
}
}
finish = 0 ;
getch();
break ;
case 7 :
system ( "cls" ) ;
int opt ;
printf ( "RECORD TABLES\n" ) ;
printf ( "Press 1 for Arrival history\n" ) ;
printf ( "Press 2 for Departure history\n" ) ;
printf ( "Press 3 for Fine Sheet\n" ) ;
scanf ( "%d", &opt ) ;
historyrec(opt);
printf ( "\nPress any key to continue..." ) ;
getch( ) ;
break ;
}
}
return 0 ;
}