本文主要是介绍C语言实现航班管理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
航班管理系统,用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;
}
这篇关于C语言实现航班管理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!