📄 我的航空系统.cpp
字号:
//民航系统
//A03(3)班 16号 陈红志
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
#include<stdlib.h>
//数据结构//
struct airticket
{
int IDcard;
int num;
int day;
char time[50];
char count[50];
char start[50];
char ends[50];
airticket *next;
};
void main();
FILE *fp;
airticket * head=NULL;
void fn();//输出函数
airticket * Create()//创建链表
{
airticket * ps;
airticket * pEnd;
ps=new airticket;
cout<<"航班号:";
cin>>ps->num;
cout<<"日期:";
cin>>ps->day;
cout<<"时间: ";
cin>>ps->time;
cout<<"身份证号:";
cin>>ps->IDcard;
cout<<"票数: ";
cin>>ps->count;
cout<<"起点:";
cin>>ps->start;
cout<<"终点:";
cin>>ps->ends;
cout<<endl<<endl;
pEnd=ps;
while(ps->num!=0)
{
if(head==NULL)
head=ps;
else
pEnd->next=ps;
pEnd=ps;
ps=new airticket;
cout<<"航班号(航班号位0结束输入):";
cin>>ps->num;
cout<<"日期: ";
cin>>ps->day;
cout<<"时间: ";
cin>>ps->time;
cout<<"身份证号:";
cin>>ps->IDcard;
cout<<"票数: ";
cin>>ps->count;
cout<<"起点:";
cin>>ps->start;
cout<<"终点:";
cin>>ps->ends;
cout<<endl<<endl;
}
pEnd->next=NULL;
return head;
}
void save(airticket * head1)//保存信息
{
if((fp=fopen("1.txt","ab"))==NULL)
{
return;
}
for(;head1;head1=head1->next)
fwrite(head1,sizeof(airticket),1,fp);
fclose(fp);
}
void dingpiao()//功能1:订票
{
airticket * head1=Create();
save(head1);
}
void tuipiao()//功能2:退票
{
airticket *tail, *temp=new airticket;
head=NULL;
if((fp=fopen("1.txt","rb"))==NULL)
{
printf("出错,请检查文件是否存在,按任意键返回住菜单");//出错提示
getchar();
fn();
}
while((fread(temp,sizeof(airticket),1,fp))==1)
{
if(head==NULL){head=tail=temp;head->next=NULL;}
else
{
temp->next=NULL;
tail->next=temp;
tail=temp;
}
temp=new airticket;
}
fclose(fp);
int num;
if(!head)
{
cout<<"\n数据为空!\n";return;
}
temp=tail=head;
cout<<"请输入想退机票的航班号:";
cin>>num;
for(;tail;tail=tail->next)
{
if(head->num==num){head=tail->next;}
else if(tail->num==num)
{
temp->next=tail->next;
cout<<"你已经成功完成退票"<<endl;
}
temp=tail;
}
if((fp=fopen("1.txt","wb"))==NULL)return;
for(;head;head=head->next)
fwrite(head,sizeof(airticket),1,fp);
fclose(fp);
}
void watch()//功能3:查看信息
{
airticket *tail, *temp=new airticket;
head=tail=NULL;
int flag=1;
if((fp=fopen("1.txt","rb"))==NULL)
{
printf("出错,请检查文件是否存在,按任意键返回住菜单\n");//出错提示
main();
}
while((fread(temp,sizeof(struct airticket),1,fp))==1)
{
if(head==NULL){head=tail=temp;head->next=NULL;}
else
{
temp->next=NULL;
tail->next=temp;
tail=temp;
}
cout<<endl;
cout<<"航班号:"<<tail->num<<endl;
cout<<"日期:"<<tail->day<<endl;
cout<<"时间(星期):"<<tail->time<<endl;
cout<<"身份证号:"<<tail->IDcard<<endl;
cout<<"票数:"<<tail->count<<endl;
cout<<"起点站:"<<tail->start<<endl;
cout<<"终点站:"<<tail->ends<<endl;
temp=new airticket;
}
delete tail,temp;
fclose(fp);
}
void search_num()//查找航班号
{
int num;
int k=1;
airticket *tail, *temp=new airticket;
head=NULL;
if((fp=fopen("1.txt","rb"))==NULL)
{
printf("出错,请检查文件是否存在,按任意键返回住菜单");//出错提示
getchar();
fn();
}
while((fread(temp,sizeof(airticket),1,fp))==1)
{
if(head==NULL){head=tail=temp;head->next=NULL;}
else
{
temp->next=NULL;
tail->next=temp;
tail=temp;
}
temp=new airticket;
}
cout<<"请输入所要查找航班号:"<<endl;
cin>>num;
if(head==NULL)
{
printf("没有你所查找的信息!");fn();
}
for(;head;head=head->next)
if(head->num==num)
{
cout<<endl<<"找到相匹配的信息"<<endl;
cout<<"航班号是:"<<head->num<<","<<"日期是:"<<head->day<<",";
cout<<"时间是(星期):"<<head->time<<",";
cout<<"身份证号是:"<<head->IDcard<<","<<"票数是:"<<head->count<<",";
cout<<"起点站是:"<<head->start<<","<<"终点站是:"<<head->ends<<endl;
}
delete tail,temp;
fclose(fp);
cout<<endl<<"是否继续?是请按1,否请按2."<<endl;
cin>>k;
switch(k)
{
case 1:
search_num();break;
case 2:
//fn();break;
default:cout<<endl<<"按键错误!请重新按键!"<<endl;
}
}
void search_day()//查找航班日期
{
int day,m;
airticket *tail, *temp=new airticket;
head=NULL;
if((fp=fopen("1.txt","rb"))==NULL)
{
cout<<"出错,请检查文件是否存在,按任意键返回住菜单"<<endl;//出错提示
getchar();
fn();
}
while((fread(temp,sizeof(airticket),1,fp))==1)
{
if(head==NULL){head=tail=temp;head->next=NULL;}
else
{
temp->next=NULL;
tail->next=temp;
tail=temp;
}
temp=new airticket;
}
cout<<"请输入所要查找的飞行时间:"<<endl;
cin>>day;
if(head==NULL)
{
cout<<endl;
cout<<"没有你所查找的信息!"<<endl;
}
for(;head;head=head->next)
if(head->day==day)
{
cout<<"找到相匹配的信息"<<endl;
cout<<"航班号是:"<<head->num<<","<<"日期是:"<<head->day<<",";
cout<<"时间(星期)是:"<<head->time<<",";
cout<<"身份证号:"<<head->IDcard<<","<<"票数是:"<<head->count<<",";
cout<<"起点站是:"<<head->start<<","<<"终点站是:"<<head->ends<<endl<<endl;
}
delete tail,temp;
fclose(fp);
cout<<endl<<"是否继续?是(按1),否(按2):";
cin>>m;
switch(m)
{ case 1:
search_day();break;
case 2:
fn();break;
default:cout<<"按键错误!请重新按键!"<<endl;
}
}
void browseticket() //功能4:查询航线
{
int i;
cout<<"请选择按键 "<<endl;
cout<<endl<<"按航班号查询请按1,按日期查询请按2,返回请按0."<<endl;
cin>>i;
switch(i)
{ case 1:
search_num();break;
case 2:
search_day();break;
case 0:
fn();break;
default:cout<<"按键错误!请重新按键!"<<endl;
}
fn();
}
void change()//功能5:修改航班信息(当修改信息不存在时,不运行)
{
int num;
int i=0;
airticket temp;
if((fp=fopen("1.txt","rb+"))==NULL)
{
printf("出错,请检查文件是否存在,按任意键返回住菜单");//出错提示
}
cout<<"请输入所要修改的航班号:"<<endl;
cin>>num;
while((fread(&temp,sizeof(airticket),1,fp))==1)
{
i++;
if(temp.num==num)
{
cout<<"修改相应的航班信息:"<<endl;
cout<<"修改后航班号是:";
cin>>temp.num;
cout<<"修改后日期是:";
cin>>temp.day;
cout<<"修改后时间(星期)是:";
cin>>temp.time;
cout<<"修改后的身份证号:";
cin>>temp.IDcard;
cout<<"修改后票数是:";
cin>>temp.count;
cout<<"修改后起点站是:";
cin>>temp.start;
cout<<"修改后终点站是:";
cin>>temp.ends;
fseek(fp,(i-1)*sizeof(airticket),0);
fwrite(&temp,sizeof(airticket),1,fp);
fseek(fp,0,0);
}
}
fclose(fp);
}
void fn()//输出信息函数
{
int i=1;
while(i)
{
cout<<endl<<endl;
cout<<"输入航班信息:"<<endl<<endl; //打印提示信息
cout<<" 1 订购机票(航班号为0结束输入)"<<endl;
cout<<" 2 返退机票"<<endl;
cout<<" 3 查看航班信息"<<endl;
cout<<" 4 查询航班信息"<<endl;
cout<<" 5 修改航班信息"<<endl;
cout<<" 0 返回主界面 "<<endl;
cout<<"请选择按键(0-5):";
cin>>i;
cout<<endl;
if(i>=0&&i<=5)
{
switch(i)
{
case 1:dingpiao();break;
case 2:tuipiao();break;
case 3:watch();break;
case 4:browseticket();break;
case 5:change();break;
case 0:
{
cout<<"谢谢使用! "<<endl;
};break;
}
}
else
cout<<"按键错误!请重新按键!"<<endl;
cout<<endl;
}
}
//主函数
void main()
{ int j=1;
while(j)
{
cout<<" $---------------------------------$"<<endl;
cout<<" | |"<<endl;
cout<<" | **欢迎进入中国民航管理系统** |"<<endl;
cout<<" | |"<<endl;
cout<<" @---------------------------------@"<<endl<<endl;
cout<<"管理员进入请按1,客户进入请按2: "<<endl;
cin>>j;
if(j>=1&&j<=2)
{
switch(j)
{
case 1:
{
char f[]="2005"; //设置密码
printf("请输入密码并以回车键结束(密码是2005):\n\n");
scanf("%s",f); //读取密码
if(strcmp(f,"2005")==0)fn();//密码正确是时进入系统
else
cout<<"密码错误!请重新选择按键!"<<endl;
};break;
case 2:
{
cout<<"^_^客户能查看和查询航空信息^_^"<<endl;
cout<<endl<<"按1查看信息,按2查询信息:";
int m;
cin>>m;
if(m>=1&&m<=2)
{ switch(m)
{case 1:
watch();break;
case 2:
{
int num;
int k=1;
airticket *tail, *temp=new airticket;
head=NULL;
if((fp=fopen("1.txt","rb"))==NULL)
{
printf("出错,请检查文件是否存在,按任意键返回住菜单");//出错提示
getchar();
fn();
}
while((fread(temp,sizeof(airticket),1,fp))==1)
{
if(head==NULL){head=tail=temp;head->next=NULL;}
else
{
temp->next=NULL;
tail->next=temp;
tail=temp;
}
temp=new airticket;
}
cout<<endl<<"请输入所要查找航班号:"<<endl;
cin>>num;
if(head==NULL)
{
printf("没有你所查找的信息!");fn();
}
for(;head;head=head->next)
if(head->num==num)
{
cout<<endl<<"找到相匹配的信息"<<endl;
cout<<"航班号是:"<<head->num<<","<<"日期是:"<<head->day<<",";
cout<<"时间是(星期):"<<head->time<<",";
cout<<"身份证号是:"<<head->IDcard<<","<<"票数是:"<<head->count<<",";
cout<<"起点站是:"<<head->start<<","<<"终点站是:"<<head->ends<<endl;
}
delete tail,temp;
fclose(fp);
cout<<endl<<"是否继续?是请按1,否请按2."<<endl;
cin>>k;
switch(k)
{
case 1:
search_num();break;
case 2:
main();break;
default:cout<<"按键错误!请重新按键!"<<endl;
}
};break;
}
}else{cout<<"按键错误!请重新选择按键!"<<endl;};
} break;
}
}
else
{
cout<<"按键错误!请重新选择按键!"<<endl;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -