• 【数据结构】小项目:航班查询系统


    项目要求

      1.已经给出链表定义(本系统用双链表实现更为方便,但是由于要求用单链表,所以按照规定做事)

      2.信息录入(当然是添加航班与取消航班了)

      3.按照起飞时间先后顺序排列(可以在插入时即顺序插入,但为了体现排序过程,封装成了排序函数)

      4.可根据不同关键字进行查询(实现了三种具有代表性的查询方案:航班号查询(结果唯一),起点站查询(结果不唯一),路线查询(最常用))


    实现

    头文件

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 #ifndef __FLIGHT_H
     7 #define __FLIGHT_H
     8 
     9 #include <stdio.h>
    10 #include <stdlib.h>
    11 #include <stdbool.h>
    12 #include <string.h>
    13 
    14 typedef struct flight
    15 {
    16     char number[10];//航班号
    17     char staddress[20];//起站点
    18     char arraddress[20];//终点站
    19     char date[10];//班期
    20     char type[4];//机型
    21     int stime;//起飞时间
    22     int atime;//到达时间
    23     int value;//标价
    24 }datatype;
    25 
    26 typedef struct node
    27 {
    28     datatype info;
    29     struct node *next;
    30 }node_list,*p_node_list;
    31 
    32 
    33 extern void flight_manage(p_node_list phead);//航班管理
    34 extern void flight_add(p_node_list phead);//添加航班
    35 extern p_node_list in_number(p_node_list phead, char *);//通过航班号查询航班,有则返回指向航班的前一个节点的指针(删除节点时方便操作)
    36 extern void flight_cancel(p_node_list phead);//取消航班
    37 extern void flight_show(p_node_list phead);//显示航班信息
    38 extern void flight_search(p_node_list phead);//查询航班信息
    39 
    40 extern void list_init(p_node_list *phead);//链表头初始化
    41 extern void keep_scren();//保持界面
    42 extern void flight_sort(p_node_list phead);//按起飞时间排序
    43 
    44 extern void flight_print(p_node_list pnode);//打印pnode指向的结点的航班信息
    45 
    46 
    47 
    48 
    49 extern void flight_search(p_node_list phead);//查询航班信息
    50 extern void search_by_number(p_node_list phead, char *num);//航班号查询
    51 extern void search_by_saddr(p_node_list phead, char *saddr);//起点站查询
    52 extern void search_by_line(p_node_list phead, char *saddr, char *daddr);//航线查询
    53 
    54 
    55 
    56 #endif
    View Code

     主文件

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 //功能: 1.信息录入
     7 //     2.信息显示(按照起飞时间先后顺序显示)
     8 //     3.信息查询(可根据不同的关键字进行查询)
     9 
    10 #include "flight.h"
    11 
    12 int main()
    13 {
    14     p_node_list phead;
    15     int num;//功能选择
    16     int ret;//存储scanf返回值
    17 
    18     list_init(&phead);//初始化
    19 
    20     do
    21     {
    22         system("clear");
    23         printf("	Flight Searching System
    ");
    24         printf("******************************* 
    ");
    25         printf("1.Manage Flight 
    ");
    26         printf("2.Printf Flight Information 
    ");
    27         printf("3.Search Flight Information 
    ");
    28         printf("4.Exit 
    ");
    29         printf("******************************* 
    ");
    30         printf("Pls select:");
    31 
    32         ret = scanf("%d",&num);
    33         while('
    ' != getchar());//清空输入缓冲区
    34         
    35         if(1 != ret)//输入字符
    36         {
    37             printf("******************************* 
    ");
    38             printf("	Pls input your select !
    ");
    39             printf("******************************* 
    ");
    40             sleep(2);
    41         }
    42 
    43 
    44         switch(num)
    45         {
    46             case 1://航班管理
    47                 flight_manage(phead);
    48                 break;
    49             case 2://显示航班信息
    50                 flight_show(phead);
    51                 break;
    52             case 3://查询航班信息
    53                 flight_search(phead);
    54                 break;
    55             case 4://退出系统
    56                 exit(0);
    57             default:
    58                 break;
    59         }
    60     }
    61     while(4 != num);
    62 
    63     return 0;
    64 }
    View Code

    航班管理文件

      1 //@ author 成鹏致远
      2 //@ net http://infodown.tap.cn
      3 //@ qq 552158509
      4 //@ blog lcw.cnblogs.com
      5 
      6 #include "flight.h"
      7 
      8 void flight_manage(p_node_list phead)//航班管理
      9 {
     10     int num;
     11     int ret;//存储scanf返回值
     12 
     13     do
     14     {
     15         system("clear");
     16         printf("	Flight Management
    ");
     17         printf("******************************* 
    ");
     18         printf("1.Add flight 
    ");
     19         printf("2.Cancel flight 
    ");
     20         printf("3.Print flight information 
    ");
     21         printf("4.Exit 
    ");
     22         printf("******************************* 
    ");
     23         printf("Pls select:");
     24 
     25         ret = scanf("%d",&num);
     26         while('
    ' != getchar());//清空缓冲区
     27 
     28         if(1 != ret)//输入字符
     29         {
     30             printf("******************************* 
    ");
     31             printf("	Pls input your select !
    ");
     32             printf("******************************* 
    ");
     33             sleep(2);
     34         }
     35         switch(num)
     36         {
     37             case 1://添加航班
     38                 flight_add(phead);
     39                 break;
     40             case 2://取消航班
     41                 flight_cancel(phead);
     42                 break;
     43             case 3://航班信息
     44                 flight_show(phead);
     45                 break;
     46             case 4:
     47                 return;
     48             default:
     49                 break;
     50         }
     51     }
     52     while(4 != num);
     53 }
     54 
     55 void flight_add(p_node_list phead)//添加航班
     56 {
     57     p_node_list pnode;
     58 
     59     pnode = (p_node_list)malloc(sizeof(node_list));
     60     if(NULL == pnode)
     61     {
     62         perror("flight_add(malloc)");
     63         exit(1);
     64     }
     65     
     66     //输入航班信息
     67     system("clear");
     68     printf("	Pls input flight information:
    ");
     69     printf("******************************* 
    ");
     70     printf("1.Flight number      :");
     71         while(1)//航班号为主键,不能重复
     72         {
     73             gets(pnode->info.number);
     74             if(NULL != in_number(phead,pnode->info.number))
     75                 printf("Flight number exist,Pls input again:");
     76             else
     77                 break;
     78         }
     79     printf("2.Start address      :");
     80         gets(pnode->info.staddress);
     81     printf("3.Destination address:");
     82         gets(pnode->info.arraddress);
     83     printf("4.Flight date        :");
     84         gets(pnode->info.date);
     85     printf("5.Flight type        :");
     86         gets(pnode->info.type);
     87     printf("6.Flight start time  :");//需要对输入时间进行格式化处理和判断,这里略过
     88         scanf("%d",&pnode->info.stime);
     89     printf("7.Flight arrive time :");
     90         scanf("%d",&pnode->info.atime);
     91     printf("8.Ticket price       :");
     92         scanf("%d",&pnode->info.value);
     93         
     94     pnode->next = phead->next;
     95     phead->next = pnode;
     96     printf("*******************************");
     97     printf("
    	Add success!
    ");
     98     printf("*******************************");
     99     keep_scren();//保持界面
    100 
    101 }
    102 
    103 p_node_list in_number(p_node_list phead, char *number)//通过航班号查询是否已有此航班,如果有,则返回指向航班的前一个节点的指针(删除节点时方便操作)
    104 {
    105     p_node_list pnode  = phead;//遍历节点
    106     
    107     while(NULL != pnode->next)
    108     {
    109         if(0 == strcmp(number,pnode->next->info.number))
    110         {
    111             return pnode;
    112         }
    113         pnode = pnode->next;
    114     }
    115 
    116     return NULL;
    117 }
    118 
    119 void flight_cancel(p_node_list phead)//取消航班
    120 {
    121     char num[10];
    122     p_node_list pnode;//存储删除节点前一节点地址
    123     p_node_list tmp;//临时指向删除节点地址
    124 
    125     printf("Pls input the number to be cancel:");
    126     gets(num);
    127     pnode = in_number(phead,num);
    128 
    129 
    130     if(NULL == pnode)//不存在此航班
    131     {
    132         printf("	No this flight,Pls check!");
    133         keep_scren();//保持界面
    134     }
    135     else//删除航班
    136     {
    137         tmp = pnode->next;//删除该节点
    138         pnode->next = tmp->next;//这里可以利用in_number()的返回值进行删除节点,pnode为另一变量,故只能对pnode->next赋值才能改变原链表
    139         free(tmp);
    140         printf("	Cancel success,Pls check!");
    141         keep_scren();//保持界面
    142     }
    143     
    144 }
    145 
    146 void flight_show(p_node_list phead)//显示航班信息
    147 {
    148     printf("*******************************
    ");
    149     printf("	Flight Information
    ");
    150     printf("*******************************
    ");
    151     p_node_list pnode = phead->next;
    152 
    153     if(NULL == pnode)
    154     {
    155         printf("	No flight,Pls check!
    ");
    156         printf("*******************************
    ");
    157     }
    158     
    159     while(NULL != pnode)
    160     {
    161         flight_sort(phead);//先按起飞时间进行排序
    162         flight_print(pnode);//打印航班信息
    163         pnode = pnode->next;
    164     }
    165     keep_scren();//保持界面
    166 }
    167 
    168 
    169 
    170 void list_init(p_node_list *phead)//链表头初始化
    171 {
    172     *phead = (p_node_list)malloc(sizeof(node_list));
    173     if(NULL == *phead)
    174     {
    175         perror("List_init(malloc)");
    176         exit(1);
    177     }
    178     (*phead)->next = NULL;//初始化为空
    179 }
    180 
    181 void keep_scren()//保持界面
    182 {
    183     char answer;
    184 
    185     printf("
    *******************************
    ");
    186     printf("	return ?(y/Y) 
    ");
    187     while(1)
    188     {
    189         scanf("%c",&answer);
    190         if('y' == answer || 'Y' == answer)
    191             return;
    192     }
    193 }
    194 
    195 
    196 void flight_sort(p_node_list phead)//按起飞时间排序
    197 {
    198     p_node_list pnode = phead->next;
    199     p_node_list tmp;
    200     datatype data;//用来临时存放元素数据
    201     
    202     while(NULL != pnode)
    203     {
    204         tmp = pnode->next;
    205 
    206         while(NULL != tmp)
    207         {
    208             if(pnode->info.stime > tmp->info.stime)//交换值域
    209             {
    210                 data = tmp->info;
    211                 tmp->info = pnode->info;
    212                 pnode->info = data;
    213             }
    214             tmp = tmp->next;
    215         }
    216         pnode = pnode->next;
    217     }
    218 
    219 }
    220 
    221 void flight_print(p_node_list pnode)//打印pnode指向的结点的航班信息
    222 {
    223     printf("Flight number      :%s	<------Key value 
    Start address      :%s 
    Destination address:%s 
    Flight date        :%s 
    Flight type        :%s 
    Start time         :%d 
    Arrive time        :%d 
    Ticket price       :%d 
    ",pnode->info.number,pnode->info.staddress,pnode->info.arraddress,pnode->info.date,pnode->info.type,pnode->info.stime,pnode->info.atime,pnode->info.value);
    224     printf("*******************************
    ");
    225 }
    View Code

     航班查询文件

      1 //@ author 成鹏致远
      2 //@ net http://infodown.tap.cn
      3 //@ qq 552158509
      4 //@ blog lcw.cnblogs.com
      5 
      6 #include "flight.h"
      7 
      8 
      9 void flight_search(p_node_list phead)//查询航班信息
     10 {
     11     int num;
     12     int ret;//存储scanf返回值
     13     char number[10],saddr[20],daddr[20];//航班号,起点站,终点站
     14 
     15     do
     16     {
     17         system("clear");
     18         printf("	Search Flight!
    ");
     19         printf("******************************* 
    ");
     20         printf("1.Search by number 
    ");
     21         printf("2.Search by start address 
    ");
     22         printf("3.Search by flight line 
    ");
     23         printf("4.Exit 
    ");
     24         printf("******************************* 
    ");
     25         printf("Pls select:");
     26 
     27         ret = scanf("%d",&num);
     28         while('
    ' != getchar());//清空缓冲区
     29 
     30         if(1 != ret)//输入字符
     31         {
     32             printf("******************************* 
    ");
     33             printf("	Pls input your select
    ");
     34             printf("******************************* 
    ");
     35             sleep(2);
     36         }
     37         switch(num)
     38         {
     39             case 1://航班号查询
     40                 {
     41                     printf("Pls input the number:");
     42                     gets(number);
     43                     search_by_number(phead,number);
     44                     break;
     45                 }
     46             case 2://起点站查询
     47                 {
     48                     printf("Pls input the start address:");
     49                     gets(saddr);
     50                     search_by_saddr(phead,saddr);
     51                     break;
     52                 }
     53             case 3://路线查询
     54                 {
     55                     printf("Pls input the start address:");
     56                     gets(saddr);
     57                     printf("Pls input the destination address:");
     58                     gets(daddr);
     59                     search_by_line(phead,saddr,daddr);
     60                     break;
     61                 }
     62             case 4:
     63                 return;
     64             default:
     65                 break;
     66         }
     67     }
     68     while(4 != num);
     69 }
     70 
     71 
     72 void search_by_number(p_node_list phead, char *num)//航班号查询
     73 {
     74     p_node_list pnode = in_number(phead, num);//返回指定节点的下一个节点
     75     if(NULL == pnode)
     76     {
     77         printf("No Flight!
    ");
     78         keep_scren();
     79     }
     80     else//打印航班信息
     81     {
     82         pnode = pnode->next;
     83         flight_print(pnode);
     84         keep_scren();
     85     }
     86 }
     87 
     88 
     89 void search_by_saddr(p_node_list phead, char *saddr)//起点站查询
     90 {
     91     p_node_list pnode = phead->next;
     92 
     93     if(NULL == pnode)//无航班
     94     {
     95         printf("No Flight!
    ");
     96         keep_scren();
     97     }
     98     else//遍历查找
     99     {
    100         while(NULL != pnode)
    101         {
    102             if(0 == strcmp(saddr,pnode->info.staddress))//找到
    103             {
    104                 flight_print(pnode);//打印航班信息
    105                 keep_scren();
    106                 break;
    107             }
    108             pnode = pnode->next;
    109         }
    110     }
    111 }
    112 void search_by_line(p_node_list phead, char *saddr, char *daddr)//航线查询
    113 {
    114     p_node_list pnode = phead->next;
    115 
    116     if(NULL == pnode)//无航班
    117     {
    118         printf("No Flight!
    ");
    119         keep_scren();
    120     }
    121     else//遍历查找
    122     {
    123         while(NULL != pnode)
    124         {
    125             if(0 == strcmp(saddr,pnode->info.staddress) && 0 == strcmp(daddr,pnode->info.arraddress))//找到
    126             {
    127                 flight_print(pnode);//打印航班信息
    128                 keep_scren();
    129                 break;
    130             }
    131             pnode = pnode->next;
    132         }
    133     }
    134 }
    View Code

     Makefile文件

    1 flight:flight_manage.c main.c flight_search.c
    2         gcc -o $@ $^
    3 
    4 clean:
    5         $(RM) flight .*.sw?
    View Code
  • 相关阅读:
    复习一些奇怪的题目
    NOIP 考前 KMP练习
    NOIP 考前 并查集复习
    NOIP 考前 Tarjan复习
    NOIP 考前 图论练习
    BZOJ 1468 树分治
    Codeforces Round #376 (Div. 2)
    CodeVS 线段覆盖1~5
    Luogu 3396 权值分块
    BZOJ 2743 树状数组
  • 原文地址:https://www.cnblogs.com/lcw/p/3235151.html
Copyright © 2020-2023  润新知