-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlyres.c
710 lines (643 loc) · 17.4 KB
/
rlyres.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
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
#include<stdio.h>
#include "conio2.h"
#include "rlyres.h"
#include<ctype.h>
#include<string.h>
int enterchoice() //it return selected integer.
{
int i;int choice;
textcolor(YELLOW);
gotoxy(45,2);
printf("RAILWAY RESERVATION SYSTEM");
printf("\n");
for(i=0;i<=59;i++)
{
printf("_ ");
}
printf("\nSelect an option\n");
printf("\n1- Veiw train\n2- Book ticket\n3- View ticket\n4- Search ticket no.\n5- View all bookings\n6- View train booking\n7- Cancel ticket\n8- Cancel train\n9- Quit");
printf("\n\nEnter choice:- ");//printf(" ");printf("\b\b\b\b\b\b");
scanf("%d",&choice);
return choice;
}
void add_train() //to add trains in train.dat
{
FILE *fp;
fp=fopen("D:\\c\\train.dat","rb");
if(fp==NULL)
{
Train alltrains[7]=
{
{"123","BPL","GWL",2100,1500},
{"546","BPL","DEL",3500,2240},
{"345","HBJ","AGR",1560,1153},
{"896","HBJ","MUM",4500,3360},
{"786","JHS","BPL",1200,1000},
{"555","PHB","BSB",1600,1400},
{"167","GWL","BSB",2800,2200}
};
fp=fopen("D:\\c\\train.dat","wb");
fwrite(alltrains,7*sizeof(Train),1,fp);
printf("File saved successfully\n");
fclose(fp);
}
else
{
printf("File Already present\n");
fclose(fp);
}
}
void view_train() // 1 option to view train
{
int i;textcolor(YELLOW);
gotoxy(45,2);
printf("RAILWAY RESERVATION SYSTEM");printf("\n");
for(i=0;i<=59;i++)
{
printf("__");
}
gotoxy(2,6);
printf("TRAIN NO");
gotoxy(27,6);
printf("FROM");
gotoxy(57,6);
printf("TO");
gotoxy(82,6);
printf("FIRST AC FAIR");
gotoxy(107,6);
printf("SECOND AC FAIR\n");
printf("\n\n");
gotoxy(1,7);
for(i=0;i<=59;i++)
{
printf("* ");
}
Train t;
FILE *fp;
int x=8;
fp=fopen("D:\\c\\train.dat","rb");
while(fread(&t,sizeof(Train),1,fp)==1)
{
gotoxy(2,x);
printf("%s",t.train_no);
gotoxy(28,x);
printf("%s",t.from);
gotoxy(57,x);
printf("%s",t.to);
gotoxy(85,x);
printf("%d",t.fac_fare);
gotoxy(115,x);
printf("%d",t.sac_fare);
x+=2;
}
printf("\n");
gotoxy(1,30);
textcolor(WHITE);
printf("PRESS ANY KEY TO RETURN TO MAIN SCREEN");
getch();clrscr();
}
int*get_ticket_no(char*mob_no) //take mobile no.and show the respective ticket
{
FILE *fp;
int count=0;
int flag=0;
Passenger psn;
fp=fopen("D:\\c\\ticketbookingdetails.dat","rb");
gotoxy(1,2);
while(fread(&psn,sizeof(psn),1,fp)==1)
{
if(strcmp(psn.mobile_no,mob_no)==0)
{
count++;
flag=1;
printf("\nTicket No. %d is : %d ",count,psn.ticketno);
}
}
if(flag==0)
{
gotoxy(1,30);
textcolor(LIGHTRED);
printf("No record found for this mobile no.");
textcolor(YELLOW);
getch();clrscr();
return;
}
gotoxy(1,2);
printf("You have total %d bookings for the mobile no. %s",count,mob_no);gotoxy(1,29);textcolor(WHITE);
printf("PRESS ANY KEY TO ENTER ANOTHER MOBILE NO.");
//gotoxy(1,count+3);
getch();clrscr();
fclose(fp);
}
void view_booking(char *rr) //train no.
{
int flag=0;
FILE*fp;
fp=fopen("D:\\c\\ticketbookingdetails.dat","rb");
if(fp==NULL)
{
gotoxy(1,30);
textcolor(LIGHTGREEN);
printf("No Bookings done in this train yet");getch();
textcolor(YELLOW);clrscr();
fclose(fp);
return;
}
Passenger paa;
while(fread(&paa,sizeof(paa),1,fp)==1)
{
if(strcmp(paa.train_no,rr)==0 )
{ flag++;
textcolor(YELLOW);
printf("\nTICKET NO.: %d\n",paa.ticketno);
printf("NAME: %s\n",paa.p_name);
printf("GENDER: %c\n",paa.gender);
printf("TRAIN NO.: %s\n",paa.train_no);
printf("CLASS: %c\n",paa.p_class);
printf("ADDRESS: %s\n",paa.address);
printf("AGE: %d\n",paa.age);
printf("MOBILE NO.: %s\n\n",paa.mobile_no);
}
}
textcolor(WHITE);
printf("ENTER ANY KEY FOR ANOTHER TRAIN NO.");
if(flag==0)
{
gotoxy(1,3);printf("\t\t\t\t\t\t"); // this line is for removing above printf statment............
gotoxy(1,29);
textcolor(LIGHTRED);
printf("TRAIN HAVE NOT BOOKING YET!\n");
textcolor(WHITE);
printf("ENTER ANY KEY FOR ANOTHER TRAIN NO."); textcolor(YELLOW);
}
fclose(fp);
getch();clrscr();return;
}
void view_all_booking() //5
{
clrscr();
Passenger paa;
FILE*fp;int j=8;
fp=fopen("D:\\c\\ticketbookingdetails.dat","rb");
if(fread(&paa,sizeof(paa),1,fp)==0) //if open
{gotoxy(1,28);printf("NO BOOKING DONE YET!!");return;}
int i;textcolor(YELLOW);
gotoxy(45,2);
printf("RAILWAY RESERVATION SYSTEM");printf("\n");
for(i=0;i<=59;i++)
{
printf("__");
}
gotoxy(1,6);
printf("Ticket no.");
gotoxy(18,6);
printf("NAME");
gotoxy(40,6);
printf("ADDRESS");
gotoxy(60,6);
printf("CLASS");gotoxy(80,6);
printf("TRAIN NO.");
gotoxy(107,6);
printf("mobile no\n");
printf("\n\n");
gotoxy(1,7);
for(i=0;i<=59;i++)
{
printf("* ");
}
if(fp==NULL)
{
gotoxy(1,29);
textcolor(WHITE);
printf("!!NO RESERVATION YET!!\n");
printf("ENTER ANY KEY TO RETURN");fclose(fp);getch();clrscr();return;
}
textcolor(YELLOW);
gotoxy(1,j);
printf("%d",paa.ticketno);
gotoxy(18,j);
printf("%s",paa.p_name);
gotoxy(33,j);
printf("%s",paa.address);
gotoxy(60,j);
printf("%c",paa.p_class);
gotoxy(80,j);
printf("%s",paa.train_no);
gotoxy(107,j);
printf("%s",paa.mobile_no);
printf("\n\n");j=j+2;
while(fread(&paa,sizeof(paa),1,fp)==1)
{
textcolor(YELLOW);
gotoxy(1,j);
printf("%d",paa.ticketno);
gotoxy(18,j);
printf("%s",paa.p_name);
gotoxy(33,j);
printf("%s",paa.address);
gotoxy(60,j);
printf("%c",paa.p_class);
gotoxy(80,j);
printf("%s",paa.train_no);
gotoxy(107,j);
printf("%s",paa.mobile_no);
printf("\n\n");
j=j+2;
}
fclose(fp);
return;
}
int book_ticket(Passenger pp) //2
{
int v;int rohanjain;
v=get_booked_ticket_count(pp.train_no,pp.p_class);
FILE*fp=fopen("D:\\c\\ticketbookingdetails.dat","ab+");
if(v==-1)
{
pp.ticketno=1;
fwrite(&pp,sizeof(Passenger),1,fp);
fclose(fp);
return 1;
}
else if(v==0)
{
fclose(fp);
return 0;
}
else
{
int ticket;
ticket=last_ticket_no();
pp.ticketno=ticket+1;
fwrite(&pp,sizeof(Passenger),1,fp);
fclose(fp);
return ticket+1;
}
}
int last_ticket_no() //2
{
Passenger psn;
FILE *fp;
fp=fopen("D:\\c\\ticketbookingdetails.dat","rb");
if(fp==NULL)
{
return 0;
}
fseek(fp,-1*sizeof(Passenger),SEEK_END);
fread(&psn,sizeof(Passenger),1,fp);
int last_ticket;
last_ticket=psn.ticketno;
fclose(fp);
return last_ticket;
}
int get_booked_ticket_count(char *trainnoo,char classs) //2
{
int count=0;
FILE*fp; Passenger passen;
fp=fopen("D:\\c\\ticketbookingdetails.dat","rb");
if(fp==NULL || fread(&passen,sizeof(passen),1,fp)==0)
{
fclose(fp);
return -1;
}
while(fread(&passen,sizeof(passen),1,fp)==1)
{
if(strcmp(passen.train_no,trainnoo)==0 && classs==passen.p_class)
{
count++;
}
}
if(count>=2)
{
fclose(fp);
return 0;
}
fclose(fp);
return 1;
}
int check_train_no(char*trainno) //CAN VERIFY THE TRAIN NO.
{
FILE*fp;
fp=fopen("D:\\c\\train.dat","rb");
Train tr;
while(fread(&tr,sizeof(tr),1,fp)==1)
{
if(strcmp(tr.train_no,trainno)==0)
{
fclose(fp);return 1;
}
}
fclose(fp);
return 0;
}
void view_ticket(int ss) //3 option to view
{
int w;Passenger paa;
FILE*fp;
fp=fopen("D:\\c\\ticketbookingdetails.dat","rb");
if(fp==NULL)
{
gotoxy(1,29);
textcolor(WHITE);
printf("!!NO RESERVATION YET!!\n");
printf("ENTER ANY KEY TO RETURN");getch();
clrscr();return;
}
while(fread(&paa,sizeof(paa),1,fp)==1)
{
if(paa.ticketno==ss)
{
gotoxy(1,4);
textcolor(YELLOW);
printf("NAME: %s\n",paa.p_name);
printf("GENDER: %c\n",paa.gender);
printf("TRAIN NO.: %s\n",paa.train_no);
printf("CLASS: %c\n",paa.p_class);
printf("ADDRESS: %s\n",paa.address) ;
printf("AGE: %d\n",paa.age);
printf("MOBILE NO.: %s\n",paa.mobile_no);
printf("TICKET NO.: %d\n",paa.ticketno);
FILE*fpp;
fpp=fopen("D:\\c\\train.dat","rb");
Train trr;
while(fread(&trr,sizeof(trr),1,fpp)==1)
{
if(strcmp(trr.train_no,paa.train_no)==0&&paa.p_class=='f')
{
printf("FROM: %s\n",trr.from);
printf("TO: %s\n",trr.to);
printf("FARE: %d\n",trr.fac_fare);
fclose(fpp);
}
else if(strcmp(trr.train_no,paa.train_no)==0&&paa.p_class=='s')
{
printf("FROM: %s\n",trr.from);
printf("TO: %s\n",trr.to);
printf("FARE: %d\n",trr.sac_fare);
fclose(fpp);
}
}
fclose(fpp);
gotoxy(1,30);
textcolor(WHITE);
printf("PRESS ENTER TO VIEW THE DETAILS OF ANOTHER TICKET");getch();clrscr();
fclose(fp);
return;
}
}
gotoxy(1,29);
textcolor(WHITE);
printf("!!NO RESERVATION YET!!\n");
printf("ENTER ANY KEY TO RETURN");getch(); gotoxy(1,29);printf("\t\t\t\t\t\t\n");
printf("\t\t\t\t");gotoxy(37,2);printf("\t\t\t\t\t");gotoxy(37,2);
d:fclose(fp);
}
Passenger* get_passenger_details()
{
int i;static Passenger psn;
gotoxy(100,2);
textcolor(WHITE);
printf("PRESS 0 TO EXIT");
gotoxy(1,2);
textcolor(YELLOW);
printf("ENTER PASSENGER NAME:");r:
fflush(stdin);
fgets(psn.p_name,30,stdin);
char*p=strchr(psn.p_name,'\n');
*p='\0';
if(strcmp(psn.p_name,"0")==0)
{
gotoxy(1,30);
textcolor(GREEN);
printf("RESERVATION CANCELLED!");
getch();
textcolor(YELLOW);clrscr();
return NULL;
}
int a=0;
while(psn.p_name[a]!='\0')
{
if(1<=psn.p_name[a]&&psn.p_name[a]<=31||psn.p_name[a]>=33&&psn.p_name[a]<=57)
{ gotoxy(1,30);printf("please enter character only");getch();gotoxy(1,30);printf("\t\t\t\t");
gotoxy(22,2);printf("\t\t\t"); gotoxy(22,2);
goto r;
}
a++;
}
gotoxy(1,4);
printf("ENTER GENDER:");
a:
fflush(stdin);
scanf("%c",&psn.gender);
if(psn.gender=='0')
{
gotoxy(1,30);
textcolor(GREEN);
printf("RESERVATION CANCELLED!");
getch();
textcolor(YELLOW);clrscr();
return NULL;
}
if(psn.gender!='m' && psn.gender!='M' && psn.gender!='f' && psn.gender!='F') //if character is not equals to any of them.....
{
gotoxy(1,30);printf("please enter valid character only!");getch();gotoxy(1,30);
printf("\t\t\t\t\t");
gotoxy(14,4);printf("\t\t\t"); gotoxy(14,4);
goto a;
}
gotoxy(1,6);
textcolor(YELLOW);
printf("ENTER ADDRESS OF PASSENGER:");m:
fflush(stdin);
fgets(psn.address,30,stdin);
char*q=strchr(psn.address,'\n');
*q='\0';
if(strcmp(psn.address,"0")==0)
{
gotoxy(1,30);
textcolor(GREEN);
printf("RESERVATION CANCELLED!");
getch();
textcolor(YELLOW);clrscr();
return NULL;
}
if(psn.address[0]<=48) // \n\n----\0\n by strchr()
{
gotoxy(1,30);printf("please enter valid address");getch();gotoxy(1,30);printf("\t\t\t\t");
gotoxy(28,6);printf("\t\t\t"); gotoxy(28,6);
goto m;
}
int e;char rohan[30];
gotoxy(1,8);
textcolor(YELLOW);
printf("ENTER AGE OF PASSENGER:");y:
fflush(stdin);
fgets(rohan,30,stdin);
char*s=strchr(rohan,'\n');
*s='\0';
if(strcmp(rohan,"0")==0)
{
gotoxy(1,30);
textcolor(GREEN);
printf("RESERVATION CANCELLED!");
getch();
textcolor(YELLOW);clrscr();
return NULL;
}
if(rohan[0]>=65||rohan[1]>=65||rohan[2]!='\0')
{
gotoxy(1,30);printf("please enter valid integer only");getch();gotoxy(1,30);printf("\t\t\t\t");
gotoxy(24,8);printf("\t\t\t"); gotoxy(24,8);
goto y;
}
psn.age=atoi(rohan);
gotoxy(1,10);
textcolor(YELLOW);
printf("ENTER TRIAN NO.:");z:
fflush(stdin);
fgets(psn.train_no,20,stdin);
char*r=strchr(psn.train_no,'\n');
*r='\0';
if(strcmp(psn.train_no,"0")==0)
{
gotoxy(1,30);
textcolor(GREEN);
printf("RESERVATION CANCELLED!");
getch();
textcolor(YELLOW);clrscr();
return NULL;
}
if(!check_train_no(psn.train_no))
{
gotoxy(1,30);printf("please enter valid train no only");getch();gotoxy(1,30);printf("\t\t\t\t");
gotoxy(17,10);printf("\t\t\t"); gotoxy(17,10);
goto z;
}
gotoxy(1,12);
printf("ENTER CLASS:");aa:fflush(stdin);
scanf("%c",&psn.p_class);
if(psn.p_class=='0')
{
gotoxy(1,30);
textcolor(GREEN);
printf("RESERVATION CANCELLED!");
getch();
textcolor(YELLOW);clrscr();
return NULL;
}
if(psn.p_class!='s' && psn.p_class!='f')
{
gotoxy(1,30);printf("please enter valid class f and s only!");getch();gotoxy(1,30);
printf("\t\t\t\t\t");
gotoxy(13,12);printf("\t\t\t"); gotoxy(13,12);
goto aa;
}
gotoxy(1,14);
textcolor(YELLOW);
printf("Enter mobile no:");t:
fflush(stdin);
fgets(psn.mobile_no,11,stdin);
char*ss=strchr(psn.mobile_no,'\n');
if(ss!=NULL)
*ss='\0';
if(strcmp(psn.mobile_no,"0")==0)
{
gotoxy(1,30);
textcolor(GREEN);
printf("RESERVATION CANCELLED!");
getch();
textcolor(YELLOW);clrscr();
return NULL;
}
if(!check_mobile_no(psn.mobile_no))
{
gotoxy(1,30);printf("please enter valid mobile no only");getch();gotoxy(1,30);printf("\t\t\t\t\t");
gotoxy(17,14);printf("\t\t\t"); gotoxy(17,14);
goto t;
}
return &psn;
}
int check_mobile_no(char*ro)
{
if(strlen(ro)!=10)
return 0;
int i;
for(i=0;i<10;i++)
{
if(!isdigit(ro[i]))
return 0;
}
return 1;
}
int cancel_ticket(int ticket)
{
FILE *fp,*t_file;
Passenger psn;
int flag=0;
fp=fopen("D:\\c\\ticketbookingdetails.dat","rb");
if(fp==NULL)
{
return 0;
}
t_file=fopen("D:\\c\\temp.dat","wb");
while(fread(&psn,sizeof(Passenger),1,fp)==1)
{
if(ticket!=psn.ticketno)
{
fwrite(&psn,sizeof(Passenger),1,t_file);
}
else
{
flag=1;
}
}
fclose(fp);
fclose(t_file);
if(flag==1)
{
remove("D:\\c\\ticketbookingdetails.dat"); //present in stdio.h
rename("D:\\c\\temp.dat","D:\\c\\ticketbookingdetails.dat");
return 1;
}
else
{
remove("D:\\c\\temp.dat");
return -1;
}
}
int cancel_train(char *train_no)
{
FILE *fp,*t_file;
Passenger psn;
int flag=0;
fp=fopen("D:\\c\\ticketbookingdetails.dat","rb");
if(fp==NULL)
{
return 0;
}
t_file=fopen("D:\\c\\temp.dat","wb");
while(fread(&psn,sizeof(Passenger),1,fp)==1)
{
if(strcmp(psn.train_no,train_no)!=0)
{
fwrite(&psn,sizeof(Passenger),1,t_file);
}
else
{
flag=1;
}
}
fclose(fp);
fclose(t_file);
if(flag==1)
{
remove("D:\\c\\ticketbookingdetails.dat");
rename("D:\\c\\temp.dat","D:\\c\\ticketbookingdetails.dat");
return 1;
}
else
{
remove("D:\\c\\temp.dat");
return 0;
}
}