加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

c++设计 航班订票系统

(2010-06-02 06:29:51)
标签:

it

c设计

航班订票系统

东华理工

july

分类: c/vc源码

 

                   //此航班订票系统由我一同学方辉制作。本人并不喜欢此设计。原因..。

                   //此设计基本实现了系统所要求的各项基本功能。

                   //不及细看。有不足之处,请帮他指正。/July.整理。06.02/。

        

 

       

#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include <iomanip.h>


class Plan               //航班类

 
 char num[20];           //航班号
 char s[20];             //出发站
 char e[20];          //到达站
 char t[20];             //起飞时间
 int fee;             //票价
 int sum;                //总票数
 int n;                  //剩余票数
 Plan   *next;           //下个航班
public:
 Plan(){}
 ~Plan(){}
 char *getnu(){return num;}
 void setnu(char *n){strcpy(num,n);}
 char *gets(){return s;}
 void sets(char *s){strcpy(this->s,s);}
 char *gete(){return e;}
 void sete(char *e){strcpy(this->e,e);}
 char *gett(){return t;}
 void sett(char *t){strcpy(this->t,t);}
 int getf(){return fee;}
    void setf(int f){fee=f;}
 int getsu(){return sum;}
    void setsu(int s){sum=s;}
    int getn(){return n;}
    void setn(int n){this->n=n;}
 Plan *getnext(){return next;}
 void setnext(Plan*n){next=n;}
};


class Client            //乘客类
{
 char num[20];          //航班号
    char name[20];      //乘客姓名
    int n;              //所购票数
 Client *next;
public:
 Client(){}
 ~Client(){}
 char *getna(){return name;}
 void setna(char *n){strcpy(name,n);}
 char *getnu(){return num;}
 void setnu(char *n){strcpy(num,n);}
    int getn(){return n;}
    void setn(int n){this->n=n;}
 Client *getnext(){return next;}
 void setnext(Client*n){next=n;}
};


class manage           //管理类
{
private:
 Plan *head1;         //航班头指针
    Plan *last1;
 Client *head2;       //乘客头指针
 Client *last2;
public:
 manage(){}
    ~manage(){}
 void ini();
    void newf();
    void buy();
    void show1();
    void show2();
    void modify();
    void del();
    void delall();
    void baClient();
    void query1();
    void query2();
    void save();
    void run();
};


void manage::ini()    //读取文件
{
 head1=last1=NULL; head2=last2=NULL;
 ifstream rf("plane.txt",ios::out|ios::nocreate,filebuf::openprot);
 if(rf.fail())cout<<"\n提示,文件读取失败!\n";
 else
 {
  int n=0;                       //得到个数
  rf.read((char *)&n,sizeof(n));       //得到航班个数
  for(int i=0;i<n;i++)
  {
   Plan *p1=new Plan;
   rf.read((char*)p1,sizeof(Plan));
   p1->setnext(NULL);
   if(head1==NULL)head1=last1=p1;
   else {last1->setnext(p1);last1=p1;}
  }
  cout<<"\n初始化"<<i<<"条航班信息成功!";
  
  rf.read((char *)&n,sizeof(n));      //得到乘客个数
  for( i=0;i<n;i++)
  {
   Client *p2=new Client;
   rf.read((char*)p2,sizeof(Client));
   p2->setnext(NULL);
   if(head2==NULL)head2=last2=p2;
   else {last2->setnext(p2);last2=p2;}
  }
  cout<<"\n初始化"<<i<<"条乘客信息成功!";
 }
 rf.close();
}


void manage::newf()    //录入航班信息
{
 Plan*p;
 p=new Plan;
 cout<<"\n录入航班信息\n";
 char c[50];
 cout<<"请输入航班号\n";
 cin>>c;
 p->setnu(c);
 cout<<"请输入起始地\n";
 cin>>c;
 p->sets(c);
 cout<<"请输入目的地\n";
 cin>>c;
 p->sete(c);
 cout<<"请输入起飞日期(年/月/日/时:分)\n";
 cin>>c;
 p->sett(c);
 cout<<"\n请输入机票金额\n\n";
 int n;
 cin>>n;p->setf(n);
 cout<<"\n请输入机票总数\n\n";
 cin>>n;p->setsu(n);
 p->setn(n);
 p->setnext(NULL);
 if(head1){last1->setnext(p);last1=p;}
 else
  head1=last1=p;
 cout<<"\n新航班创建成功\n\n";
}


void manage::buy()
{
 if(head1==NULL){cout<<"提示:无航班信息!\n";return ;}
 cout<<"\n\n\n\n 购买机票\n";
 char c[50];
 cout<<"\n请输入航班号\n";
 cin>>c;
 int y=1;
 Plan *p=head1;
 while(p)
 {
  if(strcmp(p->getnu(),c)==0)
  {y=0;break;}
  p=p->getnext();
 }
 if(y){cout<<"\n错误,无此航班号!";return;}
 if(p->getn()<=0){cout<<"\n提示,此航班票已经售完!\n";return;}
 cout<<"\n|---------------------------------------------------------------------------|";
 cout<<"\n  航次: "<<p->getnu()<<"            起飞时间:"<<p->gett()<<"     总票数: "<<p->getsu();
 cout<<"\n  航线:"<<p->gets()<<"---> "<<p->gete()<<"     票价:"<<p->getf()<<"      剩余票数: "<<p->getn();
 cout<<"\n|---------------------------------------------------------------------------|\n";
 Client *p1;
 p1=new Client;
 p1->setnext(NULL);
 p1->setnu(c);
 cout<<"\n-------------------------请进行购票登记 ---------------------------";
 cout<<"\n请输入姓名:\n";
 cin>>c;
 p1->setna(c);
loop:
 cout<<"\n请输入购买票数:\n";
 int n;
 cin>>n;
 if(n>p->getn())
 {
  cout<<"\n提示,您最多只能购买"<<p->getn()<<"张票!";
  goto loop;
 }
 p1->setn(n);
 p->setn(p->getn()-n);
 if(head2==NULL)head2=last2=p1;
 else {last2->setnext(p1); last2=p1;}
 cout<<"\n------------买票成功,您需要付款:"<<p->getf()*p1->getn()<<"元-----------------";
}


void manage::show1()
{
 if(head1==NULL){cout<<"航班信息为空!\n\n";return;}
 Plan *p=head1;
 cout<<setw(50)<<"航班信息";
 while(p)
 {
  cout<<"\n  航次: "<<p->getnu()<<"            起飞时间:"<<p->gett()<<"     总票数: "<<p->getsu();
  cout<<"\n  航线:"<<p->gets()<<"---> "<<p->gete()<<"     票价:"<<p->getf()<<"      剩余票数: "<<p->getn();
  cout<<"\n|---------------------------------------------------------------------------|\n";
  p=p->getnext();
 }
}


void manage::show2()
{
 if(head2==NULL){cout<<"乘客信息为空!\n\n";return;}
 Client *p=head2;
 cout<<setw(50)<<" 乘客信息";
 while(p)
 {
  cout<<"\n  姓名 : "<<p->getna()<<"           航班号:"<<p->getnu()<<"     所购票数: "<<p->getn();
  cout<<"\n|---------------------------------------------------------------------------|\n";
  p=p->getnext();
 }
}


void manage::modify()
{
 cout<<setw(50)<<"修改航班";
 Plan *p=head1;
 char m[20];
 int y=1;
 cout<<"\n\n请输入要修改的航班\n\n";
 cin>>m;
 while(p)
 {
  if(strcmp(p->getnu(),m)==0)
  {
   y=0;  
   cout<<"\n请修改起始地\n\n";
   cin>>m;
   p->sets(m);
   cout<<"\n请修改目的地\n\n";
   cin>>m;
   p->sete(m);
   cout<<"\n请修改登机时间(年/月/日/时:分)\n\n";
   cin>>m;p->sett(m);
   cout<<"\n请修改机票价格\n\n";
   int n;
   cin>>n;
   p->setf(n);
loop:
   cout<<"\n请修改机票总数\n";
   cin>>n;
   if(n<p->getsu()-p->getn()){cout<<"\n错误,机票总数必须>=已售机票数!\n";goto loop;}
   p->setn( n-(p->getsu()-p->getn()) );
   p->setsu(n);
  }
  p=p->getnext();
 }
 if(y)
  cout<<"\n错误,无此航班号!";
 else
  cout<<"\n航班修改成功!";
}


void manage::del()
{
 if(head1==NULL){cout<<"\n\n提示:无航班信息!";return ;}
 cout<<setw(50)<<" 删除航班";
 Plan *temp,*p=head1;
 char m[20];
 int y=1;
loop:
 cout<<"\n\n请输入要删除的航班\n\n";
 cin>>m;
 if(strcmp(head1->getnu(),m)==0)
 {
  if(head1->getsu()!=head1->getn())
  { cout<<"\n该航班已经开始售票,不能取消!\n\n";goto loop;}
  y=0;
  temp=head1;
  head1=head1->getnext();
 }
 else
  while(p->getnext())
    
   if(strcmp(p->getnext()->getnu(),m)==0)
   {
    if(p->getnext()->getsu()!=p->getnext()->getn())
    {
     cout<<"\n该航班已经开始售票,不能取消!\n\n";goto loop;}
    y=0;
    temp=p->getnext();
    p->setnext(temp->getnext());
    break;
   }
   p=p->getnext();
  }
  if(y)
   cout<<"\n错误,无此航班号!";
  else
  { delete temp; cout<<"\n\n操作成功!";}
}


void manage::delall()
{
 cout<<setw(50)<<" 清空信息";
 Plan *p1=head1;
 while(p1)
 {
  Plan *temp1=p1->getnext();
  delete p1;
  p1=temp1;
 }
 head1=last1=NULL;
 Client *p2=head2;
 while(p2)
 {
  Client * temp2=p2->getnext();
  delete p2;
  p2=temp2;
 }
 head2=last2=NULL;
 cout<<"\n\n信息清空成功!";
}


void manage::query1()  //查找航班信息
{
 char c;
 char m[30];
 manage* q=new manage;
 if(head1==NULL){cout<<"\n\n提示:无航班信息!";return ;}
 do
 {
  cout<<setw(50)<<" │             [航班查询菜单]       │\n";
  cout<<setw(50)<<" │    按起始地查询 1,并按回车键    │\n";
  cout<<setw(50)<<" │    按目的地查询 2,并按回车键    │\n";
  cout<<setw(50)<<" │            返回 3,并按回车键    │\n";
  Plan *p=head1;
  cin>>c;
  switch (c)
  {
  case '1':
    
    int y=1;
    cout<<"\n\n请输入要查找的起始地\n\n";
    cin>>m;
    while(p)
    {
     if(strcmp(p->gets(),m)==0)
     {
      cout<<"\n|-----------------------------------------------------------------------------|";
      cout<<"\n  航次: "<<p->getnu()<<"            起飞时间:"<<p->gett()<<"     总票数: "<<p->getsu();
      cout<<"\n  航线:"<<p->gets()<<"---> "<<p->gete()<<"     票价:"<<p->getf()<<"      剩余票数: "<<p->getn();
      cout<<"\n|---------------------------------------------------------------------------|\n";
      y=0;
     }
     p=p->getnext();
    }
    if(y)cout<<"失败,您查询的航班不存在!\n\n";
   }
   break;
  case'2':
      
    int y=1;
    cout<<"\n\n请输入要查找的目的地\n\n";
    cin>>m;
    while(p)
    {
     if(strcmp(p->gete(),m)==0)
     {
      cout<<"\n  航次: "<<p->getnu()<<"            起飞时间:"<<p->gett()<<"     总票数: "<<p->getsu();
      cout<<"\n  航线:"<<p->gets()<<"---> "<<p->gete()<<"     票价:"<<p->getf()<<"      剩余票数: "<<p->getn();
      cout<<"\n|---------------------------------------------------------------------------|\n";
      y=0;
     }
     p=p->getnext();
    }
    if(y)cout<<"失败,您查询的航班不存在!\n\n";
   }
   break;
  case'3':
   q->manage::run();
   break;
  }
 }
 while(c!='c'&&c!='C');
}


void manage::query2()  //查找乘客信息
{
 if(head1==NULL){cout<<"\n\n提示:无航班信息!";return ;}
 char c,m[20];
 do
 {
  cout<<setw(50)<<" │            [乘客查询菜单]        │\n";
  cout<<setw(50)<<" │    乘客查询菜单 1,并按回车键    │\n";
  cout<<setw(50)<<" │    乘客查询菜单 2,并按回车键    │\n";
  cout<<setw(50)<<" │            返回 3,并按回车键    │\n";
  cin>>c;
  switch (c)
  {
  case '1':
   {
    Plan *p1=head1;
    Client *p2=head2;
    int y1=1,y2=1;
    cout<<"\n\n请输入要查找的航班号\n\n";
    cin>>m;
    while(p1)
      
     if(strcmp(p1->getnu(),m)==0)
     {
      y1=0;
      cout<<"\n|---------------------------------------------------------------------------|\n";  
      cout<<"\n  航次: "<<p1->getnu()<<"            起飞时间:"<<p1->gett()<<"     总票数: "<<p1->getsu();
      cout<<"\n  航线:"<<p1->gets()<<"---> "<<p1->gete()<<"    剩余票数:"<<p1->getn();
      cout<<"\n|---------------------------------------------------------------------------|\n";
     }
     p1=p1->getnext();
    }
    if(y1)  cout<<"\n失败,您查询的航班不存在!\n";
    else{
     while(p2)
     {
      if(strcmp(p2->getnu(),m)==0)
      {
       cout<<"\n 乘客姓名:"<<p2->getna()<<"      所购机票数:"<<p2->getn();y2=0;
       cout<<"\n|---------------------------------------------------------------------------|\n";
      }
      p2=p2->getnext();
     }
     if(y2)cout<<"\n 无任何乘客! \n ";
    }
   }
   break;
  case '2':
   {
    Plan *p1=head1;
    Client *p2=head2;
    int y1=1,y2=1;
    cout<<"\n\n请输入要查询姓名:\n\n";
    cin>>m;
    while(p2)
    {
     if(strcmp(p2->getna(),m)==0)
     {
      cout<<"\n 乘客姓名:"<<p2->getna()<<"      所购机票数:"<<p2->getn();
      y2=0;y2=0;
      while(p1)
      {
       if(strcmp(p1->getnu(),p2->getnu())==0)
       {
        y1=0;
        cout<<"\n|---------------------------------------------------------------------------|\n";  
        cout<<"\n  航次: "<<p1->getnu()<<"            起飞时间:"<<p1->gett()<<"     总票数: "<<p1->getsu();
        cout<<"\n  航线:"<<p1->gets()<<"---> "<<p1->gete()<<"    剩余票数:"<<p1->getn();
        cout<<"\n|---------------------------------------------------------------------------|\n";
       }
       p1=p1->getnext();
      }
      if(y1) cout<<"失败,您查询的航班不存在!\n\n";
     }
     p2=p2->getnext();
    }
    if(y2)cout<<"失败,您查询的乘客不存在!\n\n";
   }
   break;
   // case '3':
   //  return;
  }
 }while(c!='C'&&c!='c');
}


void manage::baClient()
{
 if(head1==NULL){cout<<"\n\n提示:无航班信息!";return ;}
 if(head2==NULL){cout<<"\n\n提示:无乘客信息!";return ;}
 cout<<setw(20)<<"退机票\n";
 char m[30];
 int y1=1,y2=1;
 cout<<"\n请输入您所买的航班\n\n";
 cin>>m;
 Plan *p1=head1;
 while(p1)
 {
  if(strcmp(p1->getnu(),m)==0)
  {
   y1=0;
   cout<<"\n\n请输入您的姓名:\n\n";
   cin>>m;
   Client *p2=head2;
   while(p2)
   {
    if(strcmp(p2->getna(),m)==0)
    {
     y2=0;
loop:
     cout<<"\n您一共购买"<<p2->getn()<<"张机票!\n请输入您退的票数!\n";
     int s;cin>>s;
     if(p2->getn()<s)
     {cout<<"\n错误,您退的票数必须小于或者等于"<<p2->getn()<<"张!\n\n";goto loop;}
     else {p2->setn(p2->getn()-s);p1->setn(p1->getn()+s);}
    }
    p2=p2->getnext();
   }
   if(y2){cout<<"\n错误,您并未购买此航班机票!\n";return;}
  }
  p1=p1->getnext();
 }
 if(y1){cout<<"\n错误,无此航班!\n";return;}
 cout<<"\n退票成功!";
}


void manage::save()
{
 ofstream file("plane.txt",ios::in,filebuf::openprot);
 int n=0;
 Plan *p1=head1;
 while(p1){n++;p1=p1->getnext();}
 file.write((char*)&n,sizeof(n));
 p1=head1;
 while(p1)
 {
  file.write((char*)p1,sizeof(Plan));
  p1=p1->getnext();
 }
 cout<<"\n保存"<<n<<"条航班信息成功!";
 Client *p2=head2;
 n=0;
 while(p2){n++;p2=p2->getnext();}
 file.write((char*)&n,sizeof(n));
 p2=head2;
 while(p2)
 {
  file.write((char*)p2,sizeof(Client));
  p2=p2->getnext();
 }
 cout<<"\n保存"<<n<<"条乘客信息成功!";
 file.close();
}


void manage::run()
{
 cout<<setw(35)<<"欢迎使用航空订票系统,请选择您所需的操作              \n";
 cout<<setw(55)<<"                                 \n";
 cout<<setw(45)<<"  1,进入        \n";
 cout<<setw(55)<<"                                 \n";
 cout<<setw(45)<<"  2,退出        \n";
 int c;
 cin>>c;
 if(c==1)
 
  cout<<"\n\n\n\n\n\n\n\n\n\n";
  ini();
  char c1;
  do{
   cout<<setw(20)<<"                主      菜      单"<<endl;
   cout<<endl;
   cout<<setw(50)<<"     请选择您所需的操作               \n";
   cout<<setw(50)<<"                                      \n";
   cout<<setw(35)<<"1,信息显示 \n";
   cout<<setw(35)<<"2,买机票   \n";
   cout<<setw(35)<<"3,退机票   \n";
   cout<<setw(35)<<"4,信息查询 \n";
   cout<<setw(35)<<"5,系统管理 \n";
   cout<<setw(35)<<"6,清空数据 \n";
   cout<<setw(35)<<"0,退出     \n";
   cout<<endl;
   cin>>c1;
   switch ( c1 )
   {
   case '1':
    {
     char cc1;
     do{
      cout<<"导航菜单  1:航线导航 2:乘客导航 0:退出=>"<<endl;
      cin>>cc1;
      switch(cc1)
       
      case '1':
       show1();break;
      case '2':
       show2();break;
      case '0':
       break;    
      }
     }while(cc1!='0');
    }break;
   case '2':
    buy();break;
   case '3':
    baClient();break;
   case '4':
    {char cc1;
    do{
     cout<<"查询菜单  1:航线查询 2:乘客查询 0:退出=>"<<endl;
     cin>>cc1;
     switch(cc1)
     {
     case '1':
      query1();break;
     case '2':
      query2();break;
     case '0':break;
     }
    }while(cc1!='0');
    }break;
   case '5':
    
     char c2;
     do{
      cout<<"航班管理 1:新增 2:更改 3:删除 0:退出=>"<<endl;
      cin>>c2;
      switch (c2)
      {
      case '1':newf();break;
      case '2':modify();break;
      case '3':del();break;
      case '0':break;
      }
     }while(c2!='0');
    }break;
   case '6':
    delall();break;}
  }
  while(c1!='0');
  cout<<"\n\n 谢谢您的使用!";
  save();
 }
 else ;
}


void main()
{

 manage* p;
 p->run();
}

 

 

 

 

//JuLY/整理/06.02/。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有