航班管理系統,用C語言實現,可以作為課程設計,代碼如下:
#include<iostream>
#include<fstream>
#include<vector>
#include<string>?
#include<stdlib.h>
using namespace std;
//信息基類?
class info{
protected:
?? ?int flightnumber;
public:
? ? int Get_flightnumber(){return flightnumber;}
?? ?virtual void Get_Message();
? ? virtual void Read_File(fstream &file);//從文件中讀取?
? ? virtual void Write_File(fstream &file);//寫入文件?
? ? virtual void Show_Message();
? ? virtual ~info(){};
};
//系統類?
class System{
?? ?fstream file1,file2;
?? ?info *myi;
?? ?vector<info*> inv[4];?
?? ?vector<info*>::iterator iter;
?? ?public:
?? ??? ?System();
?? ??? ?void menu();
?? ??? ?void menu1(int i);
?? ??? ?void menu2(int i);
?? ??? ?void menu3(int i);
?? ??? ?void Load(vector<info*> &inv,int choose);//信息錄入?
?? ??? ?void Save(vector<info*> inv);//信息保存?
?? ??? ?void Add(vector<info*> &inv,int choose);//信息添加?
?? ??? ?void Delete(vector<info*> &inv,int choose);//信息刪除?
?? ??? ?void Search(vector<info*> inv);//信息查找?
?? ??? ?void Show(vector<info*> inv);//信息顯示?
?? ??? ?~System(){};
};
//航班信息類
class flight:virtual public info{
protected:
string place1;
string place2;
string time;
double price;
int maxpeople;
fstream file1,file2;
public:
flight(){};?
string Get_place1(){return place1;}?
string Get_place2(){return place2;}
string Get_time(){return time;} ?
void Get_Message();
void Read_File(fstream &file);
void Write_File(fstream &file);
void Show_Message();
~flight(){};
};
//客戶類?
class guest:virtual public info{
protected:
?? ?string name;
?? ?int id;
?? ?string sex;
?? ?int ticketnum;
?? ?public:
guest(){};?
void Get_Message();
string Get_name(){return name;}
int Get_id(){return id;}
void Read_File(fstream &file);
void Write_File(fstream &file);
void Show_Message();
~guest(){};
};
//機票類?
class ticket:virtual public info{
?protected:?
?? ?int zticket;
?? ?int syticket;
?? ?fstream file1,file2;
?? ?public:
ticket(){};?
void Get_Message();
void Read_File(fstream &file);
void Write_File(fstream &file);
void Show_Message();
~ticket(){};
};
//客戶行程信息類?
class guestflight:public flight,public guest{
public:
guestflight(){};?
void Get_Message(){};
void Read_File(fstream &file);
void Write_File(fstream &file);
void Show_Message(){};
~guestflight(){};
};
//基類信息實現?
void info::Get_Message(){
?? ?cout<<"請輸入航班號:";
?? ?cin>>flightnumber;?
}
void info::Show_Message(){
?? ?cout<<"flight:"<<flightnumber<<endl;
}
void info::Write_File(fstream &file){
?? ?file<<flightnumber;
}
void info::Read_File(fstream &file){
?? ?file>>flightnumber;
}
//航班實現?
void flight::Get_Message(){
info::Get_Message();
cout<<endl<<"請輸入出發地:";
cin>>place1;
cout<<endl<<"請輸入目的地:";
cin>>place2;
cout<<endl<<"請輸入出發時間:";
cin>>time;
cout<<endl<<"請輸入票價:";
cin>>price;
cout<<endl<<"請輸入最大載客量:";
cin>>maxpeople;
cout<<endl;
}
void flight::Show_Message(){
info::Show_Message();
cout<<"place1:"<<place1<<endl;
cout<<"place2:"<<place2<<endl;
cout<<"time:"<<time<<endl;
cout<<"price:"<<price<<endl;
cout<<"maxpeople:"<<maxpeople<<endl;
}
void flight::Write_File(fstream &file){
info::Write_File(file);
file<<place1<<" "<<place2<<" "<<time<<" "<<price<<" "<<maxpeople<<endl;
}
void flight::Read_File(fstream &file){
info::Read_File(file);
file>>place1>>place2>>time>>price>>maxpeople;
}
//客戶實現?
void guest::Get_Message(){
info::Get_Message();
cout<<endl<<"請輸入姓名:";
cin>>name;
cout<<endl<<"請輸入證件號:";
cin>>id;
cout<<endl<<"請輸入性別:";
cin>>sex;
cout<<endl<<"請輸入訂票票數:";
cin>>ticketnum;
cout<<endl;
}
void guest::Show_Message(){
info::Show_Message();
cout<<"name:"<<name<<endl;
cout<<"id:"<<id<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"ticketnum:"<<ticketnum<<endl;
}
void guest::Write_File(fstream &file){
info::Write_File(file);
file<<name<<" "<<id<<" "<<sex<<" "<<ticketnum<<endl;
}
void guest::Read_File(fstream &file){
info::Read_File(file);
file>>name>>id>>sex>>ticketnum;
}
//機票實現?
void ticket::Get_Message(){
info::Get_Message();
cout<<endl<<"請輸入總票數:";
cin>>zticket;
cout<<endl<<"請輸入剩余票數:";
cin>>syticket;
cout<<endl;?
}
void ticket::Show_Message(){
info::Show_Message();
cout<<"zticket:"<<zticket<<endl;
cout<<"syticket:"<<syticket<<endl;
}
void ticket::Write_File(fstream &file){
info::Write_File(file);
file<<zticket<<" "<<syticket<<endl;
}
void ticket::Read_File(fstream &file){
info::Read_File(file);
file>>zticket>>syticket;
}
//客戶行程實現?
void guestflight::Write_File(fstream &file){
info::Write_File(file);
file<<guest::name<<" "<<guest::id<<" "<<flight::flightnumber<<" "<<flight::place1<<" "<<flight::place2<<" "<<flight::time;
}
void guestflight::Read_File(fstream &file){
info::Read_File(file);
file>>guest::name>>guest::id>>flight::flightnumber>>flight::place1>>flight::place2>>flight::time;
}
//系統類實現
System::System(){
file1.open("flight1.txt",ios::out);
?? ?if(!file1){
?? ??? ?cout<<"file open error!"<<endl;
?? ??? ?abort();
?? ?}
file2.open("flight2.txt",ios::in);
?? ?if(!file2){
?? ??? ?cout<<"file open error!"<<endl;
?? ??? ?abort();
?? ?}
}
void System::menu(){
?? ?int choose,yn;
?? ?while(1){
?? ??? ?cout<<"請選擇您要管理的信息類型:"<<endl;
?? ??? ?cout<<"0-航班信息"<<endl;
?? ??? ?cout<<"1-客戶信息"<<endl;
?? ??? ?cout<<"2-機票信息"<<endl;
?? ??? ?cout<<"3-顯示所有客戶行程信息"<<endl;
?? ??? ?cout<<"請輸入:"<<endl;?
?? ??? ?cin>>choose;
?? ??? ?cout<<endl;
?? ? ? ??? ?guest g;
?? ??? ??? ?flight f;
?? ??? ??? ?fstream infile("guestflight2.txt",ios::in);
?? ??? ?switch(choose){
?? ??? ??? ?case 0:
?? ??? ??? ??? ?menu1(choose);
?? ??? ??? ??? ?break;
?? ??? ??? ?case 1:
?? ??? ??? ??? ?menu2(choose);
?? ??? ??? ??? ?break;
? ? ? ? ?? ?case 2:
?? ??? ??? ??? ?menu3(choose);
?? ??? ??? ??? ?break;
?? ??? ??? ?case 3:?
?? ??? ??? ? ? ?while(!infile.eof()){
?? ??? ??? ? ? ?infile<<g.Get_name()<<g.Get_id()<<f.Get_flightnumber()<<f.Get_place1()<<f.Get_place2()<<f.Get_time();
?? ??? ??? ? ? ?cout<<g.Get_name()<<g.Get_id()<<f.Get_flightnumber()<<f.Get_place1()<<f.Get_place2()<<f.Get_time();
?? ??? ??? ?}
?? ??? ??? ? ? ?break;
?? ??? ??? ?default:
?? ??? ??? ??? ?cout<<"輸入有誤!"<<endl;
?? ??? ??? ??? ?exit(0);?
?? ??? ??? ?}
?? ??? ?cout<<"是否繼續?(1/0)"<<endl;
?? ??? ?cin>>yn;
?? ??? ?if(yn!=1)?
?? ??? ?break;
?? ?}
}
void System::menu1(int i){
?? ?int choose,ny;
?? ?while(1){
?? ??? ?cout<<"請選擇操作:"<<endl;
?? ??? ?cout<<"0-航班信息錄入"<<endl;
?? ??? ?cout<<"1-添加航班信息"<<endl;
?? ??? ?cout<<"2-刪除航班信息"<<endl;
?? ??? ?cout<<"3-更改航班信息"<<endl;
?? ??? ?cout<<"4-查詢航班信息"<<endl;
?? ??? ?cout<<"5-顯示航班信息"<<endl;
?? ??? ?cout<<"6-保存航班信息"<<endl;
?? ??? ?cout<<"請輸入:"<<endl;
?? ??? ?cin>>choose;
?? ??? ?cout<<endl;
?? ??? ?switch(choose){
?? ??? ??? ?case 0:
?? ??? ??? ?Load(inv[i],choose);
?? ??? ??? ?break;
?? ??? ??? ?case 1:
?? ??? ??? ?Add(inv[i],choose);
?? ??? ??? ?break;
?? ??? ??? ?case 2:
?? ??? ??? ?Delete(inv[i],choose);
?? ??? ??? ?break;
?? ??? ??? ?case 3:?? ?
?? ??? ??? ?break;
?? ??? ??? ?case 4:
?? ??? ??? ?Search(inv[i]);
?? ??? ??? ?break;
?? ??? ??? ?case 5:
?? ??? ??? ?Show(inv[i]);
?? ??? ??? ?break;
?? ??? ??? ?case 6:
?? ??? ??? ?Save(inv[i]);
?? ??? ??? ?break;
?? ??? ??? ?default:
?? ??? ??? ??? ?cout<<"輸出有誤"<<endl;
?? ??? ??? ??? ?exit(0);
? ? ? ? ? ? ? ? ? }
? ? ? ? cout<<"是否繼續?(1/0)"<<endl;
?? ??? ?cin>>ny;
?? ??? ?if(ny!=1)?
?? ??? ?break;
?? ??? ?}?
}
void System::menu2(int i){
?? ?int choose,ny;
?? ??? ?while(1){
?? ? ? ?cout<<"請選擇操作:"<<endl;
?? ??? ?cout<<"0-客戶信息錄入"<<endl;
?? ??? ?cout<<"1-添加客戶信息"<<endl;
?? ??? ?cout<<"2-刪除客戶信息"<<endl;
?? ??? ?cout<<"3-更改客戶信息"<<endl;
?? ??? ?cout<<"4-查詢客戶信息"<<endl;
?? ??? ?cout<<"5-顯示客戶信息"<<endl;
?? ??? ?cout<<"6-保存客戶信息"<<endl;
?? ??? ?cout<<"按任意鍵退出...."<<endl;
?? ??? ?cout<<"請輸入:";
?? ??? ?cin>>choose;
?? ??? ?cout<<endl;
?? ??? ?switch(choose){
?? ??? ??? ?case 0:
?? ??? ??? ?Load(inv[i],choose);
?? ??? ??? ?break;
?? ??? ??? ?case 1:
?? ??? ??? ?Add(inv[i],choose);
?? ??? ??? ?break;
?? ??? ??? ?case 2:
?? ??? ??? ?Delete(inv[i],choose);
? ? ? ? ? ? break;
? ? ? ? ? ? case 3:
? ? ? ? ? ? ?? ?
?? ??? ??? ?case 4:
?? ??? ??? ?Search(inv[i]);
?? ??? ??? ?break;
?? ??? ??? ?case 5:
?? ??? ??? ?Show(inv[i]);
?? ??? ??? ?break;
?? ??? ??? ?case 7:
?? ??? ??? ?Save(inv[i]);
?? ??? ??? ?break;
?? ??? ?default:
?? ??? ?cout<<"輸出有誤"<<endl;
?? ??? ?exit(0);
? ? ? ? ? ?}
? ? ? ? cout<<"是否繼續?(1/0)"<<endl;
?? ??? ?cin>>ny;
?? ??? ?if(ny!=1)?
?? ??? ?break;?? ??? ?
?? ??? ?} ?? ??? ?
?? ??? ?}
void System::menu3(int i){
?? ?int choose,ny;
?? ??? ?while(1){
?? ? ? ?cout<<"請選擇操作:"<<endl;
?? ??? ?cout<<"0-機票信息錄入"<<endl;
?? ??? ?cout<<"1-添加機票信息"<<endl;
?? ??? ?cout<<"2-刪除機票信息"<<endl;
?? ??? ?cout<<"3-更改機票信息"<<endl;
?? ??? ?cout<<"4-查詢機票信息"<<endl;
?? ??? ?cout<<"5-顯示機票信息"<<endl;
?? ??? ?cout<<"6-保存機票信息"<<endl;
?? ??? ?cout<<"按任意鍵退出...."<<endl;
?? ??? ?cout<<"請輸入:";
?? ??? ?cin>>choose;
?? ??? ?cout<<endl;
?? ??? ?switch(choose){
?? ??? ??? ?case 0:
?? ??? ??? ?Load(inv[i],choose);
?? ??? ??? ?break;
?? ??? ??? ?case 1:
?? ??? ??? ?Add(inv[i],choose);
?? ??? ??? ?break;
?? ??? ??? ?case 2:
?? ??? ??? ?Delete(inv[i],choose);
?? ??? ??? ?break;
?? ??? ??? ?case 3:?? ?
?? ??? ??? ?break;
?? ??? ??? ?case 4:
?? ??? ??? ?Search(inv[i]);
?? ??? ??? ?break;
?? ??? ??? ?case 5:
?? ??? ??? ?Show(inv[i]);
?? ??? ??? ?break;
?? ??? ??? ?case 6:
?? ??? ??? ?Save(inv[i]);
?? ??? ??? ?break;
?? ??? ??? ?default:
?? ??? ? ?cout<<"輸出有誤"<<endl;
?? ? ? ? ?exit(0);
? ? ? ? ? ? ? ? }
? ? ? ? cout<<"是否繼續?(1/0)"<<endl;
?? ??? ?cin>>ny;
?? ??? ?if(ny!=1)?
?? ??? ?break;?? ??? ?
?? ??? ?} ?? ??? ?
?? ??? ?}
void System::Load(vector<info*> &myv,int choose){
?? ?while(!file2.eof()){
?? ?switch(choose){
?? ??? ?case 1:
?? ??? ?myi=new flight;
?? ??? ?break;
?? ??? ?case 2:
?? ??? ?myi=new guest;
?? ??? ?break;
?? ??? ?case 3:?? ?
?? ??? ?myi=new ticket;
?? ??? ?break;
?? ??? ?case 4:
?? ??? ?myi=new guestflight;
?? ??? ?break;
?? ?}
?? ?myi->Read_File(file2);
?? ?myv.push_back(myi);
}
file2.close();
}
void System::Save(vector<info*> myv){
?? ?for(iter=myv.begin();iter<myv.end();iter)
?? ?(*iter)->Write_File(file1);
?? ?file1.close();
}
void System::Add(vector<info*> &myv,int choose){
?? ?switch(choose){
?? ??? ?case 1:
?? ??? ?myi=new flight;
?? ??? ?break;
?? ??? ?case 2:
?? ??? ?myi=new guest;
?? ??? ?break;
?? ??? ?case 3:?? ?
?? ??? ?myi=new ticket;
?? ??? ?break;
?? ??? ?case 4:
?? ??? ?myi=new guestflight;
?? ??? ?break;
?? ?}
?? ?myi->Get_Message();
?? ?myv.push_back(myi);
}
void System::Delete(vector<info*> &myv,int choose){?
int flight1,flight2;
?? ?switch(choose){
?? ??? ?case 1:
?? ??? ?cout<<"請輸入您要刪除的航班號"<<endl;
?? ??? ?cin>>flight1;
?? ??? ?for(iter=myv.begin();iter!=myv.end();iter++){
?? ??? ??? ?if((*iter)->Get_flightnumber()==flight1){
?? ??? ??? ??? ?myv.erase(iter);
?? ??? ??? ?}
?? ??? ?}?
?? ??? ?break;
?? ??? ?case 2:
?? ?//cout<<"請輸入您要刪除的客戶名"<<endl;
??? ?//?? ?string guest1;
?? ?//?? ?cin>>guest1;
?? ?//?? ?for(iter=myv.begin();iter!=myv.end();iter++){
? ? //?? ??? ??? ?if((*iter)->Get_name()==guest1){
?? ?//?? ??? ??? ?myv.erase(iter);
?? ?//?? ??? ?}
? ? //?? ??? ?}?
?? ?//break;
?? ??? ?case 3:?? ?
?? ??? ?cout<<"請輸入您要刪除的航班號"<<endl;
?? ??? ?cin>>flight2;
?? ??? ?for(iter=myv.begin();iter!=myv.end();iter++){
?? ??? ?if((*iter)->Get_flightnumber()==flight2){
?? ??? ?myv.erase(iter);
?? ? }
? ? ?}
?? ??? ?break;
?? ?}
}
void System::Search(vector<info*> myv){
?? ??? ?int flight1;
?? ??? ?cout<<"請輸入您要查找的航班號"<<endl;
?? ??? ?cin>>flight1;
?? ??? ?for(iter=myv.begin();iter!=myv.end();iter++){
?? ??? ??? ?if((*iter)->Get_flightnumber()==flight1){
?? ??? ??? ?(*iter)->Show_Message();
?? ??? ??? ?}
?? ??? ?}?
?? ?}
?? ?
void System::Show(vector<info*> myv){
?? ?for(iter=myv.begin();iter!=myv.end();iter++){
?? ?(*iter)->Show_Message();
?? ?}
}
int main(){
?? ?System s;
?? ?s.menu();
?? ?return 0;
}
?