票务管理系统

时间:2022-02-06 12:23:36

 

 

#include<fstream>

#include"conio.h"

#include<string>

#include<iostream>

#include<iomanip>

using namespace std;

struct date

{

         int year;

         int month;

         int day;

};

struct time

{

         int hour;

         int minute;

         int sec;

};

struct Route

{

    char number[10];//车次

         char begin_place[10];//始发点

         char end_place[10];//终点站

         char type[10];//车辆类型

         float length;//里程

         //date begin_time;

         time begin_time1;//出站时间

         //date end_time;

         time end_time1;//到站时间

         float money;//票价

         int seat_number;//座位数

};

class route//建立汽车路线10条路线

{

public:

         route();

         void input1();

         void output1();

         //void route_delete();

         //void route_updata();

         void route_serach();

         Route data;

//private:

         ofstream outfile1;

         fstream file1;

 

};

route::route()

{

         char a;

         cout<<"是否清空路线文件数据,第一次运行程序请新建文件数据(Y,N)"<<endl;

         while(1)

         {

                   cin>>a;

                   a=tolower(a);

                   if(a=='y'||a=='n')

                            break;

                   else

                            cout<<"输入数据有误,请重新输入:"<<endl;

                  

         }

         if(a=='y')

         {

                   outfile1.open("f1.bin",ios::out|ios::binary);

                   outfile1.close();

         }

         file1.open("f1.bin",ios::in|ios::out|ios::binary);

         system("cls");

}

void route::route_serach()

{

         cout<<"请输入汽车路线的车次"<<endl;

         char che_ci[10];

         cin>>che_ci;

         file1.seekg(0,ios::beg);

         while(!file1.eof())

         {

                   if(file1.peek()==EOF)

                            break;

                   file1.read((char*)&data,sizeof(data));

                   if(strcmp(data.number,che_ci)==0)

                   {

                            cout<<"请输入(时间有时分秒)\n车次  始发点  终点站  车辆类型  里程  出站时间  到站时间  票价  座位数"<<endl;

                            cout<<data.number<<"  "<<data.begin_place<<"  "<<data.end_place<<"  "<<data.type<<"  "<<data.length<<"  "<<data.begin_time1.hour<<":"

                            <<data.begin_time1.minute<<":"<<data.begin_time1.sec<<"  "<<data.end_time1.hour<<":"<<data.end_time1.minute<<":"<<data.end_time1.sec

                            <<"  "<<data.money<<"  "<<data.seat_number<<endl;

                            break;

                   }       

         }

         file1.clear();

}

 

 

void route::input1()

{

         file1.seekp(0,ios::end);

         cout<<"请输入你要添加数据的个数:"<<endl;

         int NUM;

         cin>>NUM;

         cout<<"请输入(时间有时分秒)\n车次  始发点  终点站  车辆类型  里程  出站时间  到站时间  票价  座位数"<<endl;

         for(int i=0;i<NUM;i++)

         {

         cin>>data.number>>data.begin_place>>data.end_place>>data.type>>data.length>>data.begin_time1.hour>>data.begin_time1.minute>>data.begin_time1.sec

                            >>data.end_time1.hour>>data.end_time1.minute>>data.end_time1.sec>>data.money>>data.seat_number;

                   file1.write((char*)&data,sizeof(data));

         }

         file1.clear();

}

void route::output1()

{

         file1.seekg(0,ios::beg);

         cout<<"输出信息为:"<<endl;

         while(!file1.eof())

         {

                   if(file1.peek()==EOF)

                            break;

                   file1.read((char*)&data,sizeof(data));

         cout<<data.number<<setw(4)<<data.begin_place<<setw(4)<<data.end_place<<setw(5)<<data.type<<setw(4)<<data.length<<setw(6)<<data.begin_time1.hour<<":"

                   <<data.begin_time1.minute<<":"<<data.begin_time1.sec<<setw(6)<<data.end_time1.hour<<":"<<data.end_time1.minute<<":"<<data.end_time1.sec

                            <<setw(4)<<data.money<<setw(4)<<data.seat_number<<endl;

         }

         file1.clear();

}

struct Ticket

{

         char number[10];//车次

         //date begin_time;

         time begin_time1;//时间

         int put_seat_amount;//已定票数

         int remain_seat_amount;//剩余票数

};

class ticket//建立与汽车路线相对应的票务信息

{

public:

         ticket();

         void input2();

         void output2();

         void ticket_serach();

         Ticket ll;

//private:

        

         ofstream outfile2;

         fstream file2;

};

ticket::ticket()

{

    char a;

         cout<<"是否清空票务文件数据,第一次运行程序请新建文件数据(Y,N)"<<endl;

         while(1)

         {

                   cin>>a;

                   a=tolower(a);

                   if(a=='y'||a=='n')

                            break;

                   else

                            cout<<"输入数据有误,请重新输入:"<<endl;

                  

         }

         if(a=='y')

         {

                   outfile2.open("f2.bin",ios::out|ios::binary);

                   outfile2.close();

         }

         file2.open("f2.bin",ios::in|ios::out|ios::binary);

         system("cls");

}

void ticket::ticket_serach()

{

         cout<<"请输入汽车路线的车次"<<endl;

         char che_ci[10];

         cin>>che_ci;

         file2.seekg(0,ios::beg);

         while(!file2.eof())

         {

                   if(file2.peek()==EOF)

                            break;

                   file2.read((char*)&ll,sizeof(ll));

                   if(strcmp(ll.number,che_ci)==0)

                   {

                            cout<<"车次  出发时间  已定票数  剩余票数"<<endl;

                            cout<<ll.number<<'\t'<<ll.begin_time1.hour<<":"<<ll.begin_time1.minute<<":"<<ll.begin_time1.sec

                              <<'\t'<<"  "<<ll.put_seat_amount<<'\t'<<"  "<<"  "<<ll.remain_seat_amount<<endl;

                            break;

                   }       

         }

         file2.clear();

}

void ticket::input2()

{

         file2.seekp(0,ios::end);

         cout<<"请输入你要添加数据的个数:"<<endl;

         int NUM;

         cin>>NUM;

         cout<<"请输入数据(时间包括时分秒)\n车次  出发时间  已定票数  剩余票数"<<endl;

         for(int i=0;i<NUM;i++)

         {

           cin>>ll.number>>ll.begin_time1.hour>>ll.begin_time1.minute>>ll.begin_time1.sec>>ll.put_seat_amount>>ll.remain_seat_amount;

                   file2.write((char*)&ll,sizeof(ll));

         }

         file2.clear();

}

void ticket::output2()

{

         file2.seekg(0,ios::beg);

         cout<<"输出信息为:"<<endl;

         while(!file2.eof())

         {

                   if(file2.peek()==EOF)

                                     break;

                   file2.read((char*)&ll,sizeof(ll));

                   cout<<ll.number<<setw(6)<<ll.begin_time1.hour<<":"<<ll.begin_time1.minute<<":"<<ll.begin_time1.sec

                            <<setw(6)<<ll.put_seat_amount<<setw(6)<<ll.remain_seat_amount<<endl;

                            //file2.clear();

         }

         file2.clear();

         //system("pause");

}

struct Information

{

         char name[10];//姓名

         char ID_CARD[20];//身份证号

         char number[10];//车次

         date DATE;//日期

         int seat_number;//座位号

};

class information//建立10条订票信息

{

public:

         information();

         void input3();

         void output3();

         void information_serach();

         Information bb;

//private:

        

         ofstream outfile3;

         fstream file3;

};

information::information()

{

         char a;

         cout<<"是否清空订票文件数据,第一次运行程序请新建文件数据(Y,N)"<<endl;

         while(1)

         {

                   cin>>a;

                   a=tolower(a);

                   if(a=='y'||a=='n')

                            break;

                   else

                            cout<<"输入数据有误,请重新输入:"<<endl;

                  

         }

         if(a=='y')

         {

                   outfile3.open("f3.bin",ios::out);

                   outfile3.close();

         }

         file3.open("f3.bin",ios::in|ios::out|ios::binary);

         system("cls");

}

void information::information_serach()

{

         cout<<"请输入汽车路线的姓名"<<endl;

         char Name[10];

         cin>>Name;

         file3.seekg(0,ios::beg);

         while(!file3.eof())

         {

                   if(file3.peek()==EOF)

                            break;

                   file3.read((char*)&bb,sizeof(bb));

                   if(strcmp(bb.name,Name)==0)

                   {

                            cout<<"姓名  身份证号  车次    日期   座位号"<<endl;

                         cout<<bb.name<<'\t'<<bb.ID_CARD<<'\t'<<bb.number<<'\t'<<bb.DATE.year<<"-"<<bb.DATE.month<<"-"<<bb.DATE.day<<'\t'<<"  "<<bb.seat_number<<endl;

                            break;

                   }       

         }

         file3.clear();

}

void information::input3()

{

         file3.seekp(0,ios::end);

         cout<<"请输入你要添加数据的个数:"<<endl;

         int NUM;

         cin>>NUM;

         cout<<"请输入数据(日期包含年月日)\n姓名  身份证号  车次    日期   座位号"<<endl;

         for(int i=0;i<NUM;i++)

         {

                   cin>>bb.name>>bb.ID_CARD>>bb.number>>bb.DATE.day>>bb.DATE.month>>bb.DATE.year>>bb.seat_number;

                   file3.write((char*)&bb,sizeof(bb));

         }

         file3.clear();

}

void information::output3()

{

         file3.seekg(0,ios::beg);

         cout<<"输出信息为:"<<endl;

         while(!file3.eof())

         {

                   if(file3.peek()==EOF)

                            break;

                   file3.read((char*)&bb,sizeof(bb));

         cout<<bb.name<<setw(8)<<bb.ID_CARD<<setw(8)<<bb.number<<setw(10)<<bb.DATE.year<<"-"<<bb.DATE.month<<"-"<<bb.DATE.day<<setw(8)<<bb.seat_number<<endl;

         }

         file3.clear();

}

class xi_tong

{

public:

         void show();

         void ding_piao();

         void tui_piao();

         void route_ticket_updata();

         void route_ticket_delete();

         void explain();

//protected:

         route aa;

         ticket bb;

         information cc;

};

void xi_tong::route_ticket_delete()

{

         ofstream outfile3("ff2.bin",ios::out|ios::binary);

         ofstream  outfile2("ff1.bin",ios::out|ios::binary);

         cout<<"请输入你要删除路线的车次"<<endl;

         char che[10];

         cin>>che;

         aa.file1.seekg(0,ios::beg);

         while(!aa.file1.eof())

         {

                   if(aa.file1.peek()==EOF)

                            break;

                   aa.file1.read((char*)&aa.data,sizeof(aa.data));

                   if(strcmp(aa.data.number,che)!=0)

                            outfile2.write((char*)&aa.data,sizeof(aa.data));

                   if(strcmp(aa.data.number,che)==0)

                   {

                            cout<<"要删除的路线为"<<endl;

                            cout<<aa.data.number<<"  "<<aa.data.begin_place<<"  "<<aa.data.end_place<<"  "<<aa.data.type<<"  "<<aa.data.length

                                     <<"  "<<aa.data.begin_time1.hour<<":"<<aa.data.begin_time1.minute<<":"<<aa.data.begin_time1.sec

                                     <<"  "<<aa.data.end_time1.hour<<":"<<aa.data.end_time1.minute<<":"<<aa.data.end_time1.sec

                                <<"  "<<aa.data.money<<"  "<<aa.data.seat_number<<endl;

                            bb.file2.seekg(0,ios::beg);

                            while(!bb.file2.eof())

                            {

                                     if(bb.file2.peek()==EOF)

                                               break;

                                     bb.file2.read((char*)&bb.ll,sizeof(bb.ll));

                                     if(strcmp(bb.ll.number,che)!=0)

                                               outfile3.write((char*)&bb.ll,sizeof(bb.ll));

                                     if(strcmp(bb.ll.number,che)==0)

                                     {

                                               cout<<"删除路线对应的票务信息为:"<<endl;

                                              cout<<bb.ll.number<<'\t'<<bb.ll.begin_time1.hour<<":"<<bb.ll.begin_time1.minute<<":"<<bb.ll.begin_time1.sec

                                                        <<'\t'<<"  "<<bb.ll.put_seat_amount<<'\t'<<"  "<<"  "<<bb.ll.remain_seat_amount<<endl;

                                     }

                            }

                            bb.file2.clear();

                 bb.file2.close();

                 outfile3.close();

                 remove("f2.bin");

                 rename("ff2.bin","f2.bin");

                 bb.file2.open("f2.bin",ios::in|ios::out|ios::binary);

                 bb.file2.clear();

                   }

         }

         aa.file1.clear();

         aa.file1.close();

         outfile2.close();

         remove("f1.bin");

         rename("ff1.bin","f1.bin");

         aa.file1.open("f1.bin",ios::in|ios::out|ios::binary);

         aa.file1.clear();

}

void xi_tong::route_ticket_updata()

{

         cout<<"请输入你要更新路线的车次"<<endl;

         char che[10];

         cin>>che;

         aa.file1.seekg(0,ios::beg);

         while(!aa.file1.eof())

         {

                   aa.file1.read((char*)&aa.data,sizeof(aa.data));

                   if(strcmp(aa.data.number,che)==0)

                            break;

         }

         if(!aa.file1.eof())

         {

                   bb.file2.seekg(0,ios::beg);

                   while(!bb.file2.eof())

                   {

                   bb.file2.read((char*)&bb.ll,sizeof(bb.ll));

                   if(strcmp(bb.ll.number,aa.data.number)==0)

                            break;

                   }

                   if(!bb.file2.eof())

                   {

                            cout<<"请输入数据(时间包括时分秒)\n车次  出发时间  已定票数  剩余票数"<<endl;

                       cin>>bb.ll.number>>bb.ll.begin_time1.hour>>bb.ll.begin_time1.minute>>bb.ll.begin_time1.sec>>bb.ll.put_seat_amount

                                     >>bb.ll.remain_seat_amount;

                            cout<<endl;

            bb.file2.seekp(-sizeof(bb.ll),ios::cur);

                       bb.file2.write((char*)&bb.ll,sizeof(bb.ll));

                   }

 

                   cout<<"更新路线 请输入(时间有时分秒)\n车次  始发点  终点站  车辆类型  里程  出站时间  到站时间  票价  座位数"<<endl;

                  cin>>aa.data.number>>aa.data.begin_place>>aa.data.end_place>>aa.data.type>>aa.data.length>>aa.data.begin_time1.hour

                            >>aa.data.begin_time1.minute>>aa.data.begin_time1.sec

                            >>aa.data.end_time1.hour>>aa.data.end_time1.minute>>aa.data.end_time1.sec>>aa.data.money>>aa.data.seat_number;

                   aa.file1.seekp(-sizeof(aa.data),ios::cur);

                   aa.file1.write((char*)&aa.data,sizeof(aa.data));

         }

         aa.file1.clear();

 

}

void xi_tong::ding_piao()

{

 

         cout<<"请输入你要到达的地方:"<<endl;

         char place[10];

         cin>>place;

         aa.file1.seekg(0,ios::beg);

         while(!aa.file1.eof())

         {

                   if(aa.file1.peek()==EOF)

                            break;

                   aa.file1.read((char*)&aa.data,sizeof(aa.data));

                   if(strcmp(aa.data.end_place,place)==0)

                   {

                            bb.file2.seekg(0,ios::beg);

                            while(!bb.file2.eof())

                            {

                                     bb.file2.read((char*)&bb.ll,sizeof(bb.ll));

                                     if(strcmp(aa.data.number,bb.ll.number)==0)

                                               break;

                            }

                            if(!bb.file2.eof())

                            {

                                               if(bb.ll.remain_seat_amount>0)

                                               {

                                                        cout<<"请输入订票信息:"<<endl<<endl;

                                                        cout<<"请输入数据(日期包含年月日)\n姓名  身份证号  车次    日期   座位号"<<endl;

                                               cin>>cc.bb.name>>cc.bb.ID_CARD>>cc.bb.number>>cc.bb.DATE.day>>cc.bb.DATE.month>>cc.bb.DATE.year>>cc.bb.seat_number;

                                                        cc.file3.seekp(0,ios::end);

                                   cc.file3.write((char*)&cc.bb,sizeof(cc.bb));

                                                        cout<<"订票成功!!!"<<endl;

                                                        bb.ll.put_seat_amount++;

                                                        bb.ll.remain_seat_amount--;

                                                        bb.file2.seekp(-sizeof(bb.ll),ios::cur);

                                                        bb.file2.write((char*)&bb.ll,sizeof(bb.ll));

                                               }

                                               else

                                                        cout<<"客满!!!"<<endl;

                            }

                            bb.file2.clear();

                   }

         }

         aa.file1.clear();

}

void xi_tong::tui_piao()

{

         ofstream outfile5("fff3.bin",ios::out|ios::binary);

         cout<<"请输入你要退票的车次"<<endl;

         char num[10];

         cin>>num;

         bb.file2.seekg(0,ios::beg);

         while(!bb.file2.eof())

         {

                   bb.file2.read((char*)&bb.ll,sizeof(bb.ll));

                   if(strcmp(bb.ll.number,num)==0)

                            break;

         }

         if(strcmp(bb.ll.number,num)==0)

         {

                   cc.file3.seekg(0,ios::beg);

                   while(!cc.file3.eof())

                   {

                            if(cc.file3.peek()==EOF)

                                     break;

                            cc.file3.read((char*)&cc.bb,sizeof(cc.bb));

                            if(strcmp(cc.bb.number,bb.ll.number)!=0)

                                     outfile5.write((char*)&cc.bb,sizeof(cc.bb));

                            if(strcmp(cc.bb.number,bb.ll.number)==0)

                            {

                                     cout<<"你退票的信息:"<<endl;

                                     cout<<cc.bb.name<<setw(8)<<cc.bb.ID_CARD<<setw(8)<<cc.bb.number<<setw(10)<<cc.bb.DATE.year

                                     <<"-"<<cc.bb.DATE.month<<"-"<<cc.bb.DATE.day<<setw(8)<<cc.bb.seat_number<<endl;

                            }

                   }

                   cc.file3.clear();

             cc.file3.close();

             outfile5.close();

             remove("f3.bin");

 

             rename("fff3.bin","f3.bin");

             cc.file3.open("f3.bin",ios::in|ios::out|ios::binary);

             cc.file3.clear();

        

                   bb.ll.put_seat_amount--;

                   bb.ll.remain_seat_amount++;

                   bb.file2.seekp(-sizeof(bb.ll),ios::cur);

                   bb.file2.write((char*)&bb.ll,sizeof(bb.ll));

                   cout<<"退票成功!!!"<<endl<<endl;

                  

         }

         else

                   cout<<"输入车次有误!!!"<<endl;

         bb.file2.clear();

}

void xi_tong::explain()

{

         cout<<"系统说明:"<<endl;

         cout<<"① 该系统建有三个文件,分别存储路线信息.票务信息.订票信息"<<endl<<endl;

         cout<<"② 该系统会提示你是否清空以前运行的三个文件的数据信息"<<endl<<endl;

         cout<<"③ 该系统可以连续运行.连续添加数据信息."<<endl<<endl;

         cout<<"④ 请在添加路线信息时别忘了建立对应的票务信息"<<endl<<endl;

         cout<<"请求:"<<endl;

         cout<<"⑤ 若你在使用时发现了错误,请与我说明情况,我会完善这个程序"<<endl<<endl;

         cout<<"QQ 993636112"<<endl<<endl;

}

 

void xi_tong::show()

{

         int tt;int num1,num2,num3;int NUM1=sizeof(aa.data),NUM2=sizeof(bb.ll),NUM3=sizeof(cc.bb);

         while(1)

         {

                   aa.file1.seekp(0,ios::end);

                   bb.file2.seekp(0,ios::end);

                   cc.file3.seekp(0,ios::end);

                   num1=aa.file1.tellp();

                   num2=bb.file2.tellp();

                   num3=cc.file3.tellp();

                   cout<<"系统正在进入主菜单(可连续添加数据在文件末尾)..."<<endl<<endl;

                   system("pause");

                   system("cls");

             cout<<" ******************************************************************************"<<endl;

        cout<<"                                 欢迎使用票务管理系统                          "<<endl;

             cout<<" ******************************************************************************"<<endl;

                   cout<<" *                                                                            *"<<endl;

        cout<<" *       1.★添加汽车路线                 2.★添加票务信息                    *"<<endl;

        cout<<" *       3.★添加订票信息                 4.★查找指定路线                    *"<<endl;

                   cout<<" *       5.★查找票务信息                 6.★查找订票情况                    *"<<endl;

                   cout<<" *       7.★删除指定路线                 8.★修改路线信息                    *"<<endl;

                   cout<<" *       9.★退票                         10.★订票                           *"<<endl;

                   cout<<" *       11.★输出路线信息                12.★输出票务信息                   *"<<endl;

                   cout<<" *       13.★输出订票信息                14.★系统说明                       *"<<endl;

                   cout<<" *       15.★退出系统                                                        *"<<endl;

        cout<<" *                                                                            *"<<endl;

                   cout<<" ******************************************************************************"<<endl;

             cout<<" ***************************"<<"目前路线文件共有"<<setw(2)<<num1/NUM1<<"个数据"<<"***************************"<<endl<<endl;

                   cout<<" ***************************"<<"目前票务文件共有"<<setw(2)<<num2/NUM2<<"个数据"<<"***************************"<<endl<<endl;

                   cout<<" ***************************"<<"目前订票文件共有"<<setw(2)<<num3/NUM3<<"个数据"<<"***************************"<<endl<<endl;

        cout<<"请输入1-14之间的任意一数字:"<<endl;

                   cin>>tt;

                   system("cls");

                   switch(tt)

                   {

                   case 1:

                            aa.input1();

                            break;

                   case 2:

                            bb.input2();

                            break;

                   case 3:

                            cc.input3();

                            break;

                   case 4:

                            aa.route_serach();

                            break;

                   case 5:

                            bb.ticket_serach();

                            break;

                   case 6:

                            cc.information_serach();

                            break;

                   case 7:

                            route_ticket_delete();

                            break;

                   case 8:

                            route_ticket_updata();

                            break;

                   case 9:

                            tui_piao();

                            break;

                   case 10:

                            ding_piao();

                            break;

                   case 11:

                            aa.output1();

                            break;

                   case 12:

                            bb.output2();

                            break;

                   case 13:

                            cc.output3();

                            break;

                 case 14:

                            explain();

                            break;

                   case 15:

                            aa.file1.close();

                            bb.file2.close();

                            cc.file3.close();

                            exit(0);

                            break;

                   default:

                            cout<<"输入数据有误"<<endl;

                            system("pause");

                            exit(0);

                   }

         }

}

void main()

{

         system("title 文件写的系统-王新");

    //system("mode con cols=90 lines=40");

         system("color 3a");

         cout<<"请输入系统密码:wangxin"<<endl;

         char b[8];char aa[8]="wangxin";

         for(int i=0;i<7;i++)

         {

                   b[i]=_getch();

                   _putch('*');

         }

         b[7]='\0';

    if(strcmp(aa,b)!=0)

         {

                   cout<<endl<<"输入密码错误!!!"<<endl<<endl;

                   exit(0);

         }

         else

                   cout<<endl<<"密码以接受!!!"<<endl<<endl;

         system("pause");

         system("cls");

         xi_tong a;

         a.show();

 

}

 

 

/*

A2  nc  hz  car  87  8  30  22  15  20  31  76  60 

A3  jg  hi  car  77  9  30  10  15  22  37  88  60 

S3  dg  si  car  97  6  30  30  15  22  37  122 60

D4  yu  ng  car  78  10 30  10  15  22  37  50  60 

A5  ng  ou  car  87  8  30  22  15  22  37  76  60 

F3  jg  hi  car  77  9  30  10  16  34  56  88  60

S6  dg  si  car  137 6  30  30  17  17  23  122 60

D7  yi  ng  car  78  13 30  10  19  12  34  50  60

S2  dg  si  car  137 7  30  30  20  20  20  122 60

D1  yu  ng  car  78  8  30  10  21  21  21  50  60

 

 

 

 

 

A2  8  30  22  55  5

A3  9  30  10  49  11

S3  6  30  30  58  2

D4  10 30  10  50  10

A5  8  30  22  44  16

F3  9  30  10  37  23

S6  6  30  30  47  13

D7  13 30  10  59  1

S2  7  30  30  54  6

D1  8  30  10  50  10

 

 

 

 

 

 

 

 

 

 

hq  36  D4  10  30  10  47

lb  27  A2  8   30  22  2

wx  33  A3  9   30  10  27

jz  17  S3  6   30  30  33

 

 

 

 

 

 

 

 

*/