//plane_h.h
#ifndef plane_h
#define plane_h
#define SIZE 10//航班号位数
struct citys
{
char *leave_city;
char *land_city;
};
struct time
{
int yea;
int mon;
int day;
int tim;
int min;
};
struct timer
{
time start_off_time;
time arrive_time;
};
typedef struct Airplane
{
float value;//票价
int chech_NU;//票数
char plane_Nu[SIZE];// 航班号
struct timer Tim;//起降时间
struct citys City;// 期间城市
int target;//标志航班是否满员
struct Airplane *next;
}Airplane;
int read_infomation( Airplane *r_plane );
Airplane *refer_to_nu( Airplane *r_plane );
Airplane *refer_to_road( Airplane *r_plane );
void book_ticket( Airplane *r_plane );
int save_text( Airplane *r_plane );
#endif
////member.cpp
#include "plane_h.h"
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
/*
**从指定的路径文件中读取信息到单链表r_plane中
**如果成功则返回值0, 否则返回值-1
*/
int read_infomation( Airplane *r_plane )
{
FILE *fp;
if( !(fp=fopen("d:\\plane.txt", "r")) )
return -1;
Airplane *temp;
while( feof(fp) == 0 )
{
temp=(Airplane *) malloc (sizeof(Airplane));
fread( temp, sizeof(Airplane), 1, fp );
temp->next = r_plane->next;
r_plane->next = temp;
}
return 0;
}
/*
**功能:依照航班号进行查询工作
**查询成功则返回相应的指针值,否则返回值NULL
*/
Airplane *refer_to_nu( Airplane *r_plane )
{
if( !r_plane->next )
return NULL;
Airplane *temp = r_plane->next;
#if SIZE
char string[SIZE];
printf("输入航班号:");
scanf("%s", string);
while( temp )
{
if( strcmp(temp->plane_Nu, string ) == 0 )
return temp;
temp = temp->next;
}
#endif
}
/*
**功能:依照航班的起止位置进行查询工作
**查询成功则返回相应的指针值, 否则返回值NULL
*/
Airplane *refer_to_road( Airplane *r_plane )
{
if( !r_plane->next )
return NULL;
Airplane *temp = r_plane->next;
char str_start[15], str_arriv[15];//定义起止位置字符串变量
printf("输入航班的起飞城市:");
scanf("%s", str_start);
printf("输入航班的降落城市:");
scanf("%s", str_arriv);
while( temp )
{
if( strcmp(temp->City.leave_city, str_start ) == 0 && strcmp(
temp->City.land_city, str_arriv ) == 0 )
return temp;
temp = temp->next;
}
}
/*
**功能:实现订票操作功能
**本函数是采用调用refer_to_road()函数功能
*/
void book_ticket( Airplane *r_plane )
{
Airplane *temp;
temp = refer_to_road( r_plane );
//判断是否航班满员1 表示满
0 表示没有满状态
if( temp->target == 1 )
printf("\t对不起, 此次航班机票已经全部售完!\n");
else
{
temp->chech_NU--;
if( temp->chech_NU == 0 )
temp->target = 1;
}
}
/*
**功能:完成在指定路径的文件进行存储
**如果成功则返回值0, 否则返回值-1
*/
int save_txt( Airplane *r_plane )
{
FILE *fp;
if( !(fp=fopen("d:\\plane.txt", "w")))
return -1;
Airplane *temp = r_plane->next;
while( temp )
{
fwrite( temp, sizeof( Airplane ), 1, fp );
temp = temp->next;
}
return 0;
}
///main.cpp
#include "plane_h.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
Airplane *r_plane = (Airplane *) malloc (sizeof( Airplane ));
r_plane->next = NULL;
if( read_infomation( r_plane ) == -1 )
exit( -1 );
int choose;
while( 1 )
{
printf("");//提示菜单输出
printf("根据提示菜单进行选择:");
scanf("%d", &choose);
switch( choose )
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
//...
}
}
return 0;
}