**
飞机订票系统
**
一、功能要求
飞机订票系统是外出旅游、商务活动是常用的软件。用户使用该软件可以完成查询航班、订票、退票、付款等操作,管理端也能方便对系统的管理,如添加航班,实时更新航班状态等。请你设计一款实用的飞机订票系统,完成相应的功能。
通过此课题,熟练掌握文件、数组、指有等的各种操作,以及一些算法思想的应用。
1.功能要求
(1) 假设某民航机场,共有n 个航班,每个航班有航班号、确定的航线
(起始点,终点站,中途是否有转机及转机城市),确定的飞行时间和一定的成员定
额等相关信息,
(2) 要求设计一个实用的飞机订票系统,能够提供以下服务:
A.服务器端部分:
完成管理员口令验证进入系统:
航班信息的录入以及浏览:
动态航班的实时更新:
B.客户端部分:
完成用户口令验证进入系统:
航班的浏览:
有条件的航班查询:
订票及退票:
2.选做要求:
(1) 票务统计,并将统计结果反馈给管理员:
(2) 用户满意度调查,并将调查结果反馈给用户:
(3) 如果有更好的、更合理功能,可自加。
3 其他要求
(1) 界面美观,交互方便,实用性强。
(2) 注释详细: 每个变量都要求有注释说明用途; 函数有注释说明功能,对参数、返回值也要以注释的形式说明用途: 关键的语句段要求有注释解释。
(3) 程序的层次清晰,可读性强。
(4) 变量、函数命名符合规范。
(5) 如有可能,可使用MFC 等开发工具,实现彩色或图形操作界面。
4 开发环境
可以选择TC2.0、TC3.0、VC++6.0 等开发环境,或者与老师讨论,选择自己熟悉的开发工具与平台。
二、代码展示
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#define LEN sizeof (Flight)
#define M 2018
#define N 15
typedef struct Time
{
int month;
int day;
int hour;
int minute;
} Time;
typedef struct Flight
{
char origin[15];
char terminal[15];
Time time[2];
char number[8]; //航班
int total; //总座位数
double price; //票价
} Flight;
//菜单函数与口令验证
void menu();
void menu1();
void menu2();
void menuA();
void menuB();
void PasswordVerificationA();
void PasswordVerificationB();
//管理员的相关函数
void Enter();
void planeAdd();
void Browse();
void Delete();
//用户的相关函数
void book();
void refund();
void change();
void Inquire();
void InquireByPlace();
void InquireByTime1();
void InquireByTime2();
void InquireByPrice();
int TimeDiffer(Time item1,Time origin);//时间差值函数的原型声明
int LastDay(int month);
void Plane();
//全局变量与全局数组
int main()
{
int a[10];
Plane();
printf("********************************************************************\n");
printf(" 欢迎进入票务系统! \n");
printf("********************************************************************\n");
printf("********************************************************************\n");
printf(" Welcome to the ticketing system. \n");
printf("********************************************************************\n");
menu();
return 0;
}
//主界面
void menu()
{
int n;
printf("*************************************************\n");
printf(" 管理员 请按0 \n");
printf(" 旅客 请按1 \n");
printf("*************************************************\n");
printf("*************************************************\n");
printf(" administrator please enter 0 \n");
printf(" passenger please enter 1 \n");
printf("*************************************************\n");
scanf("%d",&n);
if(n==0)
PasswordVerificationA();
if(n==1)
PasswordVerificationB();
}
//完成管理员口令验证进入系统
void PasswordVerificationA()
{
system("cls");
static int error;
char a[7];
int i,j;
printf("请输入密码:\n");
printf("Please enter password:\n");
for(i=0;i<3;i++)
{
for(j=0;j<6;j++)
{
a[j]=getch();
printf("*");
}
printf("\n");
a[j]='\0';
if(strcmp(a,"123456")==0)//判断密码是否正确
menu1(); //进入管理员操作界面
else
{
printf("输入密码错误!\n\n");
printf("The password error!\n");
error++;
if(error>=3)
{
printf("非法用户!\n");
printf("illegal user!\n");
return;
}
}
}
}
void menu1()
{
system("cls");
int a;
printf(" / \\ \n");
printf(" --=--| |--=-- \n");
printf(" \\ / \n");
printf("是否继续,继续请按0,退出请按1\n");
printf("to continue, press 0,exit please press 1.");
scanf("%d",&a);
if(a==0)
menuA();
if(a==1)
menu();
}
//完成用户口令验证进入系统,修改验证码
void PasswordVerificationB()
{
int i;
system("cls");
static int error;
char arr[5],arr1[5],str;
srand(time(NULL));
for(i=0;i<4;i++)
{
str=rand()%(26)+65;
arr[i]=str;
}
arr[i]='\0';
puts(arr);
printf("请输入验证码:\n");
printf("Please input the verification code:\n\n");
scanf("%s",arr1); //判断输入的验证码是否正确
if(strcmp(arr1,arr)==0)
menu2(); //进入旅客界面
else
{
printf("输入密码错误!\n\n");
printf("Error input!\n\n");
error++;
if(error>=5)
{
printf("非法用户!\n");
printf("Illegal custom!\n\n");
return;
}
}
}
void menu2()
{
system("cls");
int a;
printf(" * * \n");
printf(" * * * * * * * * \n");
printf(" * o * * o * \n");
printf(" * * * * * * * * \n");
printf(" * * \n");
printf("是否继续,继续请按1,退出请按2\n");
printf("to continue, press 1,exit please press 2.");
scanf("%d",&a);
if(a==1)
menuB();
if(a==2)
menu();
}
//管理员界面函数
void menuA()
{
system("cls");
int n;
printf("|------------------------------------------| /\\ /\\ \n");
printf("| 1.录入航班信息 | / \\ / \\ \n");
printf("|------------------------------------------| \\ / \\ / \n");
printf("| 2.增加航班信息 | \\/ \\/ \n");
printf("|------------------------------------------| \n");
printf("| 3.浏览航班信息 | /\\ /\\ \n");
printf("|------------------------------------------| / \\ / \\ \n");
printf("| 4.删除航班信息 | \\ / \\ / \n");
printf("|------------------------------------------| \\/ \\/ \n");
printf("| 5.退出此界面 | \n");
printf("|------------------------------------------| \n");
printf("请输入你的选择:\n");
printf("|------------------------------------------| /\\ /\\ \n");
printf("| 1.Enter flight information. | / \\ / \\ \n");
printf("|------------------------------------------| \\ / \\ / \n");
printf("| 2.increase flight information | \\/ \\/ \n");
printf("|------------------------------------------| \n");
printf("| 3.Browse flight information. | /\\ /\\ \n");
printf("|------------------------------------------| / \\ / \\ \n");
printf("| 4 .Delete flight information. | \\ / \\ / \n");
printf("|------------------------------------------| \\/ \\/ \n");
printf("| 5.Exit this interface. | \n");
printf("|------------------------------------------| \n");
printf("Please enter your choice:\n\n");
scanf("%d",&n);
switch(n)
{
case 1:
Enter();
break;
case 2:
planeAdd();
break;
case 3:
Browse();
break;
case 4:
Delete();
break;
case 5:
menu();
break;
default:
printf("无此选择!(Had no choice!)\n");
}
}
//用户界面函数
void menuB()
{
system("cls");
int i;
printf("|^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*|\n");
printf("| 1.浏览 2.查询 |\n");
printf("|^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*|\n");
printf("| 3.订票 4.退票 |\n");
printf("|^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*|\n");
printf("| 5.改签 6.退出 |\n");
printf("|^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*|\n");
printf("请输入你的选择:\n\n");
printf("|^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*|\n");
printf("| 1.Browse. 2.Query. |\n");
printf("|^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*|\n");
printf("| 3.Book. 4.Refund. |\n");
printf("|^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*|\n");
printf("| 5.Change. 6.Exit. |\n");
printf("|^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*|\n");
printf("Please enter your choice:\n\n");
scanf("%d",&i);
switch(i)
{
case 1:
Browse();
menu();
break;
case 2:
Inquire();
break;
case 3:
book();
break;
case 4:
refund();
break;
case 5:
change();
break;
case 6:
menu();
break;
default :
printf("无此选择(Had no choice)!\n");
}
}
//函数功能:分行输入记录,并且存储到文件中
void Enter()
{
Flight array1[M];
system("cls");
int i,j,n,m;
FILE *fp;
fp=fopen("C:\\c\\flight.dat","wb");
if(fp==NULL)
{
printf("文件错误,无法打开!\n");
printf("File error, unable to open!\n");
exit(1);
}
do
{
printf("输入需要增加的记录数:\n");
printf ("Enter the number of records that need to be added:\n");
scanf("%d",&n);
}
while(n<=0);
{
printf("请输入需要增加的记录:\n");
printf("Please enter the number of records that need to be added:\n");
}
for(i=0; i<n; i++)
{
printf("航班号\t票数\t票价\t起飞地点\t降落地点\t起飞时间\t降落时间\n");
printf("number\ttotal\tprice\torigin\t\tterminal\ttake-of time\tlanding time\n");
scanf("%s %d %lf %s %s %d %d %d %d %d %d %d %d",&array1[i].number,&array1[i].total,&array1[i].price,
array1[i].origin,array1[i].terminal,&array1[i].time[0].month,&array1[i].time[0].day,&array1[i].time[0].hour,
&array1[i].time[0].minute,&array1[i].time[1].month,&array1[i].time[1].day,&array1[i].time[1].hour,
&array1[i].time[1].minute); //依次读入每一条记录的每一个成员
fwrite(&array1[i], sizeof(Flight), 1, fp);
}
fclose(fp);
menu();
}
void planeAdd()
{
system("cls");
int x, k = 1;
Flight planeadd;
FILE *fp;
do
{
printf("\n请按下面提示输入新增航班信息:\n");
printf("请输入航班号:\n");
printf("\nPlease click the following to enter the new flight information:\n");
printf("please enter new number:\n");
scanf("%s", planeadd.number);
printf("请输入机票总数:\n");
printf("please enter total:\n");
scanf("%d", &planeadd.total);
printf("请输入价格:\n");
printf(":please enter prince:\n");
scanf("%lf", &planeadd.price);
printf("请输入起点:\n");
printf("please enter take-off time:\n");
printf("please enter origin:\n");
scanf("%s", planeadd.origin);
printf("请输入终点:\n");
printf("please enter terminal:\n");
scanf("%s", planeadd.terminal);
printf("请输入起飞时间:\n");
scanf("%d%d%d%d",&planeadd.time[0].month,&planeadd.time[0].day,&planeadd.time[0].hour,&planeadd.time[0].minute);
printf("请输入降落时间:\n");
printf("please enter landing time:\n");
scanf("%d%d%d%d",&planeadd.time[1].month,&planeadd.time[1].day,&planeadd.time[1].hour,&planeadd.time[1].minute);
if ((fp = fopen("C:\\c\\flight.dat", "ab+")) == NULL)
{
printf("文件错误,无法打开!\n");
printf("File error, unable to open!\n");
exit(0);
}
fseek(fp, LEN, 2);
fwrite(&planeadd, LEN, 1, fp);
fclose(fp);
printf("\n增加信息成功!!!");
printf("\nincrease information success.");
printf("\n是否继续增加航班?继续请输入1,否则输入0返回主菜单。");
printf("\ncontinue to add a flight enter 1,otherwise enter 0 to return to the main menu.");
scanf("%d", &x);
while (x != 0 && x != 1)
{
printf("\n您的输入有误,请重新输入0或1!\n");
printf("\nyour input is incorrect,please re-enter 0 or 1.\n");
scanf("%d", &x);
}
if (x == 0)
{
menu1();
k = 0;
}
if (x == 1)
k = 1;
} while (k);
}
//函数功能:浏览文件,并将记录存储到数组中
void Browse() //读取文件
{
system("cls");
FILE*fp;
Flight array1[M];
fp=fopen("C:\\c\\flight.dat","rb"); /*读取二进制文件,读方式*/
if(fp==0) /*文件打开失败*/
{
printf("文件错误,无法打开!\n");
exit(1);
}
fread(&array1[0], LEN, 1, fp);
while(!feof(fp))
{
printf("航班号\t票数\t票价\t起飞地点\t降落地点\t起飞时间\t降落时间\n");
printf("%s\t%d\t%.2f\t%s\t\t%s\t\t%d月%d日 %d:%d\t%d月%d日 %d:%d\n",array1[0].number,array1[0].total,array1[0].price,array1[0].origin,
array1[0].terminal,array1[0].time[0].month,array1[0].time[0].day,array1[0].time[0].hour,
array1[0].time[0].minute,array1[0].time[1].month,array1[0].time[1].day,array1[0].time[1].hour,
array1[0].time[1].minute);
fread(&array1[0], LEN, 1, fp);
}
fclose(fp);
menu();
}
void Delete()
{
system("cls");
FILE *fp;
int total = 0;
int i = 0;
int del = 0;
Flight b[M];
char str1[8],str2[15],str3[15];
int month,day,hour,minute;
fp = fopen("C:\\c\\flight.dat", "rb+");
if (fp == NULL)
{
printf("文件有错误不能打开!(A error results in unable opening!)\n");
exit(0);
}
printf("请输入要删除的航班号,起飞地点,降落地点,起飞时间: \n");
printf("Please input the number,origin,terminal,take-of time the to be delete:\n");
scanf("%s%s%s%d%d%d%d",str1,str2,str3,&month,&day,&hour,&minute);
fread(&b[total], LEN, 1, fp);
while (!feof(fp))
{
total++;
fread(&b[total], LEN, 1, fp);
}
for (i = 0; i<total; i++)
if (strcmp(b[i].number,str1)==0&&strcmp(b[i].origin,str2)==0&&strcmp(b[i].terminal,str3)==0)
if(b[i].time[0].month==month&&b[i].time[0].day==day)
if(b[i].time[0].hour==hour&&b[i].time[0].minute==minute)
break;
for (; i<total; i++)
{
b[i]=b[i+1];
total=total-1;
del=1;
}
if(del)
{
printf("\n成功删信息!\n");
printf("\nsuccessful deletion\n");
}
if (del == 0)
{
printf("系统中不存在要删除的航班!\n");
printf("There is no flight to be delete in the system!\n");
menu();
}
fclose(fp);
fp = fopen("C:\\c\\flight.dat", "wb+");
if (fp == NULL)
{
printf("文件有错误不能打开!(A error results in unable opening!)");
exit(0);
}
for (i = 0; i<total; i++)
{
fwrite(&b[i], LEN,1, fp);
}
fclose(fp);
menu();
}
//用户订票,并输出订票后的航班号信息
void book()
{
system("cls");
int num,i;
int sign=0,j=0,total=0;
char str1[8],str2[15],str3[15];
int month,day,hour,minute;
Flight b[M];
FILE *fp;
fp = fopen("C:\\c\\flight.dat", "rb+");
if (fp == NULL)
{
printf("文件有错误不能打开!");
printf("file error!\n");
exit(0);
}
printf("请输入你的航班号、起飞地点、降落地点、起飞时间:\n");
printf("Please input you flight number,take-off place,landing place,take-of time:\n");
scanf("%s%s%s%d%d%d%d",str1,str2,str3,&month,&day,&hour,&minute);
do
{
printf("请输入订票的张数:\n");
printf("Please input the number you want to book:\n");
scanf("%d",&num);
}while(num<=0);
fread(&b[total], LEN, 1, fp);
while (!feof(fp))
{
total++;
fread(&b[total], LEN, 1, fp);
}
for (i=0;i<total;i++)
{
if (strcmp(b[i].number,str1)==0&&strcmp(b[i].origin,str2)==0&&strcmp(b[i].terminal,str3)==0)
if(b[i].time[0].month==month&&b[i].time[0].day==day)
if(b[i].time[0].hour==hour&&b[i].time[0].minute==minute)
{
if(b[i].total<num)
{
printf("非常抱歉!余票不足,请更改!\n");
printf("Sorry!the remain is not enough!Please mend!\n");
menuB();
}
else
{
b[i].total=b[i].total-num;
sign=1;
break;
}
}
}
if(sign)
{
printf("订票成功!\n");
printf("Successful book!\n");
printf("订票之后,该航班的信息如下:\n");
printf("After book,that flight show the following information:\n");
printf("航班号\t票数\t票价\t起飞地点\t降落地点\t起飞时间\t降落时间\n");
printf("flight\total\tprice\ttake-off place\tlanding place\ttake-off time\tlanding time\n");
printf("%s\t%d\t%.2f\t%s\t\t%s\t\t%d月%d日 %d:%d\t%d月%d日 %d:%d\n",b[i].number,b[i].total,b[i].price,
b[i].origin,b[i].terminal,b[i].time[0].month,b[i].time[0].day,b[i].time[0].hour,
b[i].time[0].minute,b[i].time[1].month,b[i].time[1].day,b[i].time[1].hour,b[i].time[1].minute);
}
else
{
printf("您输入的订票航班信息有误!请重新输入!\n\n\n");
printf("The information that you input is fault!Please input again!\n\n\n");
}
fclose(fp);
fp = fopen("C:\\c\\flight.dat", "wb+");
if (fp == NULL)
{
printf("文件有错误不能打开!(A error results in unable opening!)");
exit(0);
}
for (i = 0; i<total; i++)
{
fwrite(&b[i], LEN,1, fp);
}
fclose(fp);
menu();
}
//退票函数
void refund()
{
system("cls");
Flight array1[M];
int num,i;
int total=0,j=0,sign=0;
char str1[8],str2[15],str3[15];
int month,day,hour,minute;
FILE *fp;
fp = fopen("C:\\c\\flight.dat", "rb+");
if (fp == NULL)
{
printf("文件有错误不能打开!");
printf("file error!\n");
exit(0);
}
printf("请输入你的航班号、起飞地点、降落地点、起飞时间:\n");
printf("Please input you flight number,take-off place,landing place,take-of time:\n");
scanf("%s%s%s%d%d%d%d",&str1,&str2,&str3,&month,&day,&hour,&minute);
do
{
printf("请输入退票的张数:\n");
printf("Please input the number you want to refund:\n");
scanf("%d",&num);
}while(num<=0);
fread(&array1[total], LEN, 1, fp);
while (!feof(fp))
{
total++;
fread(&array1[total], LEN, 1, fp);
}
for (i=0;i<total;i++)
{
if (strcmp(array1[i].number,str1)==0&&strcmp(array1[i].origin,str2)==0&&strcmp(array1[i].terminal,str3)==0)
if(array1[i].time[0].month==month&&array1[i].time[0].day==day)
if(array1[i].time[0].month==month&&array1[i].time[0].minute==minute)
{
array1[i].total+=num;
sign=1;
break;
}
}
if(sign)
{
printf("退票成功!\n\n");
printf("Successful refund!\n\n");
printf("航班号\t票数\t票价\t起飞地点\t降落地点\t起飞时间\t降落时间\n");
printf("flight\total\tprice\ttake-off place\tlanding place\ttake-off time\tlanding time\n");
printf("%s\t%d\t%.2f\t%s\t\t%s\t\t%d月%d日 %d:%d\t%d月%d日 %d:%d\n",array1[i].number,array1[i].total,array1[i].price,
array1[i].origin,array1[i].terminal,array1[i].time[0].month,array1[i].time[0].day,array1[i].time[0].hour,
array1[i].time[0].minute,array1[i].time[1].month,array1[i].time[1].day,array1[i].time[1].hour,array1[i].time[1].minute);
}
else
{
printf("您输入的订票航班信息有误!请重新输入!\n\n\n");
printf("The information that you input is fault!Please input again!\n\n\n");
}
fclose(fp);
fp = fopen("C:\\c\\flight.dat", "wb+");
if (fp == NULL)
{
printf("文件有错误不能打开!(A error results in unable opening!)");
exit(0);
}
for (i = 0; i<total; i++)
{
fwrite(&array1[i], LEN,1, fp);
}
fclose(fp);
menu();
}
//改签
void change()
{
system("cls");
Flight array1[M];
int num,i;
int total=0,j=0,sign=0;
char str1[8],str2[15],str3[15];
int month,day,hour,minute;
FILE *fp;
fp = fopen("C:\\c\\flight.dat", "rb+");
if (fp == NULL)
{
printf("文件有错误不能打开!");
printf("file error!\n");
exit(0);
}
printf("请输入你的航班号、起飞地点、降落地点、起飞时间:\n");
printf("Please input you flight number,take-off place,landing place,take-of time:\n");
scanf("%s%s%s%d%d%d%d",str1,str2,str3,&month,&day,&hour,&minute);
do
{
printf("请输入退票的张数:\n");
printf("Please input the number you want to refund:\n");
scanf("%d",&num);
}while(num<=0);
fread(&array1[total], LEN, 1, fp);
while (!feof(fp))
{
total++;
fread(&array1[total], LEN, 1, fp);
}
for (i=0;i<total;i++)
{
if (strcmp(array1[i].number,str1)==0&&strcmp(array1[i].origin,str2)==0&&strcmp(array1[i].terminal,str3)==0)
if(array1[i].time[0].month==month&&array1[i].time[0].day==day)
if(array1[i].time[0].hour==hour&&array1[i].time[0].minute==minute)
{
array1[i].total+=num;
sign=1;
break;
}
}
if(sign)
{
printf("退票成功!\n\n");
printf("Successful refund!\n\n");
printf("航班号\t票数\t票价\t起飞地点\t降落地点\t起飞时间\t降落时间\n");
printf("flight\total\tprice\ttake-off place\tlanding place\ttake-off time\tlanding time\n");
printf("%s\t%d\t%.2f\t%s\t\t%s\t\t%d月%d日 %d:%d\t%d月%d日 %d:%d\n",array1[i].number,array1[i].total,array1[i].price,
array1[i].origin,array1[i].terminal,array1[i].time[0].month,array1[i].time[0].day,array1[i].time[0].hour,
array1[i].time[0].minute,array1[i].time[1].month,array1[i].time[1].day,array1[i].time[1].hour,array1[i].time[1].minute);
}
else
{
printf("您输入的订票航班信息有误!请重新输入!\n\n\n");
printf("The information that you input is fault!Please input again!\n\n\n");
}
printf("请输入你新的航班号、起飞地点、降落地点、起飞时间:\n");
printf("Please input you flight number,take-off place,landing place,take-of time:\n");
scanf("%s%s%s%d%d%d%d",str1,str2,str3,&month,&day,&hour,&minute);
do
{
printf("请输入订票的张数:\n");
printf("Please input the number you want to book:\n");
scanf("%d",&num);
} while(num<=0);
for (i=0;i<total;i++)
{
if (strcmp(array1[i].number,str1)==0&&strcmp(array1[i].origin,str2)==0&&strcmp(array1[i].terminal,str3)==0)
if(array1[i].time[0].month==month&&array1[i].time[0].day==day)
if(array1[i].time[0].hour==hour&&array1[i].time[0].minute==minute)
{
if(array1[i].total<num)
{
printf("非常抱歉!余票不足,请更改!\n");
printf("Sorry!the remain is not enough!Please mend!\n");
menuB();
}
else
{
array1[i].total=array1[i].total-num;
sign=1;
break;
}
}
}
if(sign)
{
printf("订票成功!\n");
printf("Successful book!\n");
printf("订票之后,该航班的信息如下:\n");
printf("After book,that flight show the following information:\n");
printf("航班号\t票数\t票价\t起飞地点\t降落地点\t起飞时间\t降落时间\n");
printf("flight\total\tprice\ttake-off place\tlanding place\ttake-off time\tlanding time\n");
printf("%s\t%d\t%.2f\t%s\t\t%s\t\t%d月%d日 %d:%d\t%d月%d日 %d:%d\n",array1[i].number,array1[i].total,array1[i].price,
array1[i].origin,array1[i].terminal,array1[i].time[0].month,array1[i].time[0].day,array1[i].time[0].hour,
array1[i].time[0].minute,array1[i].time[1].month,array1[i].time[1].day,array1[i].time[1].hour,array1[i].time[1].minute);
}
else
{
printf("您输入的订票航班信息有误!请重新输入!\n\n\n");
printf("The information that you input is fault!Please input again!\n\n\n");
}
fclose(fp);
fp = fopen("C:\\c\\flight.dat", "wb+");
if (fp == NULL)
{
printf("文件有错误不能打开!(A error results in unable opening!)");
exit(0);
}
for (i = 0; i<total; i++)
{
fwrite(&array1[i], LEN,1, fp);
}
fclose(fp);
menu();
}
//函数功能:按照用户指定的方式查询
//希望能够将这以下四种进行排列组合
void Inquire()
{
system("cls");
printf("|-------------------------------------------------------------------|\n");
printf("| 1.按照起点和终点查询 |\n");
printf("|-------------------------------------------------------------------|\n");
printf("| 2.按照起飞时间查询 |\n");
printf("|-------------------------------------------------------------------|\n");
printf("| 3.按照降落时间查询 |\n");
printf("|-------------------------------------------------------------------|\n");
printf("| 4.按照票价查询 |\n");
printf("|-------------------------------------------------------------------|\n");
printf("| 1.Inquire by the beginning and ending place. |\n");
printf("|-------------------------------------------------------------------|\n");
printf("| 2.Inquire by the take-off time |\n");
printf("|-------------------------------------------------------------------|\n");
printf("| 3.Inquire by the landing time |\n");
printf("|-------------------------------------------------------------------|\n");
printf("| 4.Inquire by the price |\n");
printf("|-------------------------------------------------------------------|\n");
int choice;
printf("请输入您的选择\n");
printf("Please input your choice:\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:InquireByPlace();
break;
case 2:InquireByTime1();
break;
case 3:InquireByTime2();
break;
case 4:InquireByPrice();
break;
default:
printf("输入命令错误,请重新输入!\n");
printf("The order is fault!Please input again!\n\n");
break;
}
}
//函数功能:按照起点和终点查询
void InquireByPlace()
{
system("cls");
Flight array1[M];
int i,j,k,num=0;//i,j,k控制循环查找,num表示顾客拥有的选择总数
char origin[15],terminal[15];
int total=0;
FILE *fp;
fp = fopen("C:\\c\\flight.dat", "rb+");
if (fp == NULL)
{
printf("文件有错误不能打开!");
printf("file error!\n");
exit(0);
}
fread(&array1[total], LEN, 1, fp);
while (!feof(fp))
{
total++;
fread(&array1[total], LEN, 1, fp);
}
printf("请输入起点和终点:\n");
printf("Please input the beginning and ending place:\n\n");
scanf("%s%s",origin,terminal);
for(i=0;i<total;i++)//外层循环,所有航班查询一遍
{
if(strcmp(array1[i].origin,origin)==0&&strcmp(array1[i].terminal,terminal)==0)//如果查询到起点,则继续查询终点是否存在
{
num++;
printf("%s\t%d\t%.2f\t%s\t\t%s\t\t%d月%d日 %d:%d\t%d月%d日 %d:%d\n",array1[i].number,array1[i].total,array1[i].price,array1[i].origin,
array1[i].terminal,array1[i].time[0].month,array1[i].time[0].day,array1[i].time[0].hour,
array1[i].time[0].minute,array1[i].time[1].month,array1[i].time[1].day,array1[i].time[1].hour,
array1[i].time[1].minute);
//break;
}
}
printf("\n");
if(num>0)
{
printf("您总计有%d种选择!\n",num);
printf("You have %d opts together!\n\n",num);
}
else
{
printf("非常抱歉!没有查询到所需航班!\n");
printf("Sorry!Cannot inquire the flight in need!\n\n",num);
}
menu();
}
//函数功能:按照飞行的起始时间查询,希望能够输出一刻钟之内的所有航班
void InquireByTime1()
{
system("cls");
Flight array1[M];
int i,j,k,num=0;//i,j,k控制循环查找,num表示顾客拥有的选择总数
Time origin;//表示时间的数组
int total=0;
FILE *fp;
fp = fopen("C:\\c\\flight.dat", "rb+");
if (fp == NULL)
{
printf("文件有错误不能打开!");
printf("file error!\n");
exit(0);
}
fread(&array1[total], LEN, 1, fp);
while (!feof(fp))
{
total++;
fread(&array1[total], LEN, 1, fp);
}
printf("请输入起飞时间:\n");
printf("Please input the take-off time:\n\n",num);
scanf("%d%d%d%d",&origin.month,&origin.day,&origin.hour,&origin.minute);
for(i=0;i<total;i++)//外层循环,所有航班查询一遍for(i=0;i<count;i++)//外层循环,所有航班查询一遍
{
Time item1=array1[i].time[0];
if(TimeDiffer(item1,origin))//字符串比较大小用函数,并且如果是字符串数组只需要写上数组名
{
num++;
printf("%d-%d-%d:%d-------\n",origin.month,origin.day,origin.hour,origin.minute);
printf("%s\t%d\t%.2f\t%s\t\t%s\t\t%d月%d日 %d:%d\t%d月%d日 %d:%d\n",array1[i].number,array1[i].total,array1[i].price,array1[i].origin,
array1[i].terminal,array1[i].time[0].month,array1[i].time[0].day,array1[i].time[0].hour,
array1[i].time[0].minute,array1[i].time[1].month,array1[i].time[1].day,array1[i].time[1].hour,
array1[i].time[1].minute);
}
}
printf("\n");
if(num>0)
{
printf("在您所预期的前后半小时之内,您总计有%d种选择!\n",num);
printf("Between half an hour ,you have %d opts together!\n\n",num);
}
else
{
printf("非常抱歉!没有查询到所需航班!\n");
printf("Sorry!Cannot inquire the flight in need!\n\n",num);
}
menu();
}
//函数功能:按照飞行的降落时间查询
void InquireByTime2()
{
system("cls");
Flight array1[M];
int i,j,k,num=0;
Time terminal;//表示时间的数组
int total=0;
FILE *fp;
fp = fopen("C:\\c\\flight.dat", "rb+");
if (fp == NULL)
{
printf("文件有错误不能打开!");
printf("file error!\n");
exit(0);
}
fread(&array1[total], LEN, 1, fp);
while (!feof(fp))
{
total++;
fread(&array1[total], LEN, 1, fp);
}
printf("请输入降落时间:\n");
printf("Please input the landing time:\n\n",num);
scanf("%d%d%d%d",&terminal.month,&terminal.day,&terminal.hour,&terminal.minute);
for(i=0;i<total;i++)//外层循环,所有航班查询一遍for(i=0;i<count;i++)//外层循环,所有航班查询一遍
{
Time item2=array1[i].time[1];
if(TimeDiffer(item2,terminal))//字符串比较大小用函数,并且如果是字符串数组只需要写上数组名
{
num++;
printf("%d-%d-%d:%d-------\n",terminal.month,terminal.day,terminal.hour,terminal.minute);
printf("%s\t%d\t%.2f\t%s\t\t%s\t\t%d月%d日 %d:%d\t%d月%d日 %d:%d\n",array1[i].number,array1[i].total,array1[i].price,array1[i].origin,
array1[i].terminal,array1[i].time[0].month,array1[i].time[0].day,array1[i].time[0].hour,
array1[i].time[0].minute,array1[i].time[1].month,array1[i].time[1].day,array1[i].time[1].hour,
array1[i].time[1].minute);
}
}
printf("\n");
if(num>0)
{
printf("在您所预期的前后半小时之内,您总计有%d种选择!\n",num);
printf("Between half an hour ,you have %d opts together!\n\n",num);
}
else
{
printf("非常抱歉!没有查询到所需航班!\n");
printf("Sorry!Cannot inquire the flight in need!\n\n",num);
}
menu();
}
//函数功能:按照价格查询
void InquireByPrice()
{
system("cls");
Flight array1[M];
double low,high;
int num=0;
int i;//i必须是int型
int total=0;
FILE *fp;
fp = fopen("C:\\c\\flight.dat", "rb+");
if (fp == NULL)
{
printf("文件有错误不能打开!");
printf("file error!\n");
exit(0);
}
fread(&array1[total], LEN, 1, fp);
while (!feof(fp))
{
total++;
fread(&array1[total], LEN, 1, fp);
}
printf("请输入您预期的价格区间:\n");
printf("Please input the expected price range:\n\n",num);
scanf("%lf%lf",&low,&high);
for(i=0;i<total;i++)
{
if((array1[i].price>=low)&&(array1[i].price<=high))
{
num++;
printf("%.2lf-----%.2lf\n",low,high);
printf("%s\t%d\t%.2f\t%s\t\t%s\t\t%d月%d日 %d:%d\t%d月%d日 %d:%d\n",array1[i].number,array1[i].total,array1[i].price,array1[i].origin,
array1[i].terminal,array1[i].time[0].month,array1[i].time[0].day,array1[i].time[0].hour,
array1[i].time[0].minute,array1[i].time[1].month,array1[i].time[1].day,array1[i].time[1].hour,
array1[i].time[1].minute);
}
}
printf("\n");
if(num>0)
{
printf("您总计有%d种选择!\n",num);
printf("You have %d opts together!\n\n",num);
}
else
{
printf("非常抱歉!没有查询到所需航班!\n");
printf("Sorry!Cannot inquire the flight in need!\n\n",num);
}
menu();
}
//函数功能:依照用户的时间需求,找到前后半小时之内的所有航班
int TimeDiffer(Time item1,Time origin)
{
int i,j,k;
if(item1.month==origin.month)//保证在同一个月
{
if(item1.day==origin.day)//如果在同一天
{
if(item1.hour==origin.hour)//如果在同一个小时之内
{
if(fabs(item1.minute-origin.minute)<=N)//相差十五分钟以内,可以输出
return 1;
else//相差十五分钟以上,不可以输出
return 0;
}
else if((item1.hour-origin.hour)==1)//如果跨小时
{
if((item1.minute+60-origin.minute)<=N)//区分谁的时间比较靠后,大减小
return 1;
else
return 0;
}
else if((origin.hour-item1.hour)==1)//如果跨小时
{
if((origin.minute+60-item1.minute)<=N)
return 1;
else
return 0;
}
else//跨的小时数大于一,则不符合
return 0;
}
//如果相差一天
else if((item1.day-origin.day)==1&&item1.hour==0&&origin.hour==23)
{
if((item1.minute+60-origin.minute)<=15)
return 1;
else
return 0;
}
//如果相差一天
else if((origin.day-item1.day)==1&&item1.hour==23&&origin.hour==0)
{
if((origin.minute+60-item1.minute)<=15)
return 1;
else
return 0;
}
//如果跨两天
else
return 0;
}
//如果跨月
else if((item1.month-origin.month)==1&&item1.day==1&&origin.day==LastDay(origin.month))
{
if((item1.minute+60-origin.minute)<=15)
return 1;
else
return 0;
}
//如果跨月
else if((origin.month-item1.month)==1&&origin.day==1&&item1.day==LastDay(item1.month))
{
if((origin.minute+60-item1.minute)<=15)
return 1;
else
return 0;
}
//如果跨两月
else
return 0;
}
//函数功能:返回一个月的天数即最后一天是多少号
int LastDay(int month)
{
int result;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
result=31;
return result;
case 2:
result=28;
return result;
case 4:
case 6:
case 9:
case 11:
result=30;
return result;
default:
printf("输入不合法!\n");
printf("Illegal input!\n\n");
return 0;
}
}
//打印飞机图形
void Plane()
{
printf(" /---/ \n");
printf(" / / \n");
printf(" / / \n");
printf(" / / \n");
printf(" / / \n");
printf(" / / /---/ \n");
printf(" / / / / \n");
printf(" - - - - - - - - - - - - - - - / / \n");
printf(" / / \n");
printf(" | B 7 3 7 | \n");
printf(" \\ \\ \n");
printf(" - - - - - - - - - - - - - - - \\ \\ \n");
printf(" \\ \\ \\ \\ \n");
printf(" \\ \\ \\---\\ \n");
printf(" \\ \\ \n");
printf(" \\ \\ \n");
printf(" \\ \\ \n");
printf(" \\ \\ \n");
printf(" \\---\\ \n");
}
三、运行结果