飞机票查询系统设计
飞机票信息主要包括但不限于:航班号、起始站、终点站、起飞时间、降落时间、飞行时长、预售票数、已售票数
主要功能:
(1)添加飞机票信息
(2)遍历
(3)按起始站与终点站查询
(4)由起飞时间、降落时间自动计算出飞行时间(单位为分钟)
(5)如果已售空,则删除相应记录
(6)可追加记录
(7)按航班号查询并可选择修改已售票数
(8)从文件读入数据
(9)保存数据到文件
(10)用户登录时需要录入用户名及密码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct air_ticket AIR;
struct air_ticket{
char airline[12]; //航班号
char starpoint[20];//起始站->终点站
char departime[6]; //起飞时间
char landtime[6]; //降落时间
int flightime; //飞行时长
int pre_picket; //预售票数
int out_picket; //已售票数
AIR *next;
};
typedef AIR List;
void ID ();
struct air_ticket* creat(); //链表创建
struct air_ticket* new_c(List *head); //插入飞机票信息
void print_ticket(List *head); //遍历机票信息
void query(List *head,char a[]); //按照起点查询
int fight_time(List *head,char a[]); //计算飞行时长
struct air_ticket* delete_(List *head);//自动删除记录
void teke_back(List *head); //追加删除记录
void qurey_alter(List *head,char a[]); //按航班查询并修改已售票数
int main(){
List *head;
int choice,time;
char a[20];
printf("题目:飞机票查询系统\n");
printf("班级:软工五班\n");
ID ();
printf("|----------------------------------|\n");
printf("| [欢迎使用飞机票查询系统] |\n");
printf("| 1:加载机票信息 |\n");
printf("| 2:插入机票信息 |\n");
printf("| 3:按起始点查询 |\n");
printf("| 4:按航班号查询 |\n");
printf("| 5:计算飞行时长 |\n");
printf("| 6:查看航班信息 |\n");
printf("| 7:删除无票信息 |\n");
printf("| 8:追查删除记录 |\n");
printf("| 0:退出程序 |\n");
printf("|----------------------------------|\n");
do{
printf("请选择要服务类型\n");
scanf("%d",&choice);
switch(choice){
case 1:
head=creat();
break;
case 2:
head=new_c(head);
break;
case 3:
printf("请按照起始站->终点站输入\n");
scanf("%s",a);
query(head,a);
break;
case 4:
printf("请输入航班号\n");
scanf("%s",a);
qurey_alter(head,a);
break;
case 5:
printf("请输入航班号\n");
scanf("%s",a);
time=fight_time(head,a);
printf("你的本次航班共计飞行时长为:");
printf("%d分钟\n",time);
break;
case 6:
print_ticket(head);
break;
case 7:
delete_(head);
printf("已删除无票航班信息\n");
break;
case 8:
teke_back(head);
printf("以上为被清理的信息\n");
break;
case 0:
break;
}
}while(choice!=0);
return 0;
}
struct air_ticket* creat(){ //加载机票信息
int i;
List *head,*tail,*p;
FILE *fp;
char airline[12]; //航班号
char starpoint[20];//起始站->终点站
char departime[6]; //起飞时间
char landtime[6]; //降落时间
int pre_picket; //预售票数
int out_picket; //已售票数
int size=sizeof(struct air_ticket);
head=tail=NULL;
if((fp=fopen("读入数据.txt","r"))==NULL){
printf("File open error!\n");
exit(0);
}
printf("*请输入航班信息\n");
while(!feof(fp)){
p=(List*)malloc(size);
fscanf(fp,"%s%s%s%s%d%d",airline,starpoint,departime,//自动读入数据
landtime,&pre_picket,&out_picket);
printf("----------------------------------------+\n");
printf("%s %s %s %s %d %d \n",airline,starpoint,
departime,landtime,pre_picket,out_picket);
printf("----------------------------------------+\n");
strcpy(p->airline,airline);
strcpy(p->starpoint,starpoint);
strcpy(p->departime,departime);
strcpy(p->landtime,landtime);
p->pre_picket=pre_picket;
p->out_picket=out_picket;
p->next=NULL;
if(head==NULL){
head=p;
}
else
tail->next=p;
tail=p;
}
if(fclose(fp)){
printf("Can not close file\n");
exit(0);
}
return head;
}
struct air_ticket* delete_(List *head){ //删除无票航班信息
List *ptr1,*ptr2;
FILE *fp;
if((fp=fopen("保存数据.txt","w"))==NULL){
printf("file open error!\n");
exit(0);
}
if(head!=NULL&&head->pre_picket<=head->out_picket){
fprintf(fp,"%s %s %s %s %d %d\n",head->airline,head->starpoint,
head->departime,head->landtime,head->pre_picket,head->out_picket);
ptr2=head;
head=head->next;
free(ptr2);
}
if(head==NULL) return NULL;
ptr1=head;
ptr2=head->next;
while(ptr2){
if(ptr2->pre_picket<=ptr2->out_picket){
fprintf(fp,"%s %s %s %s %d %d\n",ptr2->airline,ptr2->starpoint,
ptr2->departime,ptr2->landtime,ptr2->pre_picket,ptr2->out_picket);
ptr1->next=ptr2->next;
free(ptr2);
}
else
ptr1=ptr2;
ptr2=ptr2->next;
}
if(fclose(fp)){
printf("can not close the file\n");
exit(0);
}
return head;
}
void teke_back(List *head){ //追加删除记录
FILE *fp1;
int i;
char airline[12]; //航班号
char starpoint[20];//起始站->终点站
char departime[6]; //起飞时间
char landtime[6]; //降落时间
int pre_picket; //预售票数
int out_picket; //已售票数
if((fp1=fopen("保存数据.txt","r"))==NULL){
printf("Can not close the file!\n");
exit(0);
}
for(i=1;i<=2;i++){
fscanf(fp1,"%s%s%s%s%d%d",airline,starpoint, //从文件读入
departime,landtime,&pre_picket,&out_picket);
printf("%s %s %s %s %d %d\n",airline,starpoint, //输出到屏幕
departime,landtime,pre_picket,out_picket);
}
if(fclose(fp1)) {
printf("can not close the file!\n");
exit(0);
}
}
void qurey_alter(List *head,char a[]){ //按航班查询并修改已售记录
List *p;
int out_picket,choice;
for(p=head;p;p=p->next){
if(strcmp(p->airline,a)==0){
printf("%s %s %s %s %d %d\n",p->airline,p->starpoint,
p->departime,p->landtime,p->pre_picket,p->out_picket);
printf("是否选择修改已售票数\n");
printf("1:是 0:否\n");
printf("请输入1或0\n");
scanf("%d",&choice);
while(choice){
printf("请输入待修改票数\n");
scanf("%d",&out_picket);
p->out_picket=out_picket;
printf("更新之后的机票信息\n");
printf("%s %s %s %s %d %d\n",p->airline,p->starpoint,
p->departime,p->landtime,p->pre_picket,p->out_picket);
break;
}
break;
}
}
}
struct air_ticket* new_c(List *head){ //插入新机票
List *q=NULL;
int choice;
char airline[12]; //航班号
char starpoint[20];//起始站->终点站
char departime[6]; //起飞时间
char landtime[6]; //降落时间
int pre_picket; //预售票数
int out_picket; //已售票数
int size=sizeof(struct air_ticket);
do{
q=(List*)malloc(size);
printf("请输入要插入的机票信息\n");
scanf("%s %s %s %s %d %d",airline,starpoint,departime,
landtime,&pre_picket,&out_picket);
strcpy(q->airline,airline);
strcpy(q->starpoint,starpoint);
strcpy(q->departime,departime);
strcpy(q->landtime,landtime);
q->pre_picket=pre_picket;
q->out_picket=out_picket;
q->next=head;
head=q;
printf("信息插入成功\n");
printf("%s %s %s %s %d %d\n",airline,starpoint,departime,
landtime,pre_picket,out_picket);
printf("是否继续插入信息\n");
printf("1:是 0:否\n");
scanf("%d",&choice);
}while(choice!=0);
return head;
}
int fight_time(List *head,char a[]){ //计算飞行时间
List *ptr1;
int i,j,t,t1,t2,s1=0,s2=0;
int s,x1,x2,x;
for(ptr1=head;ptr1;ptr1=ptr1->next){
if(strcmp(ptr1->airline,a)==0) {
for(i=0,j=0;i<5;i++,j++){
if(i!=2) s1=s1*10+(ptr1->departime[i]-'0');
if(j!=2) s2=s2*10+(ptr1->landtime[j]-'0');
}
t1=s1/100;
t2=s1%100;
t=t1*60+t2;
x1=s2/100;
x2=s2%100;
x=x1*60+x2;
s=x-t;
if(s>=0) s=x-t;
else s=24*60-t+x;
break;
}
}
return s;
}
struct yh {
char hao[20];
char mi[20];
} s[4]= {"000000","000000","王聪聪","111111","郜堂龙",
"222222","夏博","333333"};
void ID ()
{
struct yh *p;
p=s;
int i;
char a[20],b[20];
char id[20],code[20];
int flag=0;
printf("请注册账号\n");
gets(a);
strcpy(p->hao,a);
printf("请注册密码\n");
gets(b);
strcpy(p->mi,b);
p++;
printf ("账 户 名:");
gets (id);
int l=strlen(id);
while (1) {
if (l<4||l>15) {
printf("账户非法!\n");
}
i=0;
while (i<4) {
if (strcmp(id,s[i].hao)==0) {
flag=1;
break;
}
i++;
}
if (flag==1) {
break;
}
printf ("重输账户:");
gets (id);
l=strlen(id);
}
printf ("密 码:");
gets (code);
while (1) {
if (strcmp(code,s[i].mi)==0) {
break;
}
printf ("重输密码:");
gets (code);
}
printf ("\n __________\n");
printf (" / \\\n");
printf (" | 登录成功 |\n");
printf (" \\__________/\n\n");
}
void print_ticket(List *head){ //遍历航班信息
List *ptr;
if(head==NULL){
printf("暂无航班信息\n");
return;
}
printf("以下为航班信息\n");
printf("航班号 起始->终点 起飞时间 降落时间 预售票数 已售票数\n");
for(ptr=head;ptr;ptr=ptr->next){
printf("------------------------------------------------+\n");
printf("%s %s %s %s %d %d \n",ptr->airline,ptr->starpoint,
ptr->departime,ptr->landtime,ptr->pre_picket,ptr->out_picket);
printf("------------------------------------------------+\n");
}
}
void query(List *head,char a[]){ //按起始点查询
List *pr;
int time,flgh=0;
for(pr=head;pr;pr=pr->next){
if(strcmp(pr->starpoint,a)==0){
flgh=1;
printf("%s %s %s %s %d %d\n",pr->airline,pr->starpoint,
pr->departime,pr->landtime,pr->pre_picket,pr->out_picket);
break;
}
}
if(flgh==0){
printf("无此航班信息\n");
}
}