C语言飞机订票系统(数组版)

1建立航班信息和乘客信息,每次航班的信息包括:航班号、起飞城市、到达城市、起飞时间、到达时间、机票价格、机票折扣、机票剩余数量;

每位乘客的信息包括:姓名、身份证号、性别、购买机票数、乘坐航班航班号;

2添加模块:可以连续添加多条航班信息;

3查找模块:乘客可输入乘坐航班的航班号或目的地来查找已订航班的详细信息;

4订票模块:乘客可输入目的地来查看已有航班信息,然后可以选择自己想要预定的航班,然后输入个人信息来预定航班;

5修改模块:可通过输入航班的航班号来修改该航班的所有信息;

(6)退票模块:乘客可通过输入个人信息来查看已预订航班,然后可决定是否退票;

(7)显示模块:可一键显示所有航班信息;

(8)推荐模块:乘客可通过输入起飞城市和期望出发时间来获取系统推荐的航班及其详细信息;

(9)保存模块:只要输入信息就会即时自动将信息(航班信息或乘客信息)保存至相应文件夹;

(10)时间模块:实时查询当前时间功能。

一、主模块

#include <stdio.h>
#include <stdlib.h>
#include "airplane.h"
int main(int argc, char *argv[]) {
    int item;
    while(1){
        Menu();
        printf("Please input your choice(0-8): \n");
        scanf("%d",&item);
        switch(item){
            case 1:Insert();
            break;
            case 2:Search();
            break;
            case 3:Book();
            break;
            
            case 4:Modify();
            break;
            case 5:ShowAll();
            break;
            
            case 6: Recommend();
            break;
            
            case 7:Refund();
            break;
            
            case 8: CurrentTime();
            break;
            
            case 0:
            exit(0);
        }
    printf("Please input any key to continue\n");
    system("pause");
    system("cls");
    }
    return 0;
}

二、结构类型定义

#ifndef AIRPLANE_H
#define AIRPLANE_H
struct airplane{
    int fnum;
    char scity[20];
    char acity[20];
    char stime[20];
    char atime[20];
    float price;
    float discount;
    float tnum;
};
struct people{
    char name[20];
    int pid;
    char sex[10];
    int btnum;
    int fnum1;
};
void Menu();
void Insert();
void Search();
void Book();
void Modify();
void ShowAll();
void Recommend();
void Refund();
void CurrentTime();
#endif
三、菜单模块

#include<stdio.h>
void Menu(){
    int i;
    printf("\n\t\tWelcome to the Flight Booking System\n\n");
    printf("********************************************************************\n");
    printf("1.Insert flights\t\t5.Show flights\t\t\n");
    printf("2.Search flights   \t\t6.Recommend flights\t\n");
    printf("---------------\t\t---------------\t\t----------------\n");
    printf("3.Book tickets  \t\t7.Refund tickets  \t0.Exit\n");
    printf("4.Modify flight data\t\t8.Show current time\n");
    printf("********************************************************************\n\n");
   
    
}

四、添加模块

#include<stdio.h>
#include<stdlib.h>
#include"airplane.h"
void Insert(){
    int n,i;
    FILE *fp;
    if((fp=fopen("airplane.dat","ab+"))==NULL){
        printf("Can not open file\n");
        exit(0);
    }
    struct airplane air;
    printf("Please input the number of airplane: ");
    scanf("%d",&n);
    printf("Please input the information as instructed:\n");
    for(i=0;i<n;i++){
        printf("FlightNum(-1-end)||StartCity||ArrivalCity||StartTime(00:00)||ArrivalTime(00:00)||Price||Discount(0-9)||Ticketnum\n");
         scanf("%d     %s     %s     %s     %s     %f     %f     %f",&air.fnum,air.scity,air.acity,air.stime,air.atime,&air.price,&air.discount,&air.tnum);
        fwrite(&air,sizeof(struct airplane),1,fp);
    }
    fclose(fp);
    
    
    
}

五、查询模块

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"airplane.h"
void Search()
{    FILE *fp;
    if((fp=fopen("airplane.dat","ab+"))==NULL){
        printf("Can not open file\n");
        exit(0);
    }
      long begin,end,lcount;
      fseek(fp,0L,SEEK_SET);
      begin=ftell(fp);
      fseek(fp,0L,SEEK_END);
      end=ftell(fp);
      lcount=(end-begin)/sizeof(struct airplane);//所有文件记录数
      rewind(fp);
     int choice;
     int Fnum;//要输入的航班号
     char Pacity[20];//目的地
     struct airplane air[lcount],*p=air;
     fread(p,sizeof(struct airplane),lcount,fp);
     printf("Please input the search method you want:1.flight number  2.Arrival city\n");
     scanf("%d",&choice);
     if(choice==1){//按照航班号
          printf("Please input your flight number:\n");
          scanf("%d",&Fnum);
          printf("FlightNum || StartCity ||   ArrivalCity ||   StartTime ||   ArrivalTime ||   Price ||   Discount ||   Ticketnum\n");
          printf("----------||-----------||---------------||-------------||---------------||---------||------------||------------||\n");
          while(p!=NULL){
              if(p->fnum==Fnum){
                  printf("%d         %s        %s         %s         %s           %.2f          %.0f          %.0f\n",p->fnum,p->scity,p->acity,p->stime,p->atime,p->price,p->discount,p->tnum);
                  break;
            }else{
                p++;
            }
            if(p==NULL){
                printf("The flight number is not found\n");
                return;
            }
          }
     }else if(choice==2){//按照到达城市
        printf("Please input your destination:\n");
        scanf("%s",Pacity);     
          printf("FlightNum||StartCity||ArrivalCity||StartTime(00:00)||ArrivalTime(00:00)||Price||Discount(0-9)|| Ticketnum\n");
          printf("---------||---------||-----------||----------------||------------------||-----||-------------||----------||\n");
          int i;
          for(i=0;i<lcount;i++){
              if(strcmp(air[i].acity,Pacity)==0){
                  printf("%d     %s       %s        %s              %s             %.2f           %.0f           %.0f\n",air[i].fnum,air[i].scity,air[i].acity,air[i].stime,air[i].atime,air[i].price,air[i].discount,air[i].tnum);
              }
           }
        }else{
            printf("Please input the correct selection!\n");
        }
     
     
 
}

六、预订模块

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"airplane.h"
void Book(){
    FILE *fp1;
     if((fp1=fopen("people.dat","ab+"))==NULL){
         printf("can not open file\n");
         exit(0);
     }
     FILE *fp;
     if((fp=fopen("airplane.dat","ab+"))==NULL){
         printf("can not open file\n");
         exit(0);
     }
      long begin,end,lcount;
      fseek(fp,0L,SEEK_SET);
      begin=ftell(fp);
      fseek(fp,0L,SEEK_END);
      end=ftell(fp);
      lcount=(end-begin)/sizeof(struct airplane);//所有文件记录数
      rewind(fp);
     struct airplane air[lcount],*p=air;
     fread(p,sizeof(struct airplane),lcount,fp);
     char Pacity[20];
     printf("Please input your destination:\n");
        scanf("%s",Pacity);    
          printf("There are the flights you can choose:\n"); 
          printf("FlightNum||StartCity||ArrivalCity||StartTime(00:00)||ArrivalTime(00:00)||Price||Discount(0-9)||Ticketnum\n");
          printf("---------||---------||-----------||----------------||------------------||-----||-------------||---------||\n");
          int i,c;
          for(i=0;i<lcount;i++){
              if(strcmp(air[i].acity,Pacity)==0){
                  printf("%d      %s       %s         %s          %s             %.2f        %.0f         %.0f\n",air[i].fnum,air[i].scity,air[i].acity,air[i].stime,air[i].atime,air[i].price,air[i].discount,air[i].tnum);
              }
           }
        int Fnum;
        int choice;
        int m,j;
        printf("Please input the flight number you selsected:\n");//输入选择的航班号
        scanf("%d",&Fnum);
        while(p!=NULL){
              if(p->fnum==Fnum){
                  printf("%d              %s         %s             %s           %s          %.2f       %.0f         %.0f\n",p->fnum,p->scity,p->acity,p->stime,p->atime,p->price,p->discount,p->tnum);
                  printf("Is this your choice?(1.YES/Other Figures.NO)\n");
                  scanf("%d",&choice);
                  if(choice==1){
                      printf("Please input the number of tickets you want book:\n");//输入订票数
                      scanf("%d",&m);
                      if(m>p->tnum){
                          printf("Insufficient number of tickets,Please operate again\n");
                          break;
                      }
                      struct people pe;
                      printf("Please enter the information as instructed:\n");
                      for(j=0;j<m;j++){
                          printf("IDnumber     ||     Sex(M/F)     ||     NAME     ||     Fnumber(-1-end)     ||     BookTicketNumber\n");
                           scanf("%d                     %s                %s                 %d                           %d",&pe.pid,pe.sex,pe.name,&pe.fnum1,&pe.btnum);
                           fwrite(&pe,sizeof(struct people),1,fp1);
                      }
                      fclose(fp1);
                      printf("Success!\n");
                      break;
                  }else{
                      printf("Please operate again\n");
                      break;
                  }
            }else{
                p++;
            }
       }
    }

七、修改模块

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"airplane.h"
void Modify()
{
    FILE *fp;
    if((fp=fopen("airplane.dat","ab+"))==NULL){
        printf("Can not open file\n");
        exit(0);
    }
    long begin,end,lcount;
      fseek(fp,0L,SEEK_SET);
      begin=ftell(fp);
      fseek(fp,0L,SEEK_END);
      end=ftell(fp);
      lcount=(end-begin)/sizeof(struct airplane);//所有文件记录数
      rewind(fp);
      int Fnum;
      char Nscity[20],Nacity[20],Nstime[20],Natime[20];
      float Nprice,Ndiscount,Ntnum;
      struct airplane air[lcount],*p=air;
      fread(p,sizeof(struct airplane),lcount,fp);
      fclose(fp);
      if(lcount==0){
          printf("No data to modify\n");
      }else{
          printf("Please input the flight number you want to modify: ");
          int i;
        int pm=0;
          scanf("%d",&Fnum);
      for(i=0;i<lcount;i++,p++){
          if(p->fnum==Fnum){
              pm=1;
              break;
          }
      }
    if(pm==1){
        printf("The information you want to modify is as follow:\n");
        printf("StartCity||ArrivalCity||StartTime(00:00)||ArrivalTime(00:00)||Price||Discount(0-9)||Ticketnum\n");
        printf("%s          %s        %s            %s          %.2f          %.0f             %.0f\n",p->scity,p->acity,p->stime,p->atime,p->price,p->discount,p->tnum);
        printf("Please input the NEW information as instructed:\n");
        printf("Please input the NEW StartCity: ");
        scanf("%s",Nscity);
        strcpy(p->scity,Nscity);
        printf("Please input the NEW ArrivalCity: ");
        scanf("%s",Nacity);
        strcpy(p->acity,Nacity);
        printf("Please input the NEW StartTime: ");
        scanf("%s",Nstime);
        strcpy(p->stime,Nstime);
        printf("Please input the NEW ArrivalTime: ");
        scanf("%s",Natime);
        strcpy(p->atime,Natime);
        printf("Please input the NEW Price: ");
        scanf("%f",&Nprice);
        p->price=Nprice;
        printf("Please input the NEW Discount: ");
        scanf("%f",&Ndiscount);
        p->discount=Ndiscount;
        printf("Please input the NEW TicketNumber: ");
        scanf("%f",&Ntnum);
        p->tnum=Ntnum;
        printf("Modify is ok,New data are as follows: \n");
        printf("StartCity||ArrivalCity||StartTime(00:00)||ArrivalTime(00:00)||Price||Discount(0-9)||Ticketnum\n");
        printf("%s          %s        %s            %s          %.2f          %.0f             %.0f\n",p->scity,p->acity,p->stime,p->atime,p->price,p->discount,p->tnum);
    }else{
        printf("NO Data,please input the right flighg number! \n");
    }
    if((fp=fopen("airplane.dat","wb+"))==NULL){
        printf("Can not open file\n");
        exit(0);
    }
    p=air;
    fwrite(p,sizeof(struct airplane),lcount,fp);
    fclose(fp);
    
}
}

八、显示模块

#include<stdio.h>
#include<stdlib.h>
#include"airplane.h"
void ShowAll()
{
    FILE *fp;
     if((fp=fopen("airplane.dat","ab+"))==NULL){
         printf("can not open file\n");
         exit(0);
     }
     struct airplane air;
     fseek(fp,0L,SEEK_SET);
     fread(&air,sizeof(struct airplane),1,fp);
     printf("FlightNum || StartCity ||   ArrivalCity ||   StartTime ||   ArrivalTime ||   Price ||   Discount ||   Ticketnum\n");
     printf("----------||-----------||---------------||-------------||---------------||---------||------------||------------||\n");
     while(!feof(fp)){
     printf("%d             %s          %s       %s          %s             %.2f            %.0f           %.0f\n",air.fnum,air.scity,air.acity,air.stime,air.atime,air.price,air.discount,air.tnum);
        fread(&air,sizeof(struct airplane),1,fp);    
     }
     fclose(fp);
     
}

九、推荐模块

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"airplane.h"
void Recommend(){
    FILE *fp;
     if((fp=fopen("airplane.dat","ab+"))==NULL){
         printf("can not open file\n");
         exit(0);
     }
     long begin,end,lcount;
      fseek(fp,0L,SEEK_SET);
      begin=ftell(fp);
      fseek(fp,0L,SEEK_END);
      end=ftell(fp);
      lcount=(end-begin)/sizeof(struct airplane);//所有文件记录数
      rewind(fp);
      struct airplane air[lcount],*p=air;
      fread(p,sizeof(struct airplane),lcount,fp);
      char Pscity[20];
      char Pstime[20];
      printf("Please input your origin: ");
      scanf("%s",Pscity);    
      printf("Please input your take-off time: ");
      scanf("%s",Pstime);
        printf("There are the flights you can choose:\n");
        printf("FlightNum || StartCity ||   ArrivalCity ||   StartTime ||   ArrivalTime ||   Price ||   Discount ||   Ticketnum\n");
        printf("----------||-----------||---------------||-------------||---------------||---------||------------||------------||\n");
        int i;
        for(i=0;i<lcount;i++){
              if(strcmp(air[i].scity,Pscity)==0&&strcmp(Pstime,air[i].stime)<0){
                  printf("%d           %s         %s        %s      %s   %.2f     %.0f         %.0f\n",air[i].fnum,air[i].scity,air[i].acity,air[i].stime,air[i].atime,air[i].price,air[i].discount,air[i].tnum);
              }
              
           }
      
}

十、退票模块

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"airplane.h"
extern int iSave;
void Refund()
{
    FILE *fp1;
     if((fp1=fopen("people.dat","ab+"))==NULL){
         printf("can not open file\n");
         exit(0);
     }
     long begin,end,lcount;
      fseek(fp1,0L,SEEK_SET);
      begin=ftell(fp1);
      fseek(fp1,0L,SEEK_END);
      end=ftell(fp1);
      lcount=(end-begin)/sizeof(struct people);//所有文件记录数
      rewind(fp1);
      struct people pe[lcount],*p=pe;
      fread(p,sizeof(struct people),lcount,fp1);
      int Inid;
      printf("Please input your ID: ");
      scanf("%d",&Inid);
      while(p!=NULL){
          if(p->pid==Inid){
          printf("This is your tickets: \n");    
          printf("IDnumber     ||     Sex(M/F)     ||     NAME     ||     Fnumber(-1-end)     ||     BookTicketNumber\n");
          printf("%d                     %s                %s                 %d                           %d\n",p->pid,p->sex,p->name,p->fnum1,p->btnum);
          break;
          }else{
              p++;
          }
      }
      if(p==NULL){
                printf("The flight number is not found\n");
                return;
            }
            int choice;
            printf("Do you want to cancel it?(1.Yes/Other Figures.Exit) :");
            scanf("%d",&choice);
            if(choice==1){
                printf("Cancel Successfully!\n");
            }else{
                printf("Please operate again\n");
            }
        }

十一、时间模块

#include<stdio.h>
#include"airplane.h"
#include<time.h>
void CurrentTime()
{
    time_t Ti;
    Ti=time(NULL);//返回当前日历时间
    printf("The current time is %s \n", ctime(&Ti));//将*Ti中的日历时间转换为当地时间的字符串,并返回指向该字符串指针。
    
}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值