飞机订票系统
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <conio.h> 5 typedef struct flightnode{ 6 char flight_num[10]; //航班号 7 char start_time[10]; //起飞时间 8 char end_time[10]; //抵达时间 9 char start_place[20];//起飞城市 10 char end_place[20]; //抵达城市 11 int left; //空座数 12 float price; //票价 13 int isFull; //航班是否满仓 14 struct flightnode *next;//指向下一个结点 15 }flightnode;//航班结点 16 typedef struct passengernode{ 17 char name[20]; //姓名 18 char ID_num[20]; //证件号 19 char flight_num[10];//航班号 20 int order_num; //订单号 21 int ticket_num; //订票数量 22 struct passengernode *next;//指向下一个结点 23 }passengernode;//客户结点 24 typedef struct passengerList 25 { 26 passengernode *head; 27 passengernode *rear; 28 }passengerList; 29 void init_flight(flightnode *&h)//目的是要建立一个以h为头结点的空链表,录入航班信息和增加航班后将航班结点插入该链表 30 { 31 h=(flightnode *)malloc(sizeof(flightnode));//申请头结点的空间 32 if(h==NULL) exit(0); 33 h->next=NULL;//将头结点h的指针域置为空 34 } 35 void init_passengerList(passengerList *&pList) 36 { 37 pList=(passengerList *)malloc(sizeof(passengerList)); 38 pList->head=(passengernode *)malloc(sizeof(passengernode));//创建头结点 39 pList->rear=pList->head; 40 pList->rear->order_num=0;//订单号从0开始计数 41 pList->head->next=NULL; 42 } 43 void save_passenger(passengerList *PList) 44 { 45 FILE* fp = fopen("passenger.dat","wb"); 46 if(fp==NULL) 47 return; 48 passengernode *temp=PList->head->next; 49 while(temp!=NULL) 50 { 51 fwrite(temp,sizeof(passengernode),1,fp); 52 temp = temp->next; 53 }; 54 fclose(fp); 55 } 56 void load_passenger(passengerList *PList) 57 { 58 FILE* fp = fopen("passenger.dat","rb"); 59 if(fp==NULL) 60 return; 61 passengernode *q; 62 int n; 63 while(!feof(fp)) 64 { 65 q=(passengernode *)malloc(sizeof(passengernode)); 66 n =fread(q,sizeof(passengernode),1,fp); 67 if(n!=1) 68 break; 69 PList->rear->next=q; 70 PList->rear=q; 71 } 72 PList->rear->next=NULL; 73 fclose(fp); 74 } 75 void save_flight(flightnode *h) 76 { 77 FILE* fp = fopen("flightList.dat","wb"); 78 if(fp==NULL) 79 return; 80 flightnode *temp=h->next; 81 while(temp!=NULL) 82 { 83 fwrite(temp,sizeof(flightnode),1,fp); 84 temp = temp->next; 85 }; 86 fclose(fp); 87 } 88 void load_flight(flightnode *&h) 89 { 90 flightnode *pt=h; 91 FILE* fp = fopen("flightList.dat","rb"); 92 if(fp==NULL) 93 return; 94 flightnode *q; 95 int n; 96 while(!feof(fp)) 97 { 98 q=(flightnode *)malloc(sizeof(flightnode)); 99 n=fread(q,sizeof(flightnode),1,fp); 100 if(n!=1) 101 break; 102 pt->next=q; 103 pt=q; 104 } 105 pt->next=NULL; 106 fclose(fp); 107 } 108 109 int find_same_flight(flightnode *h,char *flight_num) 110 { 111 flightnode *t=h->next,*p=h->next; 112 int mark=0; 113 printf("%-8s%-12s%-12s%-10s%-10s%-8s%-10s%-8s\n","航班号","起飞城市","抵达城市","起飞时间","抵达时间","价格","是否满仓","空座数"); 114 while(t!=NULL && strcmp(t->flight_num,flight_num)!=0) t=t->next; 115 while(p!=NULL){ 116 if((strcmp(t->start_place,p->start_place)==0)&&(strcmp(t->end_place,p->end_place)== 0)&&(strcmp(t->flight_num,p->flight_num)!=0)) 117 { 118 printf("%-8s%-12s%-12s%-10s%-10s%-8.2f%-10d%-8d\n",p->flight_num, p->start_place,p->end_place,p->start_time,p->end_time,p->price,p->isFull,p->left); 119 mark=1; 120 } 121 p=p->next; 122 } 123 if(mark==0) 124 { 125 printf("\t\t抱歉,没有可选的航班!\n"); 126 return 0; 127 } 128 return 1; 129 } 130 131 void insert_flight(flightnode *&h,char* flight_num,char* start_place,char* end_place,char* start_time,char* end_time,int left,float price,float price_discount,int isFull) 132 //在录入航班情况或增加新的航班后,将新的航班结点插入到航线链表中 133 { 134 flightnode *q;//定义q为新增加的航班结点的指针的形参 135 flightnode *p=h; 136 q=(flightnode *)malloc(sizeof(flightnode));//为q结点申请空间 137 strcpy(q->flight_num,flight_num); 138 strcpy(q->start_place,start_place); 139 strcpy(q->end_place,end_place); 140 strcpy(q->start_time,start_time); 141 strcpy(q->end_time,end_time); 142 q->left=left; 143 q->price=price; 144 q->isFull=isFull; 145 //以上是用strcpy函数将新增加航班的各种信息复制到q结点中 146 q->next=p->next;//将q结点的指针域置为空 147 p->next=q; 148 p=p->next;//将指针后移 149 } 150 void insert_passengerList(flightnode *&h,passengerList *&PList,char *name,char *ID_num,char *flight_num,int ticket_num) 151 //客户订票后,将客户结点插入客户链表中,并修改相应的航班信息。 152 { 153 flightnode *p=h->next; 154 for(;p!=NULL;p=p->next) 155 if(strcmp(p->flight_num,flight_num)==0) 156 { 157 p->left=p->left-ticket_num; 158 if(p->left==0) 159 p->isFull=1; 160 } 161 passengernode *q=(passengernode *)malloc(sizeof(passengernode));//新建结点,存放新的客户订单信息 162 strcpy(q->name,name); 163 strcpy(q->ID_num,ID_num); 164 strcpy(q->flight_num,flight_num); 165 q->ticket_num=ticket_num; 166 q->next=NULL; 167 //将新订单连接到链表中 168 PList->rear->next=q; 169 q->order_num=PList->rear->order_num+1;//生成订单号 170 PList->rear=q; 171 } 172 173 void delete_flight(flightnode *&h,passengerList *&PList) 174 //目的要按照某个航班号删除航班结点 175 { 176 flightnode *p,*pr; 177 passengernode *q,*qr; 178 char flight_num[10]; 179 int mark=1; 180 qr=PList->head;//pr为顾客链表的头结点 181 q=qr->next;//用p作为中间指针对顾客链表中要删除的顾客结点进行查找 182 pr=h;//pr为航线链表的头结点 183 p=pr->next;//用p作为指针对航线链表中要删除的航班结点进行查找 184 printf("\t\t请输入你要删除的航班号:"); 185 scanf("%s",flight_num); 186 while(p!=NULL)//要删除的航班存在,下面进行删除操作 187 { 188 if(strcmp(flight_num,p->flight_num)==0) 189 { 190 pr->next=p->next;//移动指针找着要删除的航班结点 191 free(p);//删除操作 192 printf("\t\t删除%s航班成功!\n",flight_num); 193 mark=0; 194 p=NULL; 195 } 196 if(pr->next!=NULL) 197 { 198 pr=pr->next; 199 p=pr->next; 200 } 201 } 202 if(mark==1) 203 printf("\t\t无此航班,无法删除!\n"); 204 else 205 { 206 while(q!=NULL)//要删除的顾客存在,下面进行删除操作 207 { 208 if(strcmp(flight_num,q->flight_num)==0) 209 { 210 qr->next=q->next;//移动指针找着要删除的顾客结点 211 free(q); 212 q=NULL; 213 } 214 if(qr->next!=NULL) 215 { 216 qr=qr->next; 217 q=qr->next; 218 } 219 } 220 } 221 } 222 int delete_passenger(passengerList *&PList,flightnode *&h,char *name,char *ID_num) 223 //目的是按照要退票的顾客的姓名查找该顾客结点,进行删除操作 224 { 225 passengernode *p,*pr=PList->head;//pr指向顾客链表的头结点 226 p=pr->next; 227 while(p!=NULL) 228 { 229 if(strcmp(name,p->name)==0&&strcmp(ID_num,p->ID_num)==0) 230 { 231 for(flightnode *f=h->next;f!=NULL;f=f->next) 232 if(strcmp(p->flight_num,f->flight_num)==0) 233 { 234 f->left=f->left+p->ticket_num; 235 f->isFull=0; 236 break; 237 } 238 pr->next=p->next; 239 free(p); 240 printf("\t\t顾客%s,%s退票成功!\n",name,ID_num); 241 return 1; 242 } 243 pr=pr->next; 244 p=pr->next; 245 } 246 printf("\t\t无此顾客,无法退票!\n"); 247 return 0; 248 } 249 250 void add_flight(flightnode *&h)//录入航班信息后调用insert_flight函数增加航班 251 { 252 flightnode *p=h; 253 char flight_num[10],start_time[10],end_time[10],start_place[20],end_place[20]; 254 int left,isFull,mark=1; 255 float price,price_discount; 256 for(;p->next!=NULL;p=p->next){}//移动指针,找到最后一个结点 257 while(mark==1) 258 { 259 printf("\t\t请输入你要增加的航班号:"); 260 scanf("%s",flight_num); 261 printf("\t\t请输入出发地:"); 262 scanf("%s",start_place); 263 printf("\t\t请输入目的地:"); 264 scanf("%s",end_place); 265 printf("\t\t请输入起飞时间:"); 266 scanf("%s",start_time); 267 printf("\t\t请输入抵达时间:"); 268 scanf("%s",end_time); 269 printf("\t\t请输入票价:"); 270 scanf("%f",&price); 271 printf("\t\t请输入剩余座位数:"); 272 scanf("%d",&left); 273 printf("\t\t请输入是否满仓(0表示没有满仓1以满仓):"); 274 scanf("%d",&isFull); 275 insert_flight(p,flight_num,start_place,end_place,start_time,end_time,left,price, price_discount,isFull); 276 printf("\t\t增加航班%s成功!\n",flight_num); 277 printf("\t\t是否继续录入航班信息(1表示继续录入;0表示停止录入).\n"); 278 printf("\t\t请输入:"); 279 scanf("%d",&mark); 280 } 281 } 282 283 int flight_num_check(flightnode *h,char *flight_num)//用航班号进行查询 284 { 285 flightnode *p=h; 286 printf("%-8s%-12s%-12s%-10s%-10s%-8s%-10s%-8s\n","航班号","起飞城市","抵达城市","起飞时间","抵达时间","价格","是否满仓","空座数"); 287 for(;p!=NULL;p=p->next) 288 { 289 if(strcmp(p->flight_num,flight_num)==0) 290 { 291 printf("%-8s%-12s%-12s%-10s%-10s%-8.2f%-10d%-8d\n",p->flight_num, p->start_place,p->end_place,p->start_time,p->end_time,p->price,p->isFull,p->left); 292 return 1; 293 } 294 } 295 printf("\t\t抱歉,没有航班号为%s的航班信息!\n",flight_num); 296 return 0; 297 } 298 int place_check(flightnode *h,char *start_place,char *end_place)//用起飞抵达城市进行查询 299 { 300 flightnode *p=h; 301 int mark=0; 302 printf("%-8s%-12s%-12s%-10s%-10s%-8s%-10s%-8s\n","航班号","起飞城市","抵达城市","起飞时间","抵达时间","价格","是否满仓","空座数"); 303 for(;p!=NULL;p=p->next) 304 { 305 if(strcmp(p->start_place,start_place)==0&&strcmp(p->end_place,end_place)==0) 306 { 307 printf("%-8s%-12s%-12s%-10s%-10s%-8.2f%-10d%-8d\n",p->flight_num, p->start_place,p->end_place,p->start_time,p->end_time,p->price,p->isFull,p->left); 308 mark=1; 309 } 310 } 311 if(mark==0) 312 { 313 printf("\t\t抱歉,没有从%s到%s的航班信息!\n",start_place,end_place); 314 return 0; 315 } 316 return 1; 317 } 318 void check_all_flight(flightnode *h) 319 { 320 flightnode *p=h; 321 int m=0; 322 printf("%-8s%-12s%-12s%-10s%-10s%-8s%-10s%-8s\n","航班号","起飞城市","抵达城市","起飞时间","抵达时间","价格","是否满仓","空座数"); 323 for(;p!=NULL;p=p->next) 324 { 325 printf("%-8s%-12s%-12s%-10s%-10s%-8.2f%-10d%-8d\n",p->flight_num, p->start_place,p->end_place,p->start_time,p->end_time,p->price,p->isFull,p->left); 326 m=1; 327 } 328 if(m==0) 329 printf("\t\t航班信息为空!\n"); 330 } 331 void flight_check(flightnode *h)//选择使用哪一个函数进行查询 332 { 333 flightnode *p=h->next; 334 char flight_num[10],start_place[20],end_place[20]; 335 char a; 336 printf("\t\t请选择航班查询方式:\n"); 337 printf("\t\t1表示按航班号进行查询;\n"); 338 printf("\t\t2表示按起飞抵达城市进行查询;\n"); 339 printf("\t\t3表示浏览全部航班信息.\n\t\t请选择输入:"); 340 a=getch(); 341 printf("%c\n",a); 342 if(a=='1') 343 { 344 printf("\t\t请输入航班号:"); 345 scanf("%s",flight_num); 346 flight_num_check(p,flight_num); 347 } 348 else if(a=='2') 349 { 350 printf("\t\t请输入起飞城市:"); 351 scanf("%s",start_place); 352 printf("\t\t请输入抵达城市:"); 353 scanf("%s",end_place); 354 place_check(p,start_place,end_place); 355 } 356 else if(a=='3') 357 check_all_flight(p); 358 else 359 return; 360 } 361 362 int ID_name_check(passengerList *PList,char *name,char *ID_num) 363 { 364 passengernode *p=PList->head->next; 365 int mark=0; 366 printf("%-8s%-20s%-20s%-10s%-8s%\n","订单号","姓名","证件号","航班号","订票数量"); 367 for(;p!=NULL;p=p->next) 368 { 369 if(strcmp(p->ID_num,ID_num)==0&&strcmp(p->name,name)==0) 370 { 371 printf("%-8d%-20s%-20s%-10s%-8d%\n",p->order_num,p->name,p->ID_num, p->flight_num,p->ticket_num); 372 mark=1; 373 } 374 } 375 if(mark==0) 376 { 377 printf("\t\t抱歉,没有%s,%s的订单信息!\n",name,ID_num); 378 return 0; 379 } 380 return 1; 381 } 382 int order_num_check(passengerList *PList,int order_num) 383 { 384 passengernode *p=PList->head->next; 385 printf("%-8s%-20s%-20s%-10s%-8s%\n","订单号","姓名","证件号","航班号","订票数量"); 386 for(;p!=NULL;p=p->next) 387 { 388 if(p->order_num==order_num) 389 { 390 printf("%-8d%-20s%-20s%-10s%-8d%\n",p->order_num,p->name,p->ID_num, p->flight_num,p->ticket_num); 391 return 1; 392 } 393 } 394 printf("\t\t抱歉,没有订单号为%d的订单信息!\n",order_num); 395 return 0; 396 } 397 void check_all_passenger(passengerList *PList) 398 { 399 passengernode *p=PList->head->next; 400 int m=0; 401 printf("%-8s%-20s%-20s%-10s%-8s%\n","订单号","姓名","证件号","航班号","订票数量"); 402 for(;p!=NULL;p=p->next) 403 { 404 printf("%-8d%-20s%-20s%-10s%-8d%\n",p->order_num,p->name,p->ID_num, p->flight_num,p->ticket_num); 405 m=1; 406 } 407 if(m==0) 408 printf("\t\t订单信息为空!\n"); 409 } 410 void passenger_check(passengerList *PList) 411 { 412 char name[20],ID_num[15]; 413 int order_num; 414 char a; 415 printf("\t\t请选择订单查询方式:\n"); 416 printf("\t\t1表示按客户姓名和证件号进行查询;\n"); 417 printf("\t\t2表示按订单号进行查询;\n"); 418 printf("\t\t3表示查看全部订单信息.\n\t\t请选择输入:"); 419 a=getch(); 420 printf("%c\n",a); 421 if(a=='1') 422 { 423 printf("\t\t请输入客户姓名:"); 424 scanf("%s",name); 425 printf("\t\t请输入证件号:"); 426 scanf("%s",ID_num); 427 ID_name_check(PList,name,ID_num); 428 } 429 else if(a=='2') 430 { 431 printf("\t\t请输入订单号:"); 432 scanf("%d",&order_num); 433 order_num_check(PList,order_num); 434 } 435 else if(a=='3') 436 check_all_passenger(PList); 437 else 438 return; 439 } 440 441 void modify_flight(flightnode *&h,passengerList *&PList) 442 { 443 flightnode *p=h->next; 444 char flight_num[10],start_time[10],end_time[10]; 445 char a; 446 printf("\t\t************航线信息修改*************\n\n"); 447 printf("\t\t*************************************\n\n"); 448 printf("\t\t* 增加航班-------1 *\n\n"); 449 printf("\t\t* 删除航班-------2 *\n\n"); 450 printf("\t\t* 修改航班时间---3 *\n\n"); 451 printf("\t\t*************************************\n\n"); 452 printf("\t\t请选择:"); 453 a=getch(); 454 printf("%c\n",a); 455 if(a=='1') 456 add_flight(h); 457 else if(a=='2') 458 delete_flight(h,PList); 459 else if(a=='3') 460 { 461 printf("\t\t请输入要修改的航班的航班号:"); 462 scanf("%s",flight_num); 463 if(flight_num_check(p,flight_num)==1) 464 { 465 printf("\t\t请输入修改后的起飞时间:"); 466 scanf("%s",start_time); 467 printf("\t\t请输入修改后的抵达时间:"); 468 scanf("%s",end_time); 469 for(;p!=NULL;p=p->next) 470 if(strcmp(flight_num,p->flight_num)==0) 471 { 472 strcpy(p->start_time,start_time); 473 strcpy(p->end_time,end_time); 474 printf("\t\t航班%s时间修改成功!\n",flight_num); 475 } 476 } 477 } 478 else 479 return; 480 } 481 482 int book(flightnode *&h,passengerList *&PList) 483 { 484 char name[20]; //姓名 485 char ID_num[20]; //证件号 486 char flight_num[10]; //航班号 487 char start_place[20];//起飞城市 488 char end_place[20]; //抵达城市 489 int ticket_num; //订票数量 490 int k; 491 flightnode *p=h->next; 492 printf("\t\t请输入信息:\n"); 493 printf("\t\t请输入起飞城市:"); 494 scanf("%s",start_place); 495 printf("\t\t请输入抵达城市:"); 496 scanf("%s",end_place); 497 if(place_check(h,start_place,end_place)==1) 498 { 499 printf("\t\t航班号:"); 500 scanf("%s",flight_num); 501 while(flight_num==NULL){ 502 printf("航班号不能为空!\n"); 503 printf("\t\t航班号:"); 504 scanf("%s",flight_num); 505 } 506 while(p!=NULL){ 507 if(strcmp(p->flight_num,flight_num)==0) 508 { 509 printf("\t\t姓名:"); 510 scanf("%s",name); 511 printf("\t\t证件号码:"); 512 scanf("%s",ID_num); 513 printf("\t\t订票数量:"); 514 scanf("%d",&ticket_num); 515 while(name==NULL){ 516 printf("客户姓名不能为空!\n"); 517 printf("\t\t姓名:"); 518 scanf("%s",name); 519 } 520 while(ID_num==NULL){ 521 printf("客户证件号码不能为空!\n"); 522 printf("\t\t证件号码:"); 523 scanf("%s",ID_num); 524 } 525 while(ticket_num==0){ 526 printf("客户订票数量不能为空!\n"); 527 printf("\t\t订票数量:"); 528 scanf("%d",&ticket_num); 529 } 530 if(p->left>0&&p->left>=ticket_num) 531 { 532 insert_passengerList(h,PList,name,ID_num,flight_num,ticket_num); 533 534 getch(); 535 printf("\t\t恭喜您,订票成功!\n"); 536 return 1; 537 } 538 else 539 { 540 printf("\t\t***很遗憾,该航班已满!***\n"); 541 printf("\t\t***如选择其他航班请输入-----1\n\t\t***不选则输入-----0***\n"); 542 printf("\t\t输入数字进行选择:"); 543 scanf("%d",&k); 544 if(k==1) 545 { 546 printf("\t\t此航线上的其他航班有:\n"); 547 if(find_same_flight(h,flight_num)==1) 548 { 549 printf("\t\t请输入您选的航班:"); 550 scanf("%s",flight_num); 551 insert_passengerList(h,PList,name,ID_num,flight_num, ticket_num); 552 printf("\t\t恭喜您,订票成功!\n"); 553 return 1; 554 } 555 } 556 } 557 } 558 else 559 p=p->next; 560 } 561 if(p==NULL) 562 printf("\t\t对不起,您输入的航班不存在!\n"); 563 } 564 return 0; 565 } 566 567 void cancel(passengerList *&PList,flightnode *&h) 568 { 569 char name[20],ID_num[20]; 570 printf("\t\t请输入你的姓名:"); 571 scanf("%s",name); 572 printf("\t\t请输入你的证件号:"); 573 scanf("%s",ID_num); 574 delete_passenger(PList,h,name,ID_num); 575 } 576 577 int main() 578 { 579 char choice; 580 int t=1; 581 flightnode *flight; 582 passengerList *PList; 583 init_flight(flight);//初始化航班链表 584 init_passengerList(PList); 585 load_flight(flight); 586 load_passenger(PList); 587 while(t==1) 588 { 589 printf("\t\t************飞机订票系统菜单*************\n"); 590 printf("\t\t*****************************************\n"); 591 printf("\t\t* 录入信息----------1 *\n\n"); 592 printf("\t\t* 订 票----------2 *\n\n"); 593 printf("\t\t* 退 票----------3 *\n\n"); 594 printf("\t\t* 查询航班----------4 *\n\n"); 595 printf("\t\t* 查询订单----------5 *\n\n"); 596 printf("\t\t* 修改航线----------6 *\n\n"); 597 printf("\t\t* 保存退出----------0 *\n\n"); 598 printf("\t\t*****************************************\n\n"); 599 printf("\t\t请选择服务:"); 600 choice=getch(); 601 printf("%c\n",choice); 602 system("cls"); 603 if(choice=='1'){ 604 add_flight(flight); 605 getch(); 606 system("cls"); 607 }else if(choice=='2'){ 608 book(flight,PList); 609 getch(); 610 system("cls"); 611 }else if(choice=='3'){ 612 cancel(PList,flight); 613 getch(); 614 system("cls"); 615 }else if(choice=='4'){ 616 flight_check(flight); 617 getch(); 618 system("cls"); 619 }else if(choice=='5'){ 620 passenger_check(PList); 621 getch(); 622 system("cls"); 623 }else if(choice=='6'){ 624 modify_flight(flight,PList); 625 getch(); 626 system("cls"); 627 }else if(choice=='0'){ 628 printf("\t\t再见!\n"); 629 t=0; 630 } 631 } 632 save_flight(flight); 633 save_passenger(PList); 634 return 0; 635 }