程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++實現汽車訂票系統(C++課程設計)

C++實現汽車訂票系統(C++課程設計)

編輯:C++入門知識

[html] 
#include<iostream> 
#include<fstream> 
#include<cstring> 
#include<iomanip> 
#define Maxsize 100 
using namespace std; 
 
struct Route 

    char number[20]; 
    char startandreach[20]; 
    char type[20]; 
    char longroute[20]; 
    char time[20]; 
    double price; 
    int sitnumber; 
    int save; 
    int sale; 
}; 
 
struct Ticketnews 

  char type[20]; 
  char time[20]; 
  int booked; 
  int remained; 
}; 
 
struct Booknews 

    char name[20]; 
    char identitycard[20]; 
    char number[20]; 
    char date[20]; 
    char road[20]; 
    int sitnumber; 
}; 
 
void Match(Route L1[],Ticketnews L2[],int n)//票務信息保存 

    ofstream outfile("票務信息.txt",ios::out); 
 if (!outfile) 

   cerr<<"open error!"<<endl; 
   exit(1); 

    int i; 
    for(i=0;i<n;i++) 
    { 
    strcpy(L2[i].type,L1[i].type); 
    strcpy(L2[i].time,L1[i].time); 
    L2[i].booked=L1[i].sale; 
    L2[i].remained=L1[i].save; 
    outfile<<L2[i].type<<" "<<L2[i].time<<" "<<L2[i].booked<<" "<<L2[i].remained<<endl; 
    } 
    outfile.close(); 

 
 
int Book(Booknews &L1,Route &L2,int s)//這裡一定要是參數傳遞 

    ofstream outfile("買票客戶信息.txt",ios::app);//怎麼可以重復的寫入訂票人的數據 
 if (!outfile) 

   cerr<<"open error!"<<endl; 
   exit(1); 

    cout<<"請輸入訂票信息:"<<endl; 
    cout<<"姓名:";cin>>L1.name; 
    cout<<"身份證:";cin>>L1.identitycard; 
    cout<<"車次:";cin>>L1.number; 
    cout<<"日期:";cin>>L1.date; 
    cout<<"路線:";cin>>L1.road; 
    L2.sale++; 
    L2.save--; 
    L1.sitnumber=L2.sale; 
    cout<<"訂票信息為:"<<endl; 
    cout<<"姓名:"<<L1.name<<endl<<"身份證:"<<L1.identitycard<<endl<<"車次:"<<L1.number<<endl<<"日期:"<<L1.date<<endl<<"座位號:"<<L1.sitnumber<<endl<<"路線:"<<L1.road<<endl; 
    outfile<<L1.name<<" "<<L1.identitycard<<" "<<L1.number<<" "<<L1.date<<" "<<L1.sitnumber<<" "<<L1.road<<endl; 
    outfile.close(); 
    s++; 
    return s; 

 
 
int Return(Booknews L1[],int s) 

    Booknews L; 
    con:cout<<"請輸入退票者姓名,身份證"; 
    cin>>L.name>>L.identitycard; 
    int i; 
    for(i=0;i<s;i++) 
    { 
        if(((strcmp(L.name,L1[i].name))&&(strcmp(L.identitycard,L1[i].identitycard)))) 
        { 
            cout<<"這個人沒有訂票!"<<endl; 
            return 0; 
        } 
        else 
        { 
            int j=i; 
            cout<<"該訂票者的信息為:"; 
            cout<<"姓名:"<<L1[i].name<<endl; 
            cout<<"身份證:"<<L1[i].identitycard<<endl; 
            cout<<"車次:"<<L1[i].number<<endl; 
            cout<<"訂票日期:"<<L1[i].date<<endl; 
            cout<<"座位號:"<<L1[i].sitnumber<<endl; 
            cout<<"路線:"<<L1[i].road<<endl; 
            cout<<"以上信息是否符合?符合請按1,不符合請按2:"; 
            int k; 
            cin>>k; 
            if(k==2) goto con; 
            else 
            /*cout<<"輸入訂票者的車次,座位號和日期:"; 
            cin>>L.number>>L.sitnumber>>L.date; 
            for(i=L.sitnumber-1;i<s;i++) 
            { 
                strcpy(L1[i].name,L1[i+1].name); 
                strcpy(L1[i].identitycard,L1[i+1].identitycard); 
                strcpy(L1[i].number,L1[i+1].number); 
                strcpy(L1[i].date,L1[i+1].date); 
                L1[i].sitnumber=L1[i+1].sitnumber;   
                cout<<"退票成功!"<<endl; 
            }*/ 
            { 
                for(;i<s;i++)//刪除退票者的信息 
                { 
                strcpy(L1[i].name,L1[i+1].name); 
                strcpy(L1[i].identitycard,L1[i+1].identitycard); 
                strcpy(L1[i].number,L1[i+1].number); 
                strcpy(L1[i].date,L1[i+1].date); 
                L1[i].sitnumber=L1[i+1].sitnumber;   
                } 
                cout<<"退票成功!"<<endl; 
                ofstream outfile("買票客戶信息.txt",ios::out); 
                if(!outfile) 
                { 
                    cerr<<"open error!"<<endl; 
                    exit(1); 
                } 
                for(i=0;i<j;i++) 
                { 
                    outfile<<L1[i].name<<" "<<L1[i].identitycard<<" "<<L1[i].number<<" "<<L1[i].date<<" "<<L1[i].sitnumber<<endl; 
                } 
                outfile.close(); 
            } 
            return j; 
        } 
    } 

 
 
int main() 

    ifstream infile("所有車次路線信息.txt",ios::in); 
    if (!infile) 
    { 
       cerr<<"open error!"<<endl; 
       exit(1); 
    } 
    ifstream infile1("買票客戶信息.txt",ios::in);//怎麼可以重復的寫入訂票人的數據 
 if (!infile1) 

   cerr<<"open error!"<<endl; 
   exit(1); 

    int n,i,n1,s,j; 
     infile>>n; 
     infile1>>s; 
    infile1.close(); 
     Route route[Maxsize]; 
     Ticketnews ticketnews[Maxsize]; 
    Booknews booknews[Maxsize]; 
     for(i=0;i<n;i++) 
     { 
         infile>>route[i].number>>route[i].startandreach>>route[i].type>>route[i].longroute>>route[i].time>>route[i].price>>route[i].sitnumber>>route[i].save>>route[i].sale; 
     } 
     cout<<"輸出所有汽車路線為:"<<endl; 
     cout<<"車次"<<setw(7)<<"路線"<<setw(18)<<"車型"<<setw(12)<<"路線長度"<<setw(7)<<"時間"<<setw(14)<<"價格"<<setw(8)<<"座位"<< 
setw(5)<<"剩票"<<setw(5)<<"賣票"<<endl; 
     for(i=0;i<n;i++) 
     { 
 cout<<route[i].number<<setw(20)<<route[i].startandreach<<setw(12)<<route[i].type<<setw(8)<<route[i].longroute<<setw(15)<<route[i].time<<setw(8)<< 
             route[i].price<<setw(4)<<route[i].sitnumber<<setw(4)<<route[i].save<<setw(4)<<route[i].sale<<endl; 
         //cout<<route[i].number<<" "<<route[i].startandreach<<" "<<route[i].type<<" "<<route[i].longroute<<" "<<route[i].time<<" "<< 
            // route[i].price<<" "<<route[i].sitnumber<<" "<<route[i].save<<" "<<route[i].sale<<endl; 
     } 
    Match(route,ticketnews,n); 
    cout<<"主菜單為:"<<endl; 
    con:cout<<"1 為訂票,2 為退票,3 為改變路線,4為查詢訂票情況,5為退出該系統,請根據你所要實現的功能輸入相應的數字:"; 
    cin>>n1; 
    switch(n1) 
    { 
        case 1: 
        { 
            con1:cout<<"請輸入你要訂票的車次:"; 
            cin>>booknews[s].number; 
            bool flag(true);//一定要用bool變量控制 
            for(i=0;i<n;i++) 
            { 
                if(!(strcmp(booknews[s].number,route[i].number))&&(route[i].save!=0)) 
                { 
                    //定義一個變量是否需要訂票 
                    flag = false; 
                    cout<<booknews[s].number<<"車次還有票是否需要訂票,是就輸入yes,否就輸入no:"; 
                    char k1[5]; 
                    con3:cin>>k1; 
                    if(!strcmp(k1,"yes")) 
                    { 
                    s=Book(booknews[s],route[i],s); 
                    } 
                    else 
                        break; 
                    cout<<"是否還要訂票,是就輸入yes,不是就輸入no:"; 
                    goto con3; 
                } 
            } 
            if(flag) 
                { 
                    cout<<"你輸入的車次不存在,請重新輸入!"<<endl; 
                    goto con1; 
                } 
ofstream outfile("買票客戶信息.txt",ios::out||ios::app); 
 if (!outfile) 

   cerr<<"open error!"<<endl; 
   exit(1); 

 outfile<<s<<endl;//一定要加一行endl,在另一行添加才可 
 outfile.close(); 
            cout<<"返回主菜單請按1,否則請按2:"; 
            int kkk; 
            cin>>kkk; 
            if(kkk==1)goto con; 
            else 
            break; 
        } 
        case 2: 
        { 
            if(s==0){cout<<"目前沒有人訂票,不能退票!"<<endl;goto con;} 
            else 
            { 
                j=Return(booknews,s); 
                s--; 
               route[j].sale--; 
                route[j].save++; 
                cout<<"返回主菜單請按1,退出請按2:"; 
                int kk1; 
                cin>>kk1; 
                if(kk1==1)goto con; 
            } 
ofstream outfile("買票客戶信息.txt",ios::out||ios::app);//怎麼可以重復的寫入訂票人的數據 
 if (!outfile) 

   cerr<<"open error!"<<endl; 
   exit(1); 

 outfile<<s<<endl; 
 outfile.close(); 
            break; 
        } 
        case 3://用指針指向文件?? 
        { 
            cout<<"請輸入管理員密碼:"; 
            int code; 
            cin>>code; 
            if(code==123)                       //記住密碼 
            { 
          con4:cout<<"請根據A為增加路線,B為刪除路線,C為修改汽車路線信息輸入相應的字母:"; 
          char z;cin>>z; 
          if(z=='A') 
          { 
              infile.close(); 
              con5:n++;//注意con5要指對地方 
              ofstream outfile("所有車次路線信息.txt",ios::out||ios::app); 
              if (!outfile) 
              { 
                  cerr<<"open error!"<<endl; 
                  exit(1); 
              }      
              outfile<<n;//輸出過後要在把n輸進去???? 
              //infile>>n; 
              if(!outfile.eof())//指向文件末尾 
                  outfile.seekp(0,ios::end); 
                //outfile<<endl; 
            cout<<"請輸入增加路線的所有數據:"<<endl; 
              cout<<"車次:";cin>>route[n-1].number; 
              cout<<"路線:";cin>>route[n-1].startandreach; 
              cout<<"車輛類型:";cin>>route[n-1].type; 
              cout<<"裡程:";cin>>route[n-1].longroute; 
              cout<<"時間:";cin>>route[n-1].time; 
              cout<<"票價:";cin>>route[n-1].price; 
              cout<<"座位數:";cin>>route[n-1].sitnumber; 
              cout<<"剩余票數:";cin>>route[n-1].save; 
              cout<<"賣票數:";cin>>route[n-1].sale; 
              outfile<<route[n-1].number<<" "<<route[n-1].startandreach<<" "<<route[n-1].type<<" "<<route[n-1].longroute<<" " 
                  <<route[n-1].time<<" "<<route[n-1].price<<" "<<route[n-1].sitnumber<<" "<<route[n-1].save<<" "<<route[n-1].sale<<endl; 
              outfile.close(); 
              cout<<"是否還要增加路線,是請輸入yes,退出請輸入no:"; 
              char kk3[5]; 
              cin>>kk3; 
              if(strcmp(kk3,"no")) goto con5; 
          } 
            else 
            { 
            if(z=='B') 
            { 
con6:cout<<"請輸入你要刪除的車次路線:"; 
              char z1[20];int z2=-1; 
              cin>>z1; 
              for(i=0;i<n;i++) 
              { 
                  if(!strcmp(z1,route[i].number)) 
                  { 
                      z2=i; 
                      break; 
                  } 
              } 
              if(z2>0) 
              { 
                for(;z2<n-1;z2++) 
                { 
                  strcpy(route[z2].number,route[z2+1].number); 
                  strcpy(route[z2].startandreach,route[z2+1].startandreach); 
                  strcpy(route[z2].type,route[z2+1].type); 
                  strcpy(route[z2].longroute,route[z2+1].longroute); 
                  strcpy(route[z2].time,route[z2+1].time); 
                  route[z2].price=route[z2+1].price; 
                  route[z2].sitnumber=route[z2+1].sitnumber; 
                  route[z2].save=route[z2+1].save; 
                  route[z2].sale=route[z2+1].sale; 
                } 
                n--; 
            ofstream outfile("所有車次路線信息.txt",ios::out); 
              if (!outfile) 
              { 
                  cerr<<"open error!"<<endl; 
                  exit(1); 
              }  
              outfile<<n<<endl; 
                for(i=0;i<n;i++) 
                { 
                outfile<<route[i].number<<" "<<route[i].startandreach<<" "<<route[i].type<<" "<<route[i].longroute<<" "<<route[i].time<<" "<< 
             route[i].price<<" "<<route[i].sitnumber<<" "<<route[i].save<<" "<<route[i].sale<<endl;    
                } 
                outfile.close(); 
con7:cout<<"是否還要刪除路線,是就輸入yes,否就輸入no:"; 
                char kk4[5]; 
                cin>>kk4; 
                if(!strcmp(kk4,"yes"))goto con6; 
                else 
                    if(!strcmp(kk4,"no"))break; 
                    else 
                        if(strcmp(kk4,"yes")) {cout<<"你輸入的單詞不符合要求,請重新輸入:";goto con7;} 
              } 
          } 
          else 
          { 
          if(z=='C') 
            { 
                con2:cout<<"請輸入你要修改的路線:"; 
                char k[20],k2[20];int kk; 
                cin>>k; 
                for(i=0;i<n;i++) 
                { 
                    if(!strcmp(k,route[i].startandreach)) 
                      { 
                          cout<<"修改路線為:"; 
                          cin>>k2; 
                          strcpy(route[i].startandreach,k2); 
                          /*cout<<"是否還要修改,是就輸入yes,否就輸入no:";當想要把重復的路線修改時,就要用到的程序段 
                          char kk4[5]; 
                          cin>>kk4; 
                          if(strcmp(kk4,"yes")) break;*/ 
                      } 
                     
                    ofstream outfile("所有車次路線信息.txt",ios::out); 
              if (!outfile) 
              { 
                  cerr<<"open error!"<<endl; 
                  exit(1); 
              }  
              outfile<<n<<endl; 
            for(i=0;i<n;i++) 
            { 
            outfile<<route[i].number<<" "<<route[i].startandreach<<" "<<route[i].type<<" "<<route[i].longroute<<" "<<route[i].time<<" "<< 
             route[i].price<<" "<<route[i].sitnumber<<" "<<route[i].save<<" "<<route[i].sale<<endl;    
            } 
            outfile.close(); 
            } 
            break; 
con8:cout<<"是否還需要修改,是就選1,不是就選2:"; 
                cin>>kk; 
                if(kk==1)goto con2; 
                else 
                if(kk==2)break; 
                else {cout<<"你輸入的數字不符合要求,請重新輸入:"; goto con8;} 
            } 
          else 
            { 
              cout<<"你輸入的數字不符合要求,請重新輸入:"; 
              goto con4; 
            } 
          } 
        } 
        } 
            else 
            { 
                cout<<"你所輸入的密碼錯誤,返回主菜單請按1,退出請按2:"; 
                int kk2; 
                cin>>kk2; 
                if(kk2==2)return 0; 
                else 
                    goto con; 
            } 
            break; 
        } 
        case 4: 
            { 
                char k3[10],k4[20]; 
                cout<<"請輸入用戶名和身份證號:"; 
                cin>>k3>>k4; 
                for(i=0;i<s;i++) 
                { 
                    if(!(strcmp(k3,booknews[i].name)&&strcmp(k4,booknews[i].identitycard))) 
                    { 
                        cout<<"你的訂票信息為:"<<endl; 
                        cout<<"姓名:"<<booknews[i].name<<endl; 
                        cout<<"身份證號:"<<booknews[i].identitycard<<endl; 
                        cout<<"車次:"<<booknews[i].number<<endl; 
                        cout<<"座位號:"<<booknews[i].sitnumber<<endl; 
                        cout<<"日期:"<<booknews[i].date<<endl; 
                    } 
                } 
            } 
            return 0; 
        case 5: 
            { 
                return 0; 
            } 
        default: 
        { 
            cout<<"你所選的功能不存在,請重新再選擇!"<<endl; 
            goto con; 
        } 
    } 
     cout<<"輸出所有汽車路線為:"<<endl; 
     for(i=0;i<n;i++) 
     { 
cout<<route[i].number<<setw(20)<<route[i].startandreach<<setw(12)<<route[i].type<<setw(8)<<route[i].longroute<<setw(15)<<route[i].time<<setw(8)<< 
             route[i].price<<setw(4)<<route[i].sitnumber<<setw(4)<<route[i].save<<setw(4)<<route[i].sale<<endl; 
         /*cout<<route[i].number<<" "<<route[i].startandreach<<" "<<route[i].type<<" "<<route[i].longroute<<" "<<route[i].time<<" "<< 
             route[i].price<<" "<<route[i].sitnumber<<" "<<route[i].save<<" "<<route[i].sale<<endl;*/ 
     } 
    //goto con; 
    return 0; 

自己設置的票務信息
quickplane 12:00--19:00 0 100
quickplane 12:00--19:00 0 100
train 12:00--19:00 0 100
quickplane 12:00--19:00 0 100
train 12:00--19:00 0 100
quickplane 12:00--19:00 0 100
car 12:00--19:00 0 100
car 12:00--19:00 0 100
train 12:00--19:00 0 100
quickplane 12:00--19:00 0 100
car 12:00--19:00 0 100
car 12:00--19:00 0 100
car 4:00--8:00 0 35
car 8:00--9:00 0 45
car 9:00--19:00 0 48
car 9:00--19:00 0 45
所有車次的路線信息
16
A000 Beijing--Nanchang quickplane 1000Km 12:00--19:00 456.25 100 100 0
A001 Beijing--Nanchang quickplane 1000Km 12:00--19:00 456.25 100 100 0
A002 Beijing--Nanchang train 1000Km 12:00--19:00 456.25 100 100 0
A003 Beijing--Nanchang quickplane 1000Km 12:00--19:00 456.25 100 100 0
A004 Beijing--Nanchang train 1000Km 12:00--19:00 456.25 100 100 0
A005 Beijing--Nanchang quickplane 1000Km 12:00--19:00 456.25 100 100 0
A006 Beijing--Nanchang car 1000Km 12:00--19:00 456.25 100 100 0
A007 Beijing--Nanchang car 1000Km 12:00--19:00 456.25 100 100 0
A008 Beijing--Nanchang train 1000Km 12:00--19:00 456.25 100 100 0
A009 Beijing--Nanchang quickplane 1000Km 12:00--19:00 456.25 100 100 0
A010 Beijing--Nanchang car 1000Km 12:00--19:00 456.25 100 100 0
A011 Beijing--Nanchang car 1000Km 12:00--19:00 456.25 100 100 0
A012 J--P car 456km 4:00--8:00 45 35 35 0
A013 JJ--PP car 456km 8:00--9:00 45.2 45 45 0
A013 pp--ll car 489km 9:00--19:00 45.32 48 48 0
A014 car car 456km 9:00--19:00 45.2 45 45 0

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved