📄 2.cpp
字号:
#include"SqFlight.h"
#include<fstream>
#include<iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 2
#define MAXCHAR 20
const char* a_file="A.txt";
int InitList_Sq(SqFlight &LF){
//构造一个空的线性表LF
LF.flight=(flightnode*)malloc(sizeof(flightnode));
if(!LF.flight)exit(ERROR);
LF.length=0;
LF.flight->next=NULL;
return(OK);
}//InitList_Sq
int InitList_CL(ClientLink &CL){ //为客户链表建一个空链表
CL.client=(ClientNode*)malloc(sizeof(ClientNode));
if(!CL.client)exit(ERROR);
CL.length=0;
CL.client->next=NULL;
return(OK);
}//InitList_CL
int InitList_WCQ(WClientQueue &WCQ){ //为待票客户建一个空的队列
WCQ.front=WCQ.rear=(WClientNode*)malloc(sizeof(WClientNode));
if(!WCQ.front)exit(ERROR);
WCQ.front->next=NULL;
return(OK);
}//InitList_WCQ
int InitFlightNode(SqFlight &LF){ //对航线结点赋值并接到航班顺序表
flightnode *p,*q;
ClientLink CL1;
InitList_CL(CL1);
WClientQueue WCQ1;
InitList_WCQ(WCQ1);
if(!LF.flight){cout<<"无法找到指向航线结点的指针\n";exit(ERROR);}
LF.flight->airline="guangzhou--beijing"; //头结点赋值
LF.flight->client=CL1;
LF.flight->flight_no="FU127";
LF.flight->nclient=WCQ1;
LF.flight->number=8427;
LF.flight->remain=200;
LF.flight->total=200;
LF.flight->weekday=1;
p=(flightnode*)malloc(sizeof(flightnode));
if(!p){cout<<"申请不到航线结点\n";exit(ERROR);}
LF.flight->next=p;
ClientLink CL2;
InitList_CL(CL2);
WClientQueue WCQ2;
InitList_WCQ(WCQ2);
p->airline="guangzhou--beijing"; //结点1赋值
p->client=CL2;
p->flight_no="BU127";
p->nclient=WCQ2;
p->number=8757;
p->remain=180;
p->total=180;
p->weekday=5;
q=p;
p=NULL;
p=(flightnode*)malloc(sizeof(flightnode));
if(!p){cout<<"申请不到航线结点\n";exit(ERROR);}
q->next=p;
ClientLink CL3;
InitList_CL(CL3);
WClientQueue WCQ3;
InitList_WCQ(WCQ3);
p->airline="guangzhou--shanghai"; //结点2赋值
p->client=CL3;
p->flight_no="FU786";
p->nclient=WCQ3;
p->number=3216;
p->remain=200;
p->total=200;
p->weekday=2;
q=p;
p=NULL;
p=(flightnode*)malloc(sizeof(flightnode));
if(!p){cout<<"申请不到航线结点\n";exit(ERROR);}
q->next=p;
ClientLink CL4;
InitList_CL(CL4);
WClientQueue WCQ4;
InitList_WCQ(WCQ4);
p->airline="guangzhou--shanghai"; //结点3赋值,有四个航班,共4个结点
p->client=CL4;
p->flight_no="FU236";
p->nclient=WCQ4;
p->number=5712;
p->remain=230;
p->total=230;
p->weekday=6;
p->next=NULL;
return(OK);
}
int lookup(char *airline,SqFlight LF){ //查询系统
//形参指针airline指向查询时输入的航线的首地址
//引用航班顺序线性表LF
flightnode *p=LF.flight;
char *h,*q=airline;
int i=0;
while(p){
h=p->airline;
while(*airline==*h){ //查询航线是否在LF的航班航线里有
if(*airline=='\0'||*h=='\0')
break;
airline++,h++;
}
if(*airline=='\0'&&*h=='\0')//若有该航线输出航线信息
{
i++;
cout<<p->airline<<" "<<p->flight_no<<" "<<p->number<<" "
<<p->remain<<" "<<p->total<<" "<<p->weekday<<endl;
}
airline=q;
p=p->next; //航班结点指针后移
}
cout<<"有此航班"<<i<<"条\n";
return(OK);
}
flightnode *search(int number,SqFlight LF){
//形参number查看时输入的航班号
//引用航班顺序线性表LF
flightnode *p=LF.flight;
while(p){
if(number==p->number)break;//查看的航班号是否在LF的航班号里有
p=p->next;
}
return(p); //返回指向该航班结点的指针
}
ClientNode *searchID(flightnode *p,char *ID){
//p是指向某个航班结点的指针
//ID是要查询的ID
ClientNode *h=p->client.client->next;
ClientNode *prior_h=NULL; //指向这个ID客户的前驱的指针
prior_h=p->client.client;
int i=0;
char *p_ID=ID;
char *h_ID=NULL;
while(h){
ID=p_ID;h_ID=h->ID;
while(*ID==*h_ID){ //查询的ID在已订客户链表中是否有
if(*ID=='\0'||*h_ID=='\0')break;
ID++,h_ID++;
}
if(*ID=='\0'&&*h_ID=='\0')break;//若有结束循环
prior_h=prior_h->next;
h=h->next;
}
if(!h)prior_h=NULL;
return(prior_h);//返回前驱指针
}
int wbook(SqFlight LF,flightnode *p){ //待票客户队列
//引用航班顺序线性表LF
//p是指向某个航班结点的指针
WClientNode *h=NULL;
loop:
h=(WClientNode *)malloc(sizeof(WClientNode));
if(!h)
goto loop;
cout<<"请输入客户的名字:\n";
cin>>h->name;
cout<<"请输入客户的ID:\n";
cin>>h->ID;
cout<<"请输入客户的电话号码:\n";
cin>>h->phoneno;
cout<<"请输入客户需要的票数:\n";
cin>>h->neednum;
h->next=NULL;
p->nclient.rear->next=h;
p->nclient.rear=h;
return(OK);
}
int book(int number,SqFlight LF){
//形参number查看时输入的航班号
//引用航班顺序线性表LF
ofstream output_file;//输出文件流
output_file.open(a_file,ios::app);//打开文件并将指针定位到文件尾
if(!output_file){
cout<<"Can not open a file!"<<endl;
return(OK);
}
flightnode *p=NULL;
ClientNode *h=NULL;
p=search(number,LF);//调用search(),返回指向这个航班号的航班结点的指针
output_file<<p->number<<" ";
if(!p){cout<<"无此航班号\n";return(ERROR);}
if(p->remain>=0){ //余票量大于0
loop:
h=(ClientNode *)malloc(sizeof(ClientNode));
if(!h)
goto loop;
cout<<"请输入客户的定票数:\n";
cin>>h->booknum;
output_file<<h->booknum<<" ";
if((h->booknum)<=(p->remain))
p->remain=p->remain-h->booknum; //余票量减去客户的订票量
else{ //若余票量不足,询问客户是否要候补
int i=0;
cout<<"余票数:"<<p->remain<<endl<<endl;
cout<<"已无足够的票,您是否要候补?\n\n";
cout<<"1.要 2.不要 3.按余票定票 \n";
cin>>i;
switch(i)
{
case 1:
wbook(LF,p);//调用候补函数,实现候补
free(h);h=NULL;
break;
case 2:
free(h);h=NULL;
break;
case 3:
if(p->remain!=0)
{
h->booknum=p->remain;
p->remain=0;
}
else{
cout<<"无法按余票购票,余票已为零\n";
return(ERROR);
}
break;
default:cout<<"选择有误\n";
}
if(i==1){cout<<"候票成功\n";return(OK);}
else if(i==2){cout<<"祝您下次购票成功\n";return(OK);}
}
cout<<"请输客户的姓名:\n";
cin>>h->name;
output_file<<h->name<<" ";
cout<<"请输入客户的ID:\n";
cin>>h->ID;
output_file<<h->ID<<" ";
loop1:
{
cout<<"请输入客户的舱位等级1或2或3:\n";
cin>>h->grade;
output_file<<h->grade<<endl;
}
if(h->grade!=3&&h->grade!=2&&h->grade!=1)
{
cout<<"无此等级舱位\n";
goto loop1;
}
cout<<"购票成功\n";
h->next=p->client.client->next;
p->client.client->next=h;
}
output_file.close();//关闭文件
return(OK);
}
int ask_clien(flightnode *p,SqFlight LF){
//p是指向某个航班结点的指针
////引用航班顺序线性表LF
int i,j;
WClientNode *h=p->nclient.front->next;
WClientNode *prior_h=p->nclient.front;
if(!h)cout<<"无人候票\n";
while(h){
i=0;j=0;
cout<<"余票数:"<<p->remain<<endl<<endl;
if(p->remain>=h->neednum)
cout<<h->name<<"要不要购票?\n";
else
cout<<"余票数不够"<<h->name<<"的需要,您要不要购票?\n";
loop:
{ i=2;
cout<<"\n1.要 2.不要\n";
cin>>i;
}
switch(i)
{
case 1:
cout<<"客户的名字:"<<h->name<<endl;
cout<<"客户的ID:"<<h->ID<<endl;
cout<<"客户需要的票数:"<<h->neednum<<endl<<endl;
book(p->number,LF);
if(!prior_h->next->next)p->nclient.rear=prior_h;
prior_h->next=prior_h->next->next;
j=1; //j=1,表示prior_h将跳过一个队列结点
break;
case 2:
cout<<"谢谢您对本航班的关注\n";
break;
default:
cout<<"选择有误\n";
goto loop;
}
if(j==0)prior_h=prior_h->next;
h=h->next;
if(p->remain<=0)
cout<<"余票已被购尽!!\n";
}
return(OK);
}
int back_ticket(int number,SqFlight LF){
char ID[20];
ClientNode *prior_h=NULL;
flightnode *p=NULL;
ClientNode *h=NULL;
p=search(number,LF);
if(!p){cout<<"无此航班号\n";return(ERROR);}
cout<<"请输入退票客户的ID:\n";
cin>>ID;
prior_h=searchID(p,ID);
if(!prior_h){cout<<"无此客户的ID~~~~\n";return(ERROR);}
else{
p->remain=p->remain+prior_h->next->booknum;
prior_h->next=prior_h->next->next;
}
cout<<"退票成功\n\n";
cout<<"询问待票客户:\n";
ask_clien(p,LF);
return(OK);
}
void menu()
{
cout<<" 选择菜单 \n";
cout<<" \n";
cout<<"1.查询航线 2.客票预定 3.办理退票 4.查看当前定票客户资料 5.查看文件已有客户资料6.退出\n";
}
int GetElem(SqFlight LF,int number)
{
//用e返回L中第i个数据的值,1<=i<=Listlength(L).
flightnode *p=NULL;
p=search(number,LF);
ClientNode *h=p->client.client->next;
if(!h)cout<<"尚未有客户\n";
while(h){
cout<<"客户的名字:"<<h->name<<endl;
cout<<"客户的ID:"<<h->ID<<endl;
cout<<"客户票数:"<<h->booknum<<endl<<endl;
h=h->next;
}
return OK;
}//GetList;
input(){
char a;
ifstream input_file;
input_file.open(a_file);
if(!input_file){
cout<<"Can not open a file!"<<endl;
return(OK);
}
cout<<"航班号 "<<"票数 "<<"客户名 "<<"ID "<<"舱位等级";
while(input_file.get(a))
cout<<a;
input_file.close();
}
void main(){
char airline[20];
int number=0;
int drop_out=0;
int i;
SqFlight LF;
InitList_Sq(LF);
InitFlightNode(LF);
cout<<" --------------------------------------------"<<endl;
cout<<" Welcome to film! "<<endl;
cout<<" ^ ^ 欢迎您进入民航订票系统 ^ ^ "<<endl;
cout<<" ^ ^ 在此我们将为您提供最优质的服务 ^ ^ "<<endl;
cout<<" "<<endl;
cout<<" --------------------------------------------"<<endl<<endl;
cout<<"航线有两条,输入格式:\n";
cout<<" guangzhou--beijing\n";
cout<<" guangzhou--shanghai\n";
cout<<"航班号可以通过查看航线得知;\n\n";
while(drop_out!=-1)
{
menu();
cout<<"请选择菜单:\n";
int i;
cin>>i;
switch(i)
{
case 1:
cout<<"请输入要查询的航线:\n";
cin>>airline;
cout<<"航线\n";
cout<<"起点 -- 终点 飞机号 航班号 总票数 余票数 飞行周日\n";
lookup(airline,LF);
break;
case 2:
cout<<"请输入要预定的航班号:\n";
cin>>number;
book(number,LF);
break;
case 3:
cout<<"请输入要退票的航班号:\n";
cin>>number;
back_ticket(number,LF);
break;
case 4:
cout<<"查看客户资料,输入客户的航班号\n";
cin>>number;
GetElem(LF,number);
break;
case 5:
input();
break;
case 6:
drop_out=-1;
break;
default:
cout<<"选择有误\n";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -