?? 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 + -