?? czh.cpp
字號:
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
#include<stdlib.h>
//數(shù)據(jù)結(jié)構(gòu)//
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();//輸出函數(shù)
airticket * Create()//創(chuàng)建鏈表
{
airticket * ps;
airticket * pEnd;
ps=new airticket;
cout<<"航班號:";
cin>>ps->num;
cout<<"日期:";
cin>>ps->day;
cout<<"時(shí)間: ";
cin>>ps->time;
cout<<"身份證號:";
cin>>ps->IDcard;
cout<<"票數(shù): ";
cin>>ps->count;
cout<<"起點(diǎn):";
cin>>ps->start;
cout<<"終點(diǎn):";
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結(jié)束輸入):";
cin>>ps->num;
cout<<"日期: ";
cin>>ps->day;
cout<<"時(shí)間: ";
cin>>ps->time;
cout<<"身份證號:";
cin>>ps->IDcard;
cout<<"票數(shù): ";
cin>>ps->count;
cout<<"起點(diǎn):";
cin>>ps->start;
cout<<"終點(diǎn):";
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("出錯(cuò),請檢查文件是否存在,按任意鍵返回住菜單");//出錯(cuò)提示
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數(shù)據(jù)為空!\n";return;
}
temp=tail=head;
cout<<"請輸入想退機(jī)票的航班號:";
cin>>num;
for(;tail;tail=tail->next)
{
if(head->num==num){head=tail->next;}
else if(tail->num==num)
{
temp->next=tail->next;
cout<<"你已經(jīng)成功完成退票"<<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("出錯(cuò),請檢查文件是否存在,按任意鍵返回住菜單\n");//出錯(cuò)提示
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<<"時(shí)間(星期):"<<tail->time<<endl;
cout<<"身份證號:"<<tail->IDcard<<endl;
cout<<"票數(shù):"<<tail->count<<endl;
cout<<"起點(diǎn)站:"<<tail->start<<endl;
cout<<"終點(diǎn)站:"<<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("出錯(cuò),請檢查文件是否存在,按任意鍵返回住菜單");//出錯(cuò)提示
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<<"時(shí)間是(星期):"<<head->time<<",";
cout<<"身份證號是:"<<head->IDcard<<","<<"票數(shù)是:"<<head->count<<",";
cout<<"起點(diǎn)站是:"<<head->start<<","<<"終點(diǎn)站是:"<<head->ends<<endl;
}
delete tail,temp;
fclose(fp);
cout<<endl<<"是否繼續(xù)?是請按1,否請按2."<<endl;
cin>>k;
switch(k)
{
case 1:
search_num();break;
case 2:
//fn();break;
default:cout<<endl<<"按鍵錯(cuò)誤!請重新按鍵!"<<endl;
}
}
void search_day()//查找航班日期
{
int day,m;
airticket *tail, *temp=new airticket;
head=NULL;
if((fp=fopen("1.txt","rb"))==NULL)
{
cout<<"出錯(cuò),請檢查文件是否存在,按任意鍵返回住菜單"<<endl;//出錯(cuò)提示
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<<"請輸入所要查找的飛行時(shí)間:"<<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<<"時(shí)間(星期)是:"<<head->time<<",";
cout<<"身份證號:"<<head->IDcard<<","<<"票數(shù)是:"<<head->count<<",";
cout<<"起點(diǎn)站是:"<<head->start<<","<<"終點(diǎn)站是:"<<head->ends<<endl<<endl;
}
delete tail,temp;
fclose(fp);
cout<<endl<<"是否繼續(xù)?是(按1),否(按2):";
cin>>m;
switch(m)
{ case 1:
search_day();break;
case 2:
fn();break;
default:cout<<"按鍵錯(cuò)誤!請重新按鍵!"<<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<<"按鍵錯(cuò)誤!請重新按鍵!"<<endl;
}
fn();
}
void change()//功能5:修改航班信息(當(dāng)修改信息不存在時(shí),不運(yùn)行)
{
int num;
int i=0;
airticket temp;
if((fp=fopen("1.txt","rb+"))==NULL)
{
printf("出錯(cuò),請檢查文件是否存在,按任意鍵返回住菜單");//出錯(cuò)提示
}
cout<<"請輸入所要修改的航班號:"<<endl;
cin>>num;
while((fread(&temp,sizeof(airticket),1,fp))==1)
{
i++;
if(temp.num==num)
{
cout<<"修改相應(yīng)的航班信息:"<<endl;
cout<<"修改后航班號是:";
cin>>temp.num;
cout<<"修改后日期是:";
cin>>temp.day;
cout<<"修改后時(shí)間(星期)是:";
cin>>temp.time;
cout<<"修改后的身份證號:";
cin>>temp.IDcard;
cout<<"修改后票數(shù)是:";
cin>>temp.count;
cout<<"修改后起點(diǎn)站是:";
cin>>temp.start;
cout<<"修改后終點(diǎn)站是:";
cin>>temp.ends;
fseek(fp,(i-1)*sizeof(airticket),0);
fwrite(&temp,sizeof(airticket),1,fp);
fseek(fp,0,0);
}
}
fclose(fp);
}
void fn()//輸出信息函數(shù)
{
int i=1;
while(i)
{
cout<<endl<<endl;
cout<<"輸入航班信息:"<<endl<<endl; //打印提示信息
cout<<" 1 訂購機(jī)票(航班號為0結(jié)束輸入)"<<endl;
cout<<" 2 返退機(jī)票"<<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<<"按鍵錯(cuò)誤!請重新按鍵!"<<endl;
cout<<endl;
}
}
//主函數(shù)
void main()
{ int j=1;
while(j)
{
cout<<" $---------------------------------$"<<endl;
cout<<" | |"<<endl;
cout<<" | **歡迎進(jìn)入中國民航管理系統(tǒng)** |"<<endl;
cout<<" | |"<<endl;
cout<<" @---------------------------------@"<<endl<<endl;
cout<<"管理員進(jìn)入請按1,客戶進(jìn)入請按2: "<<endl;
cin>>j;
if(j>=1&&j<=2)
{
switch(j)
{
case 1:
{
char f[]="czh"; //設(shè)置密碼
printf("請輸入密碼并以回車鍵結(jié)束:\n\n");
scanf("%s",f); //讀取密碼
if(strcmp(f,"czh")==0)fn();//密碼正確是時(shí)進(jìn)入系統(tǒng)
else
cout<<"密碼錯(cuò)誤!請重新選擇按鍵!"<<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("出錯(cuò),請檢查文件是否存在,按任意鍵返回住菜單");//出錯(cuò)提示
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<<"時(shí)間是(星期):"<<head->time<<",";
cout<<"身份證號是:"<<head->IDcard<<","<<"票數(shù)是:"<<head->count<<",";
cout<<"起點(diǎn)站是:"<<head->start<<","<<"終點(diǎn)站是:"<<head->ends<<endl;
}
delete tail,temp;
fclose(fp);
cout<<endl<<"是否繼續(xù)?是請按1,否請按2."<<endl;
cin>>k;
switch(k)
{
case 1:
search_num();break;
case 2:
main();break;
default:cout<<"按鍵錯(cuò)誤!請重新按鍵!"<<endl;
}
};break;
}
}else{cout<<"按鍵錯(cuò)誤!請重新選擇按鍵!"<<endl;};
} break;
}
}
else
{
cout<<"按鍵錯(cuò)誤!請重新選擇按鍵!"<<endl;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -