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