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

您所在的位置:网站首页 航班班次信息查询 【数据结构】小项目:航班查询系统

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

2024-01-25 23:54| 来源: 网络整理| 查看: 265

项目要求

  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 10 #include 11 #include 12 #include 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("\tFlight Searching System\n"); 24 printf("******************************* \n"); 25 printf("1.Manage Flight \n"); 26 printf("2.Printf Flight Information \n"); 27 printf("3.Search Flight Information \n"); 28 printf("4.Exit \n"); 29 printf("******************************* \n"); 30 printf("Pls select:"); 31 32 ret = scanf("%d",&num); 33 while('\n' != getchar());//清空输入缓冲区 34 35 if(1 != ret)//输入字符 36 { 37 printf("******************************* \n"); 38 printf("\tPls input your select !\n"); 39 printf("******************************* \n"); 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("\tFlight Management\n"); 17 printf("******************************* \n"); 18 printf("1.Add flight \n"); 19 printf("2.Cancel flight \n"); 20 printf("3.Print flight information \n"); 21 printf("4.Exit \n"); 22 printf("******************************* \n"); 23 printf("Pls select:"); 24 25 ret = scanf("%d",&num); 26 while('\n' != getchar());//清空缓冲区 27 28 if(1 != ret)//输入字符 29 { 30 printf("******************************* \n"); 31 printf("\tPls input your select !\n"); 32 printf("******************************* \n"); 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("\tPls input flight information:\n"); 69 printf("******************************* \n"); 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("\n\tAdd success!\n"); 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("\tNo 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("\tCancel success,Pls check!"); 141 keep_scren();//保持界面 142 } 143 144 } 145 146 void flight_show(p_node_list phead)//显示航班信息 147 { 148 printf("*******************************\n"); 149 printf("\tFlight Information\n"); 150 printf("*******************************\n"); 151 p_node_list pnode = phead->next; 152 153 if(NULL == pnode) 154 { 155 printf("\tNo flight,Pls check!\n"); 156 printf("*******************************\n"); 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("\n*******************************\n"); 186 printf("\treturn ?(y/Y) \n"); 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\t


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3