/*.航班管理系统
主界面以菜单的形式展现,用户可以按不同的键执行不同的操作,即调用不同的函数;
......
用不同的函数实现,除以下模块,还可设计其他功能模块:
1)输入记录:输入录入航班信息,包括:航班号,起降时间,起飞抵达城市,航班票价,票价折扣,航班是否满仓等;
2)输出记录:输出信息;
3)查询记录:可根据航班号查找记录;
4)删除记录:指定航班号删除该条记录;
5)插入记录:在指定位置插入新的记录。
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 50
int menu();
int input(struct );
void print(struct );
void search(struct flight_info info[],int n);
int find_id(struct flight_info info[],int n,char *p);
int del(struct,int n);
int add(struct ,int n);
//菜单
int menu(){
int i=1,choie;
// struct flight_info *info;
printf("******************航班管理系统*********************\n");
printf("\t\t%d.输入航班信息\n",i++);
printf("\t\t%d.输出航班信息\n",i++);
printf("\t\t%d.查询记录\n",i++);
printf("\t\t%d.删除记录\n",i++);
printf("\t\t%d.插入记录\n",i++);
printf("\t\t%d.退出系统\n",i++);
printf("****************************************************\n");
do{
printf("请输入你所选功能(1~6):");
scanf("%d",&choie);
}while(choie<0||choie>6);
return choie;
}
//输入航班信息
//航班号,起降时间,起飞抵达城市,航班票价,票价折扣,航班是否满仓
struct time{
int hour;
int minute;
};
struct flight_info{
char id[20]; //航班号
time takeoff; //起飞时间
time landing; //降落时间
char city[20]; //起降城市
float money; //航班票价
float discount;//票价折扣
char full[3];//航班是否满仓
};
int input(struct flight_info info[]){
int count;
system("cls");
printf("\n请输入航班记录条数: ");
scanf("%d",&count);
for(int i=0;i<count;i++){
printf("\n记录号:%d\n",i+1);
for(int j=0;j<20;j++){printf("-");}
printf("\n航班号: ");scanf("%s",info[i].id);
printf("起飞时间(x时x分)");scanf("%d%d",&info[i].takeoff.hour,&info[i].takeoff.minute);
printf("降落时间(x时x分)");scanf("%d%d",&info[i].landing.hour,&info[i].landing.minute);
printf("起降城市");scanf("%s",info[i].city);
printf("航班票价");scanf("%f",&info[i].money);
printf("票价折扣");scanf("%f",&info[i].discount);
printf("是否满仓");scanf("%s",info[i].full);
}
return count;
}
//输出航班信息
void print(struct flight_info info[],int n){
int i;
system("cls");
printf("\n******************航班信息*********************\n");
printf("航班号 起降时间 起降城市 票价 折扣 是否满仓\n");
printf("------------------------------------------------------------------\n");
for(i=0;i<n;i++){
printf("%s\t%d:%d-%d:%d %s\t %.2f\t%.2f\t%s\n",info[i].id,info[i].takeoff.hour,info[i].takeoff.minute,info[i].landing.hour,info[i].landing.minute,info[i].city,info[i].money,info[i].discount,info[i].full);
printf("------------------------------------------------------------------\n");
}
}
//查询记录
void search(struct flight_info info[],int n){
char s[20];
int i;
system("cls");
printf("请输入查找航班号:\n");
scanf("%s",s);
getchar();
i=find_id(info,n,s);
if(i>n-1)
printf("没有航班号为%s的航班!\n",s);
else {
system("cls");
printf("\n******************航班信息*********************\n");
printf("航班号 起降时间 起降城市 票价 折扣 是否满仓\n");
printf("------------------------------------------------------------------\n");
printf("%s\t%d:%d-%d:%d\t%s\t%.2f\t%.2f\t%s\n",info[i].id,info[i].takeoff.hour,info[i].takeoff.minute,info[i].landing.hour,info[i].landing.minute,info[i].city,info[i].money,info[i].discount,info[i].full);
printf("------------------------------------------------------------------\n");
}
}
//删除记录
int del(struct flight_info info[],int n){
char s[20];
int ch=0;
int i;
printf("请输入要删除的航班号:\n");
scanf("%s",s);
getchar();
i=find_id(info,n,s);
if(i>n-1)
printf("没有航班号为%s的航班!\n",s);
else {
printf("正在删除中!\n");
for(;i<n;i++){ //从删除位置开始依次前移航班记录
strcpy(info[i].id,info[i+1].id);
info[i].takeoff.hour=info[i+1].takeoff.hour;
info[i].takeoff.minute=info[i+1].takeoff.minute;
info[i].landing.hour=info[i+1].landing.hour;
info[i].landing.minute=info[i+1].landing.minute;
strcpy(info[i].city,info[i+1].city);
info[i].money=info[i+1].money;
info[i].discount=info[i+1].discount;
strcpy(info[i].full,info[i+1].full);
}
n--; //删除后记录数减1
printf("删除成功!\n");
}
return n;
}
//插入记录
int add(struct flight_info info[],int n){
struct flight_info temp; //新插入记录信息
int i,j;
char s[20];
printf("请输入要插入的航班记录:\n");
printf("**************************************************");
printf("\n航班号: ");scanf("%s",temp.id);
printf("起飞时间(x时x分)");scanf("%d%d",&temp.takeoff.hour,&temp.takeoff.minute);
printf("降落时间(x时x分)");scanf("%d%d",&temp.landing.hour,&temp.landing.minute);
printf("起降城市");scanf("%s",temp.city);
printf("航班票价");scanf("%f",&temp.money);
printf("票价折扣");scanf("%f",&temp.discount);
printf("是否满仓");scanf("%s",temp.full);
//输入插入信息
getchar();
printf("------------------------------------------\n");
if(n>0){
printf("请输入插入位置的航班号,将新纪录插入在该航班号前:\n");
scanf("%s",s); //输入插入位置的姓名
getchar();
i=find_id(info,n,s); //确定插入位置
}
else
i=0;
for(j=n-1;j>=i;j--){
strcpy(info[j+1].id,info[j].id);
info[j+1].takeoff.hour=info[j].takeoff.hour;
info[j+1].takeoff.minute=info[j].takeoff.minute;
info[j+1].landing.hour=info[j].landing.hour;
info[j+1].landing.minute=info[j].landing.minute;
strcpy(info[j+1].city,info[j].city);
info[j+1].money=info[j].money;
info[j+1].discount=info[j].discount;
strcpy(info[j+1].full,info[j].full);
}
strcpy(info[i].id,temp.id);
info[i].takeoff.hour=temp.takeoff.hour;
info[i].takeoff.minute=temp.takeoff.minute;
info[i].landing.hour=temp.landing.hour;
info[i].landing.minute=temp.landing.minute;
strcpy(info[i].city,temp.city);
info[i].money=temp.money;
info[i].discount=temp.discount;
strcpy(info[i].full,temp.full);
n++; //记录数加1
return n;//返回记录数
}
//按航班号查找函数
int find_id(flight_info info[],int n,char *p){
int i;
for(i=0;i<n;i++){
if(strcmp(p,info[i].id)==0)
return i;
}
return i;
}
int main(){
int length;
flight_info info[N];
for(;;){
switch(menu()){
case 1:length=input(info);break;
case 2:print(info,length);break;
case 3:search(info,length);break;
case 4:length=del(info,length);break;
case 5:length=add(info,length);break;
case 6:system("cls");printf("系统已关闭!!!\n感谢使用\n");exit(0);break;
}
printf("按回车键返回主菜单...\n");
getchar();
}
return 0;
}