航班管理系统:
航班信息录入 显示所有航班信息 按照要求查询(根据日期、地点等),列出相关的航班信息 根据航班号删除航班信息
结构体定义:
typedef struct flight
{
char flight_number[ 16 ] ;
char departure[ 32 ] ;
char destination[ 32 ] ;
char take_off_time[ 16 ] ;
float price;
struct flight * prev;
struct flight * next;
} Flight_t, F_List;
typedef struct flightInfo
{
char flight_number[ 16 ] ;
char departure[ 32 ] ;
char destination[ 32 ] ;
char take_off_time[ 16 ] ;
float price;
} Info_t;
函数预定义:
Manager_t * Flight_Create ( void ) ;
Flight_t * Flight_NewInfo ( Info_t info) ;
bool Flight_IsEmpty ( F_List * manager) ;
bool Flight_TailInsert ( F_List * manager, Info_t info) ;
bool Flight_FlightNumberDel ( F_List * manager, char * flight_number) ;
bool Flight_FlightNumberFind ( F_List * manager, char * flight_number) ;
bool Flight_PriceRevise ( F_List * manager, char * flight_number, float newPrice) ;
bool Flight_print ( F_List * manager) ;
void Flight_Interface ( void ) ;
各函数具体实现:
flight.c文件:
# include "flight.h"
F_List Flight_Create ( void ) {
F_List list = ( F_node * ) calloc ( 1 , sizeof ( F_node) ) ;
list-> prev = list-> next = NULL ;
return list;
}
F_node * Flight_NewInfo ( Info_t info) {
F_node* node = ( F_node * ) malloc ( sizeof ( F_node) ) ;
if ( ! node) {
printf ( "节点申请内存失败!请重新尝试\n" ) ;
}
strcpy ( node-> flight_number, info. flight_number) ;
strcpy ( node-> departure, info. departure) ;
strcpy ( node-> destination, info. destination) ;
strcpy ( node-> take_off_time, info. take_off_time) ;
node-> price = info. price;
node-> prev = node-> next = NULL ;
return node;
}
bool Flight_IsEmpty ( F_List manager) {
return ( manager-> next == NULL ) ? true : false;
}
bool Flight_TailInsert ( F_List manager, Info_t info) {
F_node * node = Flight_NewInfo ( info) ;
if ( Flight_IsEmpty ( manager) ) {
manager-> next = node;
manager-> prev = node;
node-> prev = manager;
node-> next = manager;
return true;
}
node-> next = manager-> prev;
manager-> prev-> next = node;
manager-> prev = node;
node-> next = manager;
return true;
}
bool Flight_FlightNumberDel ( F_List manager, char * flight_number) {
F_node * q = manager;
F_node * p = manager-> next;
while ( p != manager) {
if ( ! strcmp ( p-> flight_number, flight_number) ) {
if ( p-> next == manager) {
q-> next = manager;
manager-> next = q;
p-> next = p-> prev = NULL ;
free ( p) ;
return true;
}
q-> next = p-> next;
q-> next-> prev = q;
p-> next = p-> prev = NULL ;
return true;
}
q = p;
p = p-> next;
}
return false;
}
bool Flight_FlightNumberFind ( F_List manager, char * flight_number) {
F_node * p = manager-> next;
while ( p!= manager)
{
if ( ! strcmp ( p-> flight_number, flight_number) ) {
printf ( "航班号\t出发地\t目的地\t起飞时间\t价格\n" ) ;
printf ( "%s\t%s\t%s\t%s\t%f\n" ,
p-> flight_number, p-> departure, p-> destination, p-> take_off_time, p-> price) ;
return true;
}
p = p-> next;
}
if ( p == manager) {
printf ( "没有%s该航班信息。\n" , flight_number) ;
}
return false;
}
bool Flight_PriceRevise ( F_List manager, char * flight_number, float newPrice) {
F_node * p = manager-> next;
while ( p!= manager)
{
if ( ! strcmp ( p-> flight_number, flight_number) ) {
p-> price = newPrice;
return true;
}
p = p-> next;
}
if ( p == manager) {
printf ( "没有%s该航班信息。" , flight_number) ;
}
return false;
}
bool Flight_print ( F_List manager) {
F_node * p = manager-> next;
printf ( "航班号\t出发地\t目的地\t起飞时间\t价格\n" ) ;
while ( p!= manager)
{
printf ( "%s\t%s\t%s\t%s\t\t%.2f\n" ,
p-> flight_number, p-> departure, p-> destination, p-> take_off_time, p-> price) ;
p = p-> next;
}
return false;
}
void Flight_Interface ( void ) {
printf ( "===================航班信息查询系统===================\n" ) ;
printf ( "*******\t\t创建今日航班信息:1\t\t*******\n" ) ;
printf ( "*******\t\t 查询航班信息:2 \t\t*******\n" ) ;
printf ( "*******\t\t 删除航班信息:3 \t\t*******\n" ) ;
printf ( "*******\t\t 展示所有信息:4 \t\t*******\n" ) ;
printf ( "*******\t\t 修改航班票价:5 \t\t*******\n" ) ;
printf ( "*******\t\t 添加航班信息:6 \t\t*******\n" ) ;
printf ( "*******\t\t 退出系统:0 \t\t\t*******\n" ) ;
printf ( "======================================================\n" ) ;
}
flght.h文件
# define _FLIGHT_H
# ifdef _FLIGHT_H
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <stdbool.h>
typedef struct flight
{
char flight_number[ 16 ] ;
char departure[ 32 ] ;
char destination[ 32 ] ;
char take_off_time[ 16 ] ;
float price;
struct flight * prev;
struct flight * next;
} F_node, * F_List;
typedef struct flightInfo
{
char flight_number[ 16 ] ;
char departure[ 32 ] ;
char destination[ 32 ] ;
char take_off_time[ 16 ] ;
float price;
} Info_t;
F_List Flight_Create ( void ) ;
F_node * Flight_NewInfo ( Info_t info) ;
bool Flight_IsEmpty ( F_List manager) ;
bool Flight_TailInsert ( F_List manager, Info_t info) ;
bool Flight_FlightNumberDel ( F_List manager, char * flight_number) ;
bool Flight_FlightNumberFind ( F_List manager, char * flight_number) ;
bool Flight_PriceRevise ( F_List manager, char * flight_number, float newPrice) ;
bool Flight_print ( F_List manager) ;
void Flight_Interface ( void ) ;
# endif
main.c文件:
# include "flight.h"
Info_t setInfo ( ) {
Info_t info;
printf ( "请输入航班号:\n" ) ;
scanf ( "%s" , info. flight_number) ;
printf ( "请输入出发地:\n" ) ;
scanf ( "%s" , info. departure) ;
printf ( "请输入目的地:\n" ) ;
scanf ( "%s" , info. destination) ;
printf ( "请输入起飞时间:\n" ) ;
scanf ( "%s" , info. take_off_time) ;
printf ( "请输入价格:\n" ) ;
scanf ( "%f" , & info. price) ;
return info;
}
void main ( ) {
F_List list = NULL ;
int fun;
char * f_num = ( char * ) malloc ( sizeof ( char ) ) ;
Info_t info;
float new_price;
while ( 1 )
{
Flight_Interface ( ) ;
printf ( "请输入数字选择功能:" ) ;
scanf ( "%d" , & fun) ;
switch ( fun)
{
case 1 :
list = Flight_Create ( ) ;
printf ( "初始化成功\n" ) ;
break ;
case 2 :
printf ( "请输入航班编号:" ) ;
scanf ( "%s" , f_num) ;
Flight_FlightNumberFind ( list, f_num) ;
break ;
case 3 :
printf ( "请输入需要删除的航班信息:" ) ;
scanf ( "%s" , f_num) ;
if ( Flight_FlightNumberDel ( list, f_num) )
printf ( "删除成功\n" ) ;
else printf ( "删除失败\n" ) ;
break ;
case 4 :
Flight_print ( list) ;
break ;
case 5 :
printf ( "请输入需要修改的航班号:" ) ;
scanf ( "%s" , f_num) ;
printf ( "请输入新的机票价格:" ) ;
scanf ( "%f" , & new_price) ;
Flight_PriceRevise ( list, f_num, new_price) ;
break ;
case 6 :
info = setInfo ( ) ;
Flight_TailInsert ( list, info) ;
printf ( "插入成功!\n" ) ;
break ;
default :
break ;
}
}
}