c++面向对象练习:西安市地铁售票系统

自定义三个类

地铁线路类:保存本条线路的所有站点信息和换乘车站信息。
地图类:保存所有地铁线路,根据用户的起始站和终点站可以计算本次车费。
数据类:地铁线路的数据来源。

每个类的详细信息

地铁线路类

#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 << "<--
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值