C++面向对象程序设计:地铁自动售票系统

题目描述:

地铁是当今城市较为流行的一种铁路运输的形式,地铁能避免城市地面拥挤,充分利用空间,具有运量大、准时、正点率较其他公交高、速度快等优点。请设计一个简易的、带菜单的地铁自动售票机系统。


功能需求:

(1)设计一个地铁路线类Router,包含路线编号,途中的各个站点。

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

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

 

程序设计:

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

程序运行演示:

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乔卿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值