需求分析:需要通过用户在ListBox中选择起点站与终点站,来计算出俩站需要花费的路费。
设计文档:用户可以随意选择ListBox中的元素,可以清楚的看到某站到某站的路费,并在页面下面写上提示,乘坐几站多少钱,那一站是转线站。

程序设计:

#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;
}

代码复审:和室友共同检查代码认为无问题
测试结果:

 

 

报告
测试报告:三条线,设置的站数并不多,但基本运行流程没问题
计算工作量:一百多行的代码,虽然可以复制自己的一站一站的代码。但仍有问题被困,好在问过同学解决,也是花费了一定时间完成。
事后总结:代码完成度不高,有些简陋,期间也遇到很多问题,来求助同学来完成。仍需多加观看以及练习掌握知识!