简易地铁自动机售票系统实现(C++)

该程序具有以下功能

(1) 一个地铁路线类 Router,包含路线编号,途中的各个站点。可以新增、删除、查询路线,可以根据线路名称,显示线路图片。

(2) 一个地图类 Map,可以显示所有可以乘坐的地铁站名,以及线路信息。

(3) 根据用户选择起点和终点和人次信息,可以自动计算应付金额;根据用户输入的金额,计算找零信息;

(4) 使用外部文件存储路线相关信息。

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
 
class Router {
 
    private:
 
        string port[100];//经过站点
        int id;//路线编号
        int count=0; //站点数目
 
    public:
 
        void setId(int i) {
            id=i;
        }
 
        void addPort(string name) {
            port[count]=name;
            count++;
        }
 
        void getPort() {
            int i=0;
            for(i=0; i<count; i++) {
                cout<<"第"<<i+1<<"站:";
                cout<<port[i];
                cout<<endl;
            }
        }
 
        int check(string u,string v) {
            int d=0;
            for(int i=0; i<count; i++) {
                if(port[i]==u) {
                    for(int j=0; j<count; j++) {
                        if(port[j]==v) {
                            // u v
                            return ((i-j)>=0)?
                                   (i-j):(j-i);
                        }
                    }
                }
            }
            return 0;
        }
};
 
 
class Map {
 
    private:
        vector<Router> r;//路线图
 
 
    public:
        double charge=2;//每站价格
        void setCharge(double ch) {
            charge=ch;
        }
 
        void init() {
 
            Router temp1;
            temp1.setId(1);
            temp1.addPort("west");
            temp1.addPort("mid1");
            temp1.addPort("south");
            r.push_back(temp1);
 
            Router temp2;
            temp2.setId(2);
            temp2.addPort("south");
            temp2.addPort("mid2");
            temp2.addPort("east");
            r.push_back(temp2);
 
            Router temp3;
            temp3.setId(3);
            temp3.addPort("east");
            temp3.addPort("mid3");
            temp3.addPort("north");
            r.push_back(temp3);
 
            Router temp4;
            temp4.setId(4);
            temp4.addPort("north");
            temp4.addPort("mid4");
            temp4.addPort("west");
            r.push_back(temp4);
        }
 
        int buy(string start, string end) {
            int count=r.size();
            int d=0;
            for(int i=0; i<count; i++) {
                Router temp=r[i];
                d=temp.check(start,end);
                if(d>0) {
                    cout<<"您需要乘坐"<<i+1<<"号线"<<endl;
                    return d;
                }
            }
            return 0;
        }
 
        void show() {
            int count=r.size();
            cout<<"本市地铁线路图如下:"<<endl;
            for(int i=0; i<count; i++) {
                cout<<i+1<<"号线:"<<endl<<endl;
                Router temp=r[i];
                temp.getPort();
                cout<<endl<<endl;
            }
        }
};
 
 
void menu() {
    int m;
    Map map;
    map.init();
    while(1) {
        cout<<endl<<endl<<endl;
        cout<<"----------欢迎来到地铁售票系统-----------"<<endl;
        cout<<"----------1、路线查询-----------"<<endl;
        cout<<"----------2、购票-----------"<<endl;
        cin>>m;
        if(m==1) {
            map.show();
        } else if(m==2) {
            
            cout<<"请输入起点:"<<endl;
            string s;
            cin>>s;
            cout<<"请输入终点:"<<endl;
            string e;
            cin>>e;
            cout<<"请输入人数:"<<endl;
            int c;
            cin>>c;
            int d=map.buy(s,e);
            if(d>0) {
                double rs=(double)c*(double)d*map.charge;
                cout<<"您需要支付的费用为:";
                cout<<rs<<endl;
                cout<<"请输入您支付的金额:";
                double in=0;
                cin>>in;
                if(in>=rs) {
                    cout<<"购票成功!"<<endl;
                    cout<<"找零:"<<in-rs<<"元"<<endl;
                } else {
                    cout<<"金额不足,购票失败!";
                }
            } else {
                cout<<"抱歉,请选择其他交通!";
            }
        }
    }
}
 
int main() {
    menu();
    return 0;
}
 
相关推荐
新手小新1 小时前
C++游戏开发(2)
开发语言·前端·c++
程序员编程指南3 小时前
Qt 嵌入式界面优化技术
c语言·开发语言·c++·qt
逝雪Yuki4 小时前
数据结构与算法——字典(前缀)树的实现
数据结构·c++·字典树·前缀树·左程云
卷卷的小趴菜学编程4 小时前
Qt-----初识
开发语言·c++·qt·sdk·qt介绍
程序员编程指南5 小时前
Qt 开发 IDE 插件开发指南
c语言·c++·ide·qt·elasticsearch
利刃大大6 小时前
【在线五子棋对战】十一、整合封装服务器模块实现
运维·服务器·c++·项目·五子棋
ok0606 小时前
C++对象访问有访问权限是不是在ide里有效
开发语言·c++·ide
程序员编程指南7 小时前
Qt 开发自动化测试框架搭建
c语言·开发语言·c++·qt
三小尛7 小时前
C++赋值运算符重载
开发语言·c++
小徐不徐说7 小时前
C++ 模板与 STL 基础入门:从泛型编程到实战工具集
开发语言·数据结构·c++·qt·面试