自定义三个类
地铁线路类:保存本条线路的所有站点信息和换乘车站信息。
地图类:保存所有地铁线路,根据用户的起始站和终点站可以计算本次车费。
数据类:地铁线路的数据来源。
每个类的详细信息
地铁线路类
#ifndef MYROUTER_H
#define MYROUTER_H
#include <vector>
#include <map>
#include <iostream>
#include "dataconfig.h"
using namespace std;
class MyRouter
{
public:
MyRouter(string num = "1");
//输入乘车站点,遍历vector,返回值是一个vector第几个站点
int getStationNo(const string & station);
string getRouterNumber();
void printThisTrip();
//计算乘车站点和下车站点之间总共几站路。
int getDistance( const string & startStation, const string & endStation );
friend ostream & operator << ( ostream & out, const MyRouter & router );
private:
void setStartStation(const string & station);
void setEndStation(const string & station);
string mStrName; //几号线
string startStation;
int startNum;
string endStation;
int endNum;
vector<string> stations;
map<string, string> changeStations;
};
#endif // MYROUTER_H
#include "myrouter.h"
#include <stdio.h>
MyRouter::MyRouter(string num)
:mStrName(num)//几号线
{
DataConfig dataConfig;
//判断一下map中是否有key为num的键值对,如果有,返回1,没有返回0
if (dataConfig.mLineStations.count(num))
{
stations = dataConfig.mLineStations.at(num);
changeStations = dataConfig.mLineChangeStations.at(num);
startStation = stations[0];
endStation = stations[stations.size() - 1];
}
else
cout << "第 " << num <<" 条地铁线路还没有建好! 请耐心等待哦~~~" << endl;
}
string MyRouter::getRouterNumber()
{
return this->mStrName;
}
void MyRouter::setStartStation(const string & station)
{
if ( 0 < getStationNo(station) )
{
this->startStation = station;
}
}
void MyRouter::setEndStation(const string & station)
{
if ( 0 < getStationNo(station) )
{
this->endStation = station;
}
else
{
//最短路径
}
}
//输入乘车站点,遍历vector,返回值是一个vector第几个站点
int MyRouter::getStationNo(const string & station)
{
for ( unsigned int i = 0; i < stations.size(); i++ )
{
if ( stations[i] == station )
{
return i;
}
}
return -1;
}
void MyRouter::printThisTrip()
{
int i = 0;
if (startNum > endNum)
{
for ( i = startNum; i > endNum ; i--)
{
cout << stations[i] << " ---> " ;
}
}
else
{
for ( i = startNum; i < endNum ; i++)
{
cout << stations[i] << " ---> " ;
}
}
cout << stations[i] << endl;
}
//计算乘车站点和下车站点之间总共几站路。
int MyRouter::getDistance( const string & startStation, const string & endStation )
{
startNum = getStationNo(startStation);
if ( startNum < 0 )//输入了错误的站占名称
{
return -1;
}
endNum = getStationNo(endStation);
if ( endNum < 0 ) //终点站不在当前线路上
{
return stations.size() - startNum;
}
this->startStation = startStation;
this->endStation = endStation;
return (endNum - startNum) > 0 ? endNum - startNum : startNum - endNum;
}
ostream & operator << ( ostream & out, const MyRouter & router )
{
out << endl << endl << "~~~~~~~~~~您现在查看的是西安地铁【 " << router.mStrName << " 】号线线路图~~~~~~~~";
out << router.startStation << "<--->" << router.endStation << "~~~~~~~~~" <<endl<<endl<<endl;
vector<string>::iterator item;
vector<string> tmp = router.stations;
for (item = tmp.begin(); item != tmp.end(); item++)
{
out << *item << "--->";
}
out << "(终点站)"<< endl << endl <<endl;
map<string, string>::iterator mapItem;
ma