以前网上找的控制台版的航空订票系统,用链表实现的,但是原版那个的指针有几处使用是错误的。这里已经修正,算是一个比较完善的版本吧。
数据是保存在三个TXT文件里的。给初学者学习用。别照搬了。
#include < iostream.h >
#include < process.h >
#include < string .h >
#include < conio.h >
#include < stdio.h >
#include < iomanip.h >
#include < fstream.h >
#define MAX 50
#define NULL 0
class Customer
... {
public:
char Name[7]; //姓名
int Amount; //定票数
int Rank; //舱位等级
int Seat_No; //座位号
Customer *Next;
} ;
class Replace
... {
public:
char Name[7]; //姓名
int Amount; //定票数
int Rank; //舱位等级
int Seat_No; //座位号
Replace *Next;
} ;
class Flight
... {
public:
char Des_Name[10]; //终点站名
char Flight_No[6]; //航班号
char Plane_No[6]; //飞机号
int Week_Day; //飞行周日
int Customer_Amount; //乘员定额
int Free_Amount; //剩余票数
float Price[3]; //舱位等级的价格
Replace *ReplName; //该航班的候补乘客名单
Customer *CustName; //该航班的订票乘客名单
int CustNo; //该航班的已定票乘客数量
int ReplNo; //该航班的候补乘客数量
Flight *Next; //指示下一航线结点
} ;
int Customer_Count = 0 ; // 所有航线的定票乘客总数
Flight FL; // 对象声明,用于文件写入时的引用
Flight * Head; // 航线头指针
Flight * p2; // 航线结点指针
Customer * Custp1[MAX]; // 各条航线乘客结点指针
Customer CS; // 对象声明,用于文件写入时的引用
Replace * Replp1[MAX]; // 各条航线候补结点指针
Replace RP; // 对象声明,用于文件写入时的引用
int IsEmpty = 1 ; // 是否有定票乘客
int IsReplace = 1 ; // 是否有候补乘客
Customer * prior; // 满足要求的定票乘客的前结点,以作删除操作
// int Find_Line(Flight *,char *);
// int Find_Line(Flight *,char *,Flight &,int);
// void Line_See();
// ---------------菜单模板函数---------------
char ModelMenu( char * s[], int Itemcount)
... {
int i;
char choose;
system("cls");
cout<<" _ "<<endl;
cout<<" | / "<<endl;
cout<<" _| /______________________________________ "<<endl;
cout<<" - ______ ________________ /_/_`, "<<endl;
cout<<" -(_________ -======- ChinaAir ) "<<endl;
cout<<" `------=============----------------` "<<endl;
cout<<" ================================"<<endl;
cout<<" "<<s[0]<<endl;
cout<<" ================================"<<endl;
cout<<" "<<endl;
cout<<" ======================== "<<endl;
for(i=1;i<Itemcount;i++)
...{
cout<<" "<<s[i]<<endl;
if(i+1!=Itemcount)
cout<<" ======================== "<<endl;
}
cout<<" ======================== "<<endl;
cout<<" ================================"<<endl;
cout<<" 请输入菜单项前的操作代码:";
cin>>choose;
return choose;
}
// ---------------主菜单函数---------------
char MainMenu()
... {
char *MenuItem[]=...{
"┊ 航空客运订票系统菜单 ┊",
" ┊ [1]. 订 票 办 理 ┊ ",
" ┊ [2]. 退 票 办 理 ┊ ",
" ┊ [3]. 航 线 管 理 ┊ ",
" ┊ [4]. 乘 客 管 理 ┊ ",
" ┊ [5]. 系 统 离 开 ┊ "
};
return ModelMenu(MenuItem,6);
}
// ---------------是否继续函数---------------
char Continue()
... {
char answer;
while(1)
...{
cout<<" ";
cout<<"┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈";
cout<<" 是否继续(Y/N)?";
cin>>answer;
if(answer=='y'||answer=='Y')
return 'y';
else if(answer=='n'||answer=='N')
return 'n';
else
cout<<" 输入错误,请重新输入!"<<endl;
}
}
// ---------------操作出错函数---------------
void ErrorMess()
... {
cout<<" 对不起,没有相关菜单项,按任意键继续..."<<endl;
getch();
}
// --------------操作提示函数-------------
void prefix( char * Item)
... {
system("cls");
cout<<" 当前操作:"<<Item<<endl;
cout<<" ";
cout<<"┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈";
cout<<endl;
}
// ------------航线是否为空函数-------------
int Empty_Flight()
... {
if(Head==NULL)
...{
system("cls");
cout<<" 对不起,没有相关航线。按任意键返回..."<<endl;
getch();
return 1;
}
else
return 0;
}
// -----------------系统退出与保存-----------------
int ExitSystem()
... {
char answer;
cout<<" 您确定要离开系统吗(Y/N)?";
cin>>answer;
system("cls");
int size1=sizeof(Flight);
int size2=sizeof(Customer);
int size3=sizeof(Replace);
fstream Write1,Write2,Write3;
Write1.open("Flight.txt",ios::binary|ios::app|ios::trunc);
Write2.open("Customer.txt",ios::binary|ios::app|ios::trunc);
Write3.open("Replace.txt",ios::binary|ios::app|ios::trunc);
Flight *p1;
Customer *p2;
Replace *p3;
p1=Head;
p2=p1->CustName;
p3=p1->ReplName;
if(answer=='y'||answer=='Y')
...{
cout<<"航线信息写入......"<<endl;
while(p1!=NULL)
...{
strcpy(FL.Des_Name,p1->Des_Name);
cout<<FL.Des_Name<<" ";
strcpy(FL.Flight_No,p1->Flight_No);
cout<<FL.Flight_No<<" ";
strcpy(FL.Plane_No,p1->Plane_No);
cout<<FL.Plane_No<<" ";
FL.Week_Day=p1->Week_Day;
cout<<FL.Week_Day<<" ";
FL.Customer_Amount=p1->Customer_Amount;
cout<<FL.Customer_Amount<<" ";
FL.Free_Amount=p1->Free_Amount;
cout<<FL.Free_Amount<<" ";
FL.Price[0]=p1->Price[0];
FL.Price[1]=p1->Price[1];
FL.Price[2]=p1->Price[2];
FL.CustNo=p1->CustNo;
cout<<FL.CustNo<<" ";
FL.ReplNo=p1->ReplNo;
cout<<FL.ReplNo<<endl;
Write1.write((char*)&FL,size1);
p1=p1->Next;
}
Write1.close();
cout<<"乘客信息写入......"<<endl;
p1=Head;
while(p1!=NULL)
...{
if(p1->CustName!=NULL)
...{
p2=p1->CustName;
while(p2!=NULL)
...{
strcpy(CS.Name,p2->Name);
cout<<CS.Name<<" ";
CS.Rank=p2->Rank;
cout<<CS.Rank<<" ";
CS.Seat_No=p2->Seat_No;
cout<<CS.Seat_No<<" ";
CS.Amount=p2->Amount;
cout<<CS.Amount<<endl;
Write2.write((char*)&CS,size2);
p2=p2->Next;
}
}
p1=p1->Next;
}
Write2.close();
cout<<"候补信息写入......"<<endl;
p1=Head;
while(p1!=NULL)
...{
if(p1->ReplName!=NULL)
...{
p3=p1->ReplName;
while(p3!=NULL)
...{
strcpy(RP.Name,p3->Name);
cout<<RP.Name<<" ";
RP.Rank=p3->Rank;
cout<<RP.Rank<<" ";
RP.Seat_No=p3->Seat_No;
cout<<RP.Seat_No<<" ";
RP.Amount=p3->Amount;
cout<<RP.Amount<<endl;
Write3.write((char*)&RP,size3);
p3=p3->Next;
}
}
p1=p1->Next;
}
Write3.close();
cout<<" 按任意键退出..."<<endl;
getch();
return 1;
}
else
return 0;
}
// -----------------从文件读取订票乘客信息-----------------
void AccessReserve()
... {
int size=sizeof(Customer);
fstream Access("Customer.txt",ios::binary|ios::in);
Access.seekg(0,ios::beg);
Flight *p1;
Customer *p2;
p1=Head;
int Flight_No=0;
while(p1!=NULL)
...{
// cout<<"该航线乘客总数"<<p1->CustNo;
if(p1->CustNo!=0)
...{
IsEmpty=0;
p2=p1->CustName;
// cout<<"票"<<CS.Amount;
for(int i=0;i<p1->CustNo;i=i+CS.Amount)