12306系統簡單功能
阿新 • • 發佈:2018-11-19
12306系統是在課程設計時製作的,耗時三天半,仍有部分小bug。
程式碼貼上
#include<bits/stdc++.h> using namespace std; class Time//時間類 { int year,month,day,hour,minute,second; public: Time() { } Time(int a,int b) { hour=a;minute=b; } void setMohth(int x){month=x;} int getMonth(){return month;} void setDay(int x){day=x;} int getDay(){return day;} void setHour(int x){hour=x;} int getHour(){return hour;} void setMinute(int x){minute=x;} int getMinute(){return minute;} friend ostream&operator<<(ostream&out,const Time&d); friend istream&operator>>(istream&in,Time&d); void display(); bool operator<(const Time &a)const { if(hour<a.hour) return true; else if ((hour==a.hour)&&minute<a.minute) return true; return false; } bool operator>(const Time &a)const { if(hour>a.hour) return true; else if ((hour==a.hour)&&minute>a.minute) return true; return false; } }; ostream&operator<<(ostream&out,const Time&d) { out<<d.hour<<" "<<d.minute<<" "; return out; } istream&operator>>(istream&in,Time&d) { in>>d.hour>>d.minute; } class inform//列車資訊類 { string checi; string qidian; string zhongdian; string name; Time arive; Time leave; int k; int a,b,c,d,ting; public: inform() { } inform(string d,string s,Time t1,Time t2,int e) { checi=d; name=s; arive=t1; leave=t2; ting=e; } Time getarive(){return arive;} Time getleave(){return leave;} void setname(string s){name=s;} string getname(){return name;} void setcheci(string s){checi=s;} string getcheci(){return checi;} void setting(int x){ting=x;} int getting(){return ting;} /* void operator=(inform&s) { checi=s.getcheci(); name=s.getname(); arive=s.getarive(); leave=s.getleave(); ting=s.getting(); }*/ friend ostream&operator<<(ostream&out,const inform&s); friend istream&operator>>(istream&in,inform&s); }; ostream&operator<<(ostream&out,const inform&s) { out<<s.checi<<" "<<s.name<<" "<<s.arive<<" "<<s.leave<<" "<<s.ting<<endl; } istream&operator>>(istream&in,inform&s) { in>>s.checi; if (s.checi=="-1")return in; in>>s.name>>s.arive>>s.leave>>s.ting; } class station//站點類 { string cname; string checi; string name; Time t1; Time t2; int ting; //inform a; vector<inform>v1; vector<inform>::iterator it1; multimap<Time,int> m1; multimap<Time,int>::iterator mit1; public: station(){} station(string a){name=a;} void addinform(inform a){v1.push_back(a);int i=v1.size()-1;m1.insert(make_pair(a.getarive(),i));} void changeinform(int a,inform r) { Time t=v1[a].getarive(); mit1=m1.find(t); m1.erase(mit1); m1.insert(make_pair(r.getarive(),a)); v1[a]=r; } string getname(){return name;} string getcheci(int a){return v1[a].getcheci();} Time getarive(int a){return v1[a].getarive();} Time getleave(int a){return v1[a].getleave();} int getv1size(){return v1.size();} inform getinform(int a){return v1[a];} void chang(int a,inform s){v1[a]=s;} void shanchumation(string a) { Time t; int b; it1=v1.begin(); for(int i=0;i<v1.size();i++,it1++) if(v1[i].getcheci()==a) { v1.erase(it1); t=v1[i].getarive(); mit1=m1.find(t); m1.erase(mit1); } } void display() { int a; for(mit1=m1.begin();mit1!=m1.end();mit1++) { a=mit1->second; cout<<v1[a]; } } friend ostream&operator<<(ostream&ou,const station&s) { ou<<s.name<<endl; return ou; } friend istream&operator>>(istream&in,station&s) { in>>s.name; /*while(in>>s.checi&&s.checi!="-1") { in>>s.name>>s.t1>>s.t2>>s.ting; inform r(s.checi,s.name,s.t1,s.t2,s.ting); s.v1.push_back(r); }*/ return in; } }; class train//車次類 { string checi; string shifa; string zhongdian; string name; Time arive; Time leave; int no; Time t1; Time t2; vector<inform>v2; vector<inform>::iterator it2; multimap<Time,int> m2; multimap<Time,int>::iterator mit2; int ting; public: train(){} train(string che){checi=che;} void addinform(inform a){v2.push_back(a);int i=v2.size()-1;m2.insert(make_pair(a.getarive(),i));} string getname(){return name;} int getv2size(){return v2.size();} inform getinform(int a){return v2[a];} string getname(int a){return v2[a].getname();} Time getarive(int a){return v2[a].getarive();} string getcheci(){return checi;} void changeinform(int a,inform r) { Time t=v2[a].getarive(); mit2=m2.find(t); m2.erase(mit2); m2.insert(make_pair(r.getarive(),a)); v2[a]=r; } void shanchumation(string a) { Time t; int b; it2=v2.begin(); if(shifa==a) { mit2=m2.begin(); mit2++; b=mit2->second; shifa=v2[b].getname(); } if(zhongdian==a) { mit2=m2.end(); mit2--; mit2--; b=mit2->second; cout<<b<<endl; zhongdian=v2[b].getname();} for(int i=0;i<v2.size();i++,it2++) { if(v2[i].getname()==a) { v2.erase(it2); t=v2[i].getarive(); mit2=m2.find(t); m2.erase(mit2); } } } void display() { int a; for(mit2=m2.begin();mit2!=m2.end();mit2++) { a=mit2->second; cout<<v2[a]; } } friend ostream&operator<<(ostream&out,const train&s) { out<<s.checi<<" "<<s.shifa<<" "<<s.zhongdian<<endl; } friend istream&operator>>(istream&in,train&s) { in>>s.checi; //if (s.checi=="-1")return in; in>>s.shifa>>s.zhongdian; } }; class admop//管理員類 { vector<station>v3; vector<station>::iterator it3; multimap<string,int>m1; multimap<string,int>::iterator mit1; vector<train>v4; vector<train>::iterator it4; multimap<string,int>m2; multimap<string,int>::iterator mit2; public: admop() { load1(); load2(); } ~admop() { save1(); save2(); } void load1(); void load2(); void save1(); void save2(); void querrysta(); void querrytra(); int serchsta(string a); int serchtra(string a); void llstation(); void lltrain(); void addstations(); void addstation(); void addtrain(); void changetime(); void shanchumation(); void dispstation(); void disptrain(); }; void admop::llstation() { for (int i=0;i<v3.size();i++) {cout<<v3[i];v3[i].display();} } void admop::lltrain() { for (int i=0;i<v4.size();i++) {cout<<v4[i];v4[i].display();} } void admop::load1() { station a; inform c; int o; ifstream infile("d:\\station.txt",ios::in); if(!infile) return; v3.clear(); while(infile>>a) { v3.push_back(a); o=v3.size()-1; m1.insert(make_pair(a.getname(),o)); while(infile>>c) if(c.getcheci()!="-1") v3[o].addinform(c); else break; } infile.close(); } void admop::load2() { train a; inform c; int o; ifstream infile("d:\\train.txt",ios::in); if(!infile) return; v4.clear(); while(infile>>a) { v4.push_back(a); o=v4.size()-1; m2.insert(make_pair(a.getcheci(),o)); while(infile>>c) if(c.getcheci()!="-1") v4[o].addinform(c); else break; } infile.close(); } void admop::save1() { ofstream outfile("d:\\station.txt",ios::out); if(!outfile) return; for(int i=0;i<v3.size();++i) { outfile<<v3[i]; for(int j=0;j<v3[i].getv1size();++j) outfile<<v3[i].getinform(j); outfile<<"-1"<<endl; } outfile.close(); } void admop::save2() { ofstream outfile("d:\\train.txt",ios::out); if(!outfile) return; for(int i=0;i<v4.size();++i) { outfile<<v4[i]; for(int j=0;j<v4[i].getv2size();++j) outfile<<v4[i].getinform(j); outfile<<"-1"<<endl; } outfile.close(); } int admop::serchsta(string a) { mit1=m1.find(a); if (mit1!=m1.end()) return mit1->second; else return -1; } int admop::serchtra(string a) { mit2=m2.find(a); if (mit2!=m2.end()) return mit2->second; else return -1; } void admop::addstation() { inform r; station s; cout<<"請輸入站點名:"; cin>>s; int b=serchsta(s.getname()); if(b!=-1) { cout<<"請輸入車次,站點,到達時間,開車時間,停留時間:"; v3.push_back(s); int i=v3.size()-1; while(cin>>r&&r.getcheci()!="-1") { v3[i].addinform(r); } } } void admop::addtrain() { inform r; train s; cout<<"請輸入車次名,始發站,終點站:"; cin>>s; int b=serchtra(s.getcheci()); if(b==-1) { cout<<"請輸入車次,站點,到達時間,開車時間,停留時間:"; v4.push_back(s); int i=v4.size()-1; while(cin>>r&&r.getcheci()!="-1") { v4[i].addinform(r); int k=serchsta(r.getname()); if(k==-1) { v3.push_back(r.getname()); int t=v3.size(); v3[t-1].addinform(r); } else { v3[k].addinform(r); } } } else cout<<"已存在該車次!"; } void admop::querrysta() { cout<<"請輸入要查詢站點:"; station s; cin>>s; int b=serchsta(s.getname()); if (b!=-1) { cout<<s; v3[b].display(); } else cout<<"輸入有誤"; } void admop::querrytra() { cout<<"請輸入要查詢車次:"; string s; cin>>s; int b=serchtra(s); if (b!=-1) { cout<<v4[b]; v4[b].display(); } } void admop::shanchumation() { string a; station b; int c,d; while(1) { cout<<"請輸入要刪除的車次和站點"<<endl; cin>>a; if(a!="-1") { cin>>b; if (b.getname()!="-1") { c=serchtra(a); d=serchsta(b.getname()); if(c!=-1&&d!=-1) { v3[c].shanchumation(a); v4[d].shanchumation(b.getname()); } } } else break; } } void admop::changetime() { cout<<"請輸入要修改時間的車次名:"; string s; cin>>s; int a=serchtra(s); if (a!=-1) { cout<<"請輸入要修改時間的站點名:"; station p; cin>>p; int b=serchsta(p.getname()); if (b!=-1) { cout<<"請輸入要更改的時間以及停留時間;"; Time t1;Time t2;int ting; cin>>t1>>t2;cin>>ting; inform k(s,p.getname(),t1,t2,ting); v3[b].changeinform(a,k); v4[a].changeinform(b,k); } } } class clientop//使用者操作類 { vector<station>v5; vector<station>::iterator it5; multimap<string,int>m3; multimap<string,int>::iterator mit3; vector<train>v6; vector<train>::iterator it6; multimap<string,int>m4; multimap<string,int>::iterator mit4; public: clientop() { load3(); load4(); } ~clientop() { save3(); save4(); } void load3(); void load4(); void save3(); void save4(); void queryche_notime(); void queryche_byarive(); void queryche_byleave(); void querrysta(); void querrytra(); void huancheng(); int serchsta(string a); int serchtra(string a); }; void clientop::load3() { station a; inform c; int o; ifstream infile("d:\\station.txt",ios::in); if(!infile) return; //infile>>num; //for (int i=1;i<=num;i++) v5.clear(); while(infile>>a) {v5.push_back(a); o=v5.size()-1; m3.insert(make_pair(a.getname(),o)); while(infile>>c) if(c.getcheci()!="-1") v5[o].addinform(c); else break;} infile.close(); } void clientop::load4() { train a; inform c; int o; ifstream infile("d:\\train.txt",ios::in); if(!infile) return; //infile>>num; //for (int i=1;i<=num;i++) v6.clear(); while(infile>>a) {v6.push_back(a); o=v6.size()-1; m4.insert(make_pair(a.getcheci(),o)); while(infile>>c) if(c.getcheci()!="-1") v6[o].addinform(c); else break;} infile.close(); } void clientop::save3() { ofstream outfile("d:\\station.txt",ios::out); if(!outfile) return; for(int i=0;i<v5.size();++i) { outfile<<v5[i]; for(int j=0;j<v5[i].getv1size();++j) outfile<<v5[i].getinform(j); outfile<<"-1"<<endl; } outfile.close(); } void clientop::save4() { ofstream outfile("d:\\train.txt",ios::out); if(!outfile) return; for(int i=0;i<v6.size();++i) { outfile<<v6[i]; for(int j=0;j<v6[i].getv2size();++j) outfile<<v6[i].getinform(j); outfile<<"-1"<<endl; } outfile.close(); } int clientop::serchsta(string a) { mit3=m3.find(a); if (mit3!=m3.end()) return mit3->second; else return -1; } int clientop::serchtra(string a) { mit4=m4.find(a); if (mit4!=m4.end()) return mit4->second; else return -1; } void clientop::queryche_byarive() { cout<<"請輸入起始點終點,以及出發時間:"; station s1,s2; Time t; cin>>s1; cin>>s2; cin>>t; int a=serchsta(s1.getname()); int b=serchsta(s2.getname()); if (a!=-1&&b!=-1) for (int i=0;i<v5[a].getv1size();i++) { int p=0; for (int j=0;j<v5[b].getv1size();j++) { if (v5[a].getcheci(i)==v5[b].getcheci(j)) { //if(v5[a].getleave(i)<v5[b].getarive(j)) if(v5[a].getarive(i)<t) p=1; break; } } if(p==1) { string c; c=v5[a].getcheci(i); int e=serchtra(c); v6[e].display(); } } } void clientop::huancheng() { string s1,s2; cout<<"請輸入起始站,終點站"<<endl; cin>>s1>>s2; int a=serchsta(s1); int b=serchsta(s2); if (a!=-1&&b!=-1) { for (int i=0;i<v5[a].getv1size();i++) { for (int j=0;j<v5[b].getv1size();j++) { if (v5[a].getcheci(i)==v5[b].getcheci(j)&&v5[a].getarive(i)<v5[b].getarive(j)) cout<<"有車次可直達目的地。"; else { int c=serchtra(v5[a].getcheci(i)); int d=serchtra(v5[b].getcheci(j)); if (c!=-1&&d!=-1) { for (int k=0;k<v6[c].getv2size();k++) { int y=0; for (int l=0;l<v6[d].getv2size();l++) { if (v6[c].getname(k)==v6[d].getname(l)) if (v6[c].getarive(k)<v6[d].getarive(l)) { cout<<v6[c]; v6[c].display(); cout<<v6[d]; v6[d].display(); break; } } } } } } } } } void clientop::queryche_byleave() { cout<<"請輸入起始點終點,以及到達時間:"; station s1,s2; Time t; cin>>s1; cin>>s2; cin>>t; int a=serchsta(s1.getname()); int b=serchsta(s2.getname()); if (a!=-1&&b!=-1) for (int i=0;i<v5[a].getv1size();i++) { int p=0; for (int j=0;j<v5[b].getv1size();j++) { if (v5[a].getcheci(i)==v5[b].getcheci(j)) { // if(v5[a].getleave(i)<v5[b].getarive(j)) if(v5[b].getleave(j)<t) p=1; break; } } if(p==1) { string c; c=v5[a].getcheci(i); int e=serchtra(c); v6[e].display(); } } } void clientop::queryche_notime() { cout<<"請輸入起始點終點"; station s1,s2; cin>>s1; cin>>s2; int a=serchsta(s1.getname()); int b=serchsta(s2.getname()); if (a!=-1&&b!=-1) for (int i=0;i<v5[a].getv1size();i++) { int p=0; for (int j=0;j<v5[b].getv1size();j++) { if (v5[a].getcheci(i)==v5[b].getcheci(j)) { //if(v5[a].getleave(i)<v5[b].getleave(j)) p=1; break; } } if(p==1) { string c; c=v5[a].getcheci(i); int e=serchtra(c); v6[e].display(); } } } void clientop::querrysta() { cout<<"請輸入要查詢站點:"; station s; cin>>s; int b=serchsta(s.getname()); if (b!=-1) { cout<<s; v5[b].display(); } else cout<<"輸入有誤"; } void clientop::querrytra() { cout<<"請輸入要查詢車次:"; string s; cin>>s; int b=serchtra(s); if (b!=-1) { cout<<v6[b]; v6[b].display(); } } int main() { admop r1; r1.llstation(); r1.lltrain(); admop r2; r2.addtrain(); r2.querrytra(); admop r3; r3.querrysta(); r3.changetime(); clientop t1; t1.queryche_notime(); clientop t2; t2.queryche_byarive(); clientop t3; t3.queryche_byleave(); } 資料檔案station 北京南 G101 北京南 2 5 2 7 2 G5 北京南 0 0 7 0 0 G105 北京南 0 0 7 20 0 G143 北京南 0 0 7 50 0 G107 北京南 0 0 8 5 0 -1 滄州西 G101 滄州西 7 35 7 38 3 G41 滄州西 10 7 10 9 2 -1 德州東 G101 德州東 8 5 8 13 8 G105 德州東 8 40 8 43 3 G107 德州東 9 20 9 22 2 G111 德州東 9 48 9 50 2 G113 德州東 10 17 10 19 2 G115 德州東 10 41 10 43 2 -1 濟南西 G101 濟南西 8 37 8 41 4 G5 濟南西 8 30 8 32 2 G105 濟南西 9 7 9 11 4 G143 濟南西 9 37 9 39 2 G107 濟南西 9 46 9 48 2 G111 濟南西 10 14 10 17 3 -1 曲阜東 G101 曲阜東 9 13 9 15 2 -1 宿州東 G101 宿州東 10 12 10 14 2 G105 宿州東 10 49 10 51 2 G115 宿州東 12 26 12 38 12 -1 南京南 G101 南京南 11 14 11 16 2 G5 南京南 10 30 10 32 2 G105 南京南 11 51 11 53 2 G143 南京南 11 55 11 57 2 G107 南京南 12 15 12 18 3 G113 南京南 13 7 13 11 4 G1 南京南 12 24 12 26 2 G41 南京南 13 29 13 31 2 G115 南京南 13 38 13 42 4 -1 鎮江南 G101 鎮江南 11 35 11 37 2 G107 鎮江南 12 37 12 43 6 G41 鎮江南 13 51 13 56 5 G115 鎮江南 14 1 14 3 2 -1 徐州東 G105 徐州東 10 28 10 30 2 G107 徐州東 10 58 11 0 2 -1 棗莊 G101 棗莊 9 38 9 40 2 G107 棗莊 10 38 10 40 2 G111 棗莊 11 18 11 20 2 -1 泰安 G111 泰安 10 35 10 40 5 G41 泰安 11 15 11 17 2 -1 上海虹橋 G101 上海虹橋 12 40 12 40 0 G105 上海虹橋 13 8 13 8 0 G143 上海虹橋 13 12 13 12 0 G107 上海虹橋 13 47 13 47 0 G111 上海虹橋 14 22 14 22 0 G113 上海虹橋 14 33 14 33 0 G1 上海虹橋 13 28 13 28 0 G41 上海虹橋 14 49 14 51 2 G115 上海虹橋 15 10 15 10 0 -1 資料檔案train G101 北京 上海虹橋 G101 北京南 2 5 2 7 2 G101 滄州西 7 35 7 38 3 G101 德州東 8 5 8 13 8 G101 濟南西 8 37 8 41 4 G101 曲阜東 9 13 9 15 2 G101 棗莊 9 38 9 40 2 G101 宿州東 10 12 10 14 2 G101 南京南 11 14 11 16 2 G101 鎮江南 11 35 11 37 2 G101 蘇州北 12 12 12 15 3 G101 上海虹橋 12 40 12 40 0 -1 G5 北京 上海 G5 北京南 0 0 7 0 0 G5 天津南 7 31 7 33 2 G5 濟南西 8 30 8 32 2 G5 南京南 10 30 10 32 2 G5 上海 11 40 11 40 0 -1 G105 北京 上海虹橋 G105 北京南 0 0 7 20 0 G105 天津南 7 54 7 56 2 G105 德州東 8 40 8 43 3 G105 濟南西 9 7 9 11 4 G105 滕州東 9 54 10 3 9 G105 徐州東 10 28 10 30 2 G105 宿州東 10 49 10 51 2 G105 南京南 11 51 11 53 2 G105 無錫東 12 37 12 39 2 G105 上海虹橋 13 8 13 8 0 -1 G143 北京 上海虹橋 G143 北京南 0 0 7 50 0 G143 天津南 8 24 8 31 7 G143 濟南西 9 37 9 39 2 G143 南京南 11 55 11 57 2 G143 常州北 12 29 12 31 2 G143 上海虹橋 13 12 13 12 0 -1 G107 北京 上海虹橋 G107 北京南 0 0 8 5 0 G107 德州東 9 20 9 22 2 G107 濟南西 9 46 9 48 2 G107 棗莊 10 38 10 40 2 G107 徐州東 10 58 11 0 2 G107 南京南 12 15 12 18 3 G107 鎮江南 12 37 12 43 6 G107 蘇州北 13 17 13 20 3 G107 上海虹橋 13 47 13 47 0 -1 G111 北京 上海虹橋 G111 北京南 0 0 8 35 0 G111 德州東 9 48 9 50 2 G111 濟南西 10 14 10 17 3 G111 泰安 10 35 10 40 5 G111 棗莊 11 18 11 20 2 G111 定遠 12 21 12 23 2 G111 丹陽北 13 22 13 24 2 G111 無錫東 13 47 13 49 2 G111 上海虹橋 14 22 14 22 0 -1 G113 北京 上海虹橋 G113 北京南 0 0 8 50 0 G113 廊坊 9 11 9 19 8 G113 德州東 10 17 10 19 2 -1 G789 北京 上海 G789 北京 2 23 2 24 1 -1 G456 墨西哥 西班牙 G456 巴西 2 23 2 25 2 -1 G678 北京 上海 G678 日本 2 23 2 25 2 -1 G345 北京 上海 G345 日本 2 23 2 25 2 -1