C++ 课程设计之车票管理系统 - 无幻の编程 - 对于一个初学者来说,野心也是必须的...

C++ 课程设计之车票管理系统

无幻 posted @ 2009年3月29日 09:42 in C++编程 with tags c++ 车票管理 课程设计 , 2039 阅读

 

车票管理系统
设计说明与要求:
一车站每天有n个发车班次,每个班次都有一班次号(1、2、3…n),固定的发车时间,固定的路线(起始站、终点站),大致的行车时间,固定的额定载客量。如
班次   发车时间   起点站   终点站   行车时间   额定载量      已定票人数
1      8:00       郫县      广汉     2       .   45            30
2      6:30       郫县      成都     0.5          40            40
3      7:00       郫县      成都     0.5          40            20
4      10:00      郫县      成都     0.5          40            2
功能要求:用c/c++设计一系统,能提供下列服务:
(1)录入班次信息(信息用文件保存),可不定时地增加班次数据
(2)浏览班次信息,可显示出所有班次当前状况(如果当前系统时间超过了某班次的发车时间,则显示“此班已发出”的提示信息)。
(3)查询路线:可按班次号查询 ,可按终点站查询
(4)售票和退票功能
            A:当查询出已定票人数小于额定载量且当前系统时间小于发车时间时才能售票,自动更新已售票人数
B:退票时,输入退票的班次,当本班车未发出时才能退票,自动更新已售票人数


 

 

完整代码如下:

#include<iostream>          //数据流输入/输出
#include<fstream>                     //文件输入/输出
#include<string>                                //字符串操作
#include<iomanip>                     //参数化输入/输出
#include<time.h>                                //时间库函数
using namespace std;        //命名空间

class Bus_infor
{
private:       
    static int Bus_No;    //静态数据成员,统计当前所有的Bus_infor对象的数目
        char start[20];   //起点站
        char end[20];         //终点站
        int Bus_order;      //班次号
    int all_tickted;            //额定载量
        int tickted;                //已定票人数
        int  Hour_start,Minute_start;         //发车时间
        float GoHour;         //行车时间
       
public
        Bus_infor();
        ~Bus_infor();
        Bus_infor *next;
        void input();            //录入函数
    void input(ifstream & is);      //读取函数
    void output();                  //输出函数
        void output(ofstream & os);          //写入函数
        void Order_tickt(int n);                                //定票函数
        void Unorder_tickt(int n);                  //退票函数
        void GetTime_start();            //获取发车时间函数
        bool GetTime();      //判断当前班次状况函数
        int Get_all_tickted()  { return all_tickted;  }            //内联函数,返回额定载量
    int Get_tickted()  {        return tickted; }                            //返回已定票人数
        int Get_bus_order()  { return Bus_order;  }               //返回班次号
        string Get_end()const;        //返回终点站的字符串
};

int Bus_infor::Bus_No=1;

Bus_infor::Bus_infor()
{
    Bus_No++;
    tickted=0;
}

Bus_infor::~Bus_infor()
{
    Bus_No--;
}

void Bus_infor::input()
{
        cout<<"\t\t\t按提示输入:"<<endl;
                cout<<"输入班次: ";
                while(1)
                {
                        cin>>Bus_order;
                        if (cin.fail())      //判断输入的数据类型是否有错
                        {
                                cout << "\n班次输入错误,请重新输入:";
                                cin.clear();
                                cin.get();                 
                        }
                        else
                                break;
                }
                cout<<"请输入车的额定座位数: ";              
                while(1)
                {
                        cin>>all_tickted;
                        if (cin.fail())      //判断输入的数据类型是否有错
                        {
                                cout << "\n座位数输入错误,请重新输入:";
                                cin.clear();
                                cin.get();                 
                        }
                        else
                                break;
                }
                GetTime_start();
                cout<<"请输入行车时间:";              
                while(1)
                {
                        cin>>GoHour;
                        if (cin.fail())      //判断输入的数据类型是否有错
                        {
                                cout << "\n行车时间输入错误,请重新输入:";
                                cin.clear();
                                cin.get();                 
                        }
                        else
                                break;
                }
                cout<<"请输入起始站与终点站:";     
        cin>>start;cin>>end;       
                cout<<"是否清空售票(y/n)?";
                char a;cin>>a;
                if(a=='y'||a=='Y') tickted=0;
}

void Bus_infor::input(ifstream & is)                           
{
        is>>Bus_order>>Hour_start>>Minute_start>>start>>end>>GoHour>>all_tickted>>tickted;
        is.get();                                   
}

void  Bus_infor::output()
{
               
        cout<<" "<<Bus_order<<"\t";
        if(Minute_start==0)                              //判断发车时的分钟时刻,若为0分则在后面多显示个0,以符合时间格式
        {
                cout<<Hour_start<<":"<< Minute_start<<"0\t";
        }
        else
        {
                cout<<Hour_start<<":"<< Minute_start<<"\t";
        }
        cout<<start<<"\t"<<end<< "\t"<<GoHour<<"\t   "<<all_tickted<<"\t     "<<tickted;               
    if(!GetTime())
                cout<<"\t   此班已出发"<<endl;
        else
                cout<<"\t   此班未出发"<<endl;
}

void Bus_infor::output(ofstream & os)                     
{
        os<<setw(6)<<Bus_order        //setw()设置输出宽度
                <<setw(15)<<Hour_start
                <<setw(15)<<Minute_start
                <<setw(15)<<start
                <<setw(6)<<end
                <<setw(15)<<GoHour
                <<setw(15)<<all_tickted
                <<setw(15)<<tickted          
                <<endl;
}

void Bus_infor::GetTime_start()
{       
        cout<<"请输入始发时间(时 分):";
        while(1)
        {              
                cin>>Hour_start>>Minute_start;
                if (cin.fail())      //判断输入的数据类型是否有错
                        {
                                cout << "\n时间输入错误,请重新输入:";
                                cin.clear();
                                cin.get();                 
                        }                     
                else if(Hour_start<0||Hour_start>24||Minute_start<0||Minute_start>60)            
                        cout<<"\n时间格式出错,请重新输入:";      ////判断时间格式是否出错,小时不能小于0大于24,分钟不能小于0大于60
                else
                        break;
        }       
}

bool Bus_infor::GetTime()
{
        struct tm *local; 
    time_t t;
    t=time(NULL);
    local=localtime(&t);                        //获取当前系统时间
        if(local->tm_hour<Hour_start||(local->tm_hour==Hour_start && local->tm_min<=Minute_start))
                return 1;                      //比较当前时间与发车时间,获得班次的当前状况,返回1表示班次未出发
        else
                return 0;                                          //返回0表示班次已出发
}

void Bus_infor::Order_tickt(int n)
{
        tickted=tickted+n;
}

void Bus_infor::Unorder_tickt(int n)
{
        tickted=tickted-n;
}

string Bus_infor::Get_end()const
{
        string s=end;
        return s;
}

class Bus_link
{
public:
        Bus_link(){head=new Bus_infor;head->next=NULL;key=0;}         //带参数的构造函数
        ~Bus_link(){delete head;}                     //析构函数
        void input();         //录入车票信息       
        void mend();            //修改车票信息
        void del();                    //删除车票信息
        int find(Bus_infor **p1,int num,char *pn);            //查找函数,找出所有符合的   
        int find1(Bus_infor **p1,int num,char *pn);   //查找函数,找到符合的返回 
    void found();                     //查询车票信息
        void show();            //显示车票信息
    void Order();                     //定购车票信息
    void Unorder();          //退还车票信息
    void save();                        //保存车票信息
    void begin();                     //初始化车票信息
    void clear();                     //清除函数
        void about();      //关于车票信息
    char mainmenu();                //主菜单函数
    void setkey(int k){ key=k; }                        //设置系统修改标志
         int getkey(){ return key;}               //返回系统修改标志
private:                                           
    Bus_infor *head;       //链表指针                     
        int key;                                                //系统修改标志
        int password;         //管理员登陆标志
};

void Bus_link::input()
{
        if(password==1)
        {
                Bus_infor *p,*p2=NULL;
                p=head;                                         
                int n=1;   
                while(p->next)
                        p=p->next;
                while(n)
                {
                        p2=new Bus_infor;
                        p2->input();
                        p->next=p2;
                        p2->next=NULL;
                        p=p->next;                                   
                        Bus_link::setkey(1);
                        cout<<"\t\t\t按1继续,按0返回 : ";
                        cin>>n;
                        if(!cin)
                                throw string("\n数据输入错误");
                        Bus_link::setkey(1);
                }
        }
        else
                cout<<"\n\t\t对不起,游客不能录入车票信息"<<endl;
}

void Bus_link::show()
{
        cout<<"客车基本信息如下:"<<endl
                   <<"班次 发车时间  起点站  终点站  行车时间 额定载量 已定票人数 当前状况"<<endl;
        Bus_infor *p;
        p=head;
        while(p->next)
        {
                (p->next)->output();
                p=p->next;
        }
}

void Bus_link::found()
{
        Bus_infor *p;
        int num,n;
        char name[20];
        do
        {
                cout<<"\t\t1:按班次查找,2:按终点站查找: ";
                cin>>n;
                if(!cin)
                                throw string("\n数据输入错误");
        }while(n<1||n>2);
        if(n==1)
        {
                cout<<"\t\t\t输入班次: ";
                cin>>num;
                if(!cin)
                                throw string("\n数据输入错误");
        }
        if(n==2)
        {
                cout<<"\t\t\t输入终点站: ";
                cin>>name;
        }
        if(!find(&p,num,name))
        {
                cout<<"\t\t找不到你要查找的内容!"<<endl;
                return;
        }
}

int Bus_link::find(Bus_infor **p1,int num,char *pn)
{
        Bus_infor *p;   
        p=head; 
        int t=0;       
        while(p->next)
        {              
                (*p1)=p;
                if( (p->next)->Get_bus_order()==num|| (p->next)->Get_end()==pn )
                {                     
                        cout<<"客车基本信息如下:"<<endl
                                   <<"班次 发车时间  起点站  终点站  行车时间 额定载量 已定票人数 当前状况"<<endl;     
                        (p->next)->output();          
                        t=1;                   
                }              
                p=p->next;
        }
        return t;
}

int Bus_link::find1(Bus_infor **p1,int num,char *pn)
{
        Bus_infor *p;   
        p=head;  
        while(p->next)
        {              
                (*p1)=p;
                if( (p->next)->Get_bus_order()==num|| (p->next)->Get_end()==pn )
                {                     
                        cout<<"客车基本信息如下:"<<endl
                                   <<"班次 发车时间  起点站  终点站  行车时间 额定载量 已定票人数 当前状况"<<endl;     
                        (p->next)->output();          
                        return 1;                                   
                }              
                p=p->next;
        }
        return 0;
}

void Bus_link::del()
{
        if(password==1)
        {
                Bus_infor *p,*p2;
                int num;char name[20];
                cout<<"\t\t\t输入班次号: ";
                cin>>num;              
                if(!cin)
                                throw string("\n数据输入错误");
                if( !find1(&p,num,name) )
                {
                        cout<<"\t\t找不到你要删除的内容!"<<endl;
                        return;
                }       
                cout<<"\n\t\t\t确定删除(y/n)?";
                char a;cin>>a;
                if(a=='y'||a=='Y')
                {
                        p2=p->next;          
                        p->next=p2->next;
                        delete p2;
                        Bus_link::setkey(1);
                }
        }
        else
                cout<<"\n\t\t对不起,游客不能删除车票信息"<<endl;
}

void Bus_link::mend()
{
        if(password==1)
        {
                Bus_infor *p;
                int num;
                char name[20];   
                cout<<"\t\t\t输入班次号: ";
                cin>>num;       
                if(!cin)
                                throw string("\n数据输入错误");
                if( !find1(&p,num,name) )
                {
                        cout<<"\t\t找不到你要修改的内容!"<<endl;
                        return;
                }              
                (p->next)->input();
                Bus_link::setkey(1);
        }
        else
                cout<<"\n\t\t对不起,游客不能修改车票信息"<<endl;
}

void Bus_link::Order()
{
        if(password==1)
        {
                Bus_infor *p;
                cout<<"\n\t\t\t确定购票(y/n)?";
                char X;cin>>X;
                if(X=='y'||X=='Y'){
                        int num;
                        cout<<"\n\t\t\t输入班次号: ";
                        cin>>num;
                        if(!cin)
                                throw string("\n数据输入错误");
                        if( !find1(&p,num,"^") )
                        {
                                cout<<"\n\t\t找不到你要定票的车辆的内容!"<<endl;
                                return;
                        }
                        p=p->next;
                        if(!(p->GetTime()))                    //判断要定票的车辆是否已经出发,若已经出发则不允许定票
                        {
                                cout<<"\n\t\t你要订票的车辆已出发!"<<endl;
                                return;
                        }
                        cout<<"\n\t\t\t输入要定的票数 ";
                        int n;cin>>n;
                        if(!cin)
                                throw string("\n数据输入错误");
                        if((p->Get_tickted()+n)<=p->Get_all_tickted())
                                p->Order_tickt(n);
                        else cout<<"\n\t\t对不起,没有足够的票数。"<<endl; 
                }
                else if(X=='n'||X=='N') cout<<"谢谢使用"<<endl;
                else cout<<"\n\t\t\t输入字符不确定"<<endl;
                Bus_link::setkey(1);
        }
        else
                cout<<"\n\t\t对不起,订购车票请在管理员处购买"<<endl;
}

void Bus_link::Unorder()
{
        if(password==1)
        {
                Bus_infor *p;
                cout<<"\n\t\t\t确定退票(y/n)?";
                char X;cin>>X;
                if(X=='y'||X=='Y'){
                        int num;
                        cout<<"\n\t\t\t输入班次号: ";
                        cin>>num;
                        if(!cin)
                                throw string("\n数据输入错误");
                        if( !find1(&p,num,"^") )
                        {
                                cout<<"\n\t\t找不到你要退票的车辆的内容!"<<endl;
                                return;
                        }
                        p=p->next;
                        if(p->GetTime()==0)                              //判断要定票的车辆是否已经出发,若已经出发则不允许定票
                        {
                                cout<<"\n\t\t你要退票的车辆已出发!"<<endl;
                                return;
                        }
                        cout<<"\n\t\t\t输入要退的票数 ";
                        int n;cin>>n;
                        if(!cin)
                                throw string("\n数据输入错误");         
                        if((p->Get_tickted()-n)>=0)
                                p->Unorder_tickt(n);
                        else cout<<"\n\t\t\t对不起,数据出错!。"<<endl;     
                }
                else if(X=='n'||X=='N') cout<<"谢谢使用"<<endl;
                else cout<<"\n\t\t\t输入字符不确定"<<endl;
                Bus_link::setkey(1);
        }
        else
                cout<<"\n\t\t对不起,退还车票请在管理员处退还"<<endl;
}

void Bus_link::save()
{
        if(password==1)
        {
                Bus_infor *p;
                p=head;
                ofstream os("bus.txt",ios::out);                        //文件以输出方式打开
                if (Bus_link::getkey()==1)
                {
                        while(p->next)
                        {
                                (p->next)->output(os);
                                p=p->next;
                        }
                }
                cout<<"\t\t\t文件已保存! "<<endl;
                Bus_link::setkey(0);
        }
        else
                cout<<"\n\t\t对不起,游客无法保存车票信息"<<endl;
}

void Bus_link::about()
{
        cout<<endl<<"关于车票管理系统"<<endl<<"────────"<<endl;
        cout<<"使用说明:"<<endl
                <<"\t1.请按照操作提示输入正确的格式,以保证系统正常运行;"<<endl
                <<"\t2.当使用管理员登陆时,需输入密码,可进行对系统的所有操作;"<<endl
        <<"\t3.当使用游客身份登陆时,无需输入密码,但只能浏览和查询车票信息;"<<endl
        <<"\t4.车票信息用文本文档格式,保存在本程序文件夹目录下,可以直接打开查看."<<endl<<endl
        <<"系统说明:"<<endl
                <<"\t本系统为课程设计作品,可以简易的进行车票管理,欢迎提出意见和建议"<<endl                
                   <<"\t漳州师范学院07级计算机科学与工程系\t"<<endl<<endl;
}

void Bus_link::begin()
{
        password=0;
        Bus_infor *p,*p2;
        p=head;
        clear();
        long t;
        ifstream is("bus.txt",ios::in);   //文件以输入方式打开
        if(!is)
        {
                ofstream os("bus.txt",ios::out);                //文件以输出方式打开
                os.close();               //关闭文件
                return ;
        }
        int num=-1;
        while(1)
        {
        num=-1;
                t=is.tellg();         //记录下当前位置
                is>>num;
                is.seekg(t);            //移动到原来位置
                if(num<0)
                {   
                        is.close();
                        return;
                }
                p2=new Bus_infor;
                p2->input(is);    //输入is对象内容
                p->next=p2;
                p2->next=NULL;
                p=p->next;
        }
}

void Bus_link::clear()
{
        Bus_infor *p,*p2;
        p=head->next;
        while( p )
        {
                p2=p;
                p=p->next;
                delete p2;
        }
}

char Bus_link::mainmenu()
{
        struct tm *local;
         char s1[128];
        time_t t;
        t=time(NULL);
        local=localtime(&t);
        strftime(s1,128,"%Y-%m-%d %H:%M ",local);                            //按照指定的格式,把时间保存在s1字符串里面
        string s;                                   //定义字符串s,来判断功能选择是否输入错误
        cout<<"\n\n          ────欢迎使用车票管理系统────"<<endl<<endl;
        cout
                <<"  ┌─────────────────────────┐"<<endl
                <<"  │ ┌──────────────────────┐ │"<<endl
                <<"  │ │  1.    录入车票信息    2.    浏览车票信息  │ │"<<endl
                <<"  │ │  3.    查询车票信息    4.    删除车票信息  │ │"<<endl
                <<"  │ │  5.    修改车票信息    6.    定购车票信息  │ │"<<endl
                <<"  │ │  7.    退还车票信息    8.    保存车票信息  │ │"<<endl
                <<"  │ │  9.    关于车票系统    0.    退出系统      │ │"<<endl
                <<"  │ └──────────────────────┘ │"<<endl
                <<"  └─────────────────────────┘"<<endl
                <<"\t\t\t\t       "<<s1<<endl<<endl;   
        while(password==0)
        {
                cout<<"\t\t请选择用户名(1.管理员;2.游客):  ";
                string n;       cin>>n;               
                if(n=="1")
                {
                        cout<<"\n\t\t请输入管理员密码:  ";
                        string m;cin>>m;                               
                        if(m=="123456")
                        {
                                password=1;
                                cout<<endl;
                                break;
                        }
                        else
                        {
                                cout<<"\n\t\t密码输入不正确\n"<<endl;
                        }
                }
                else
                {
                        password=2;               //游客身份标志
                        break;
                }
        }
        cout<<"                     请选择功能按钮: ";
        while(true)
        {
                cin>>s;
                if(s.length()!=1||s[0]<'0'||s[0]>'9')          //s.length()返回字符串的长度,即字符个数
                        cout<<"输入错误,请重新选择功能按钮: ";
                else
                        break;
        }
        return s[0];
}

int main()
{       
        Bus_link pp;
        int k=1;
        int i=1;
        string s;
        pp.begin();
        try
        {
                 while(k==1)
                 {
                         system("cls");
                         s=pp.mainmenu();                       //调用主菜单函数
                         switch(s[0])
                         {
                         case '1':pp.input(); break;            //录入车票信息
                         case '2':pp.show(); break;           //浏览车票信息
                         case '3':pp.found(); break;          //查询车票信息
                         case '4':pp.del(); break;              //删除车票信息
                         case '5':pp.mend(); break;          //修改车票信息
                         case '6':pp.Order(); break;          //保存车票信息
                         case '7':pp.Unorder(); break;      //退还车票信息
                         case '8':pp.save(); break;           //保存车票信息
                         case '9':pp.about();break;             //关于车票系统
                         case '0':k=0;break;                    //退出系统
                         }
                         if(k==1)
                         {
                                 cout<<"\n\t\t\t是否返回主菜单?   1.是  2.不是 : ";
                                 cin>>i;
                                 if(!cin)
                                        throw string("\n数据输入错误");
                         }
                 if(k==0||i==2)
                 {
                         if(pp.getkey()==1)
                         {
                                 cout<<"\t\t\t是否保存?  1 . 保存 0.不保存 : ";
                                 cin>>i;       
                                 if(!cin)
                                        throw string("\n数据输入错误");
                                 if(i==1)
                                         pp.save();
                                 pp.clear();
                                 k=0;
                         }     
                          k=0;
                 }           
                 }
        }
        catch(string s)
        {
                cout<<s<<",为保护系统不崩溃,将自动退出系统!"<<endl;
                system("pause");               
        }
 return 0;
}
  • 无匹配
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee