C++實(shí)現(xiàn)地鐵自動(dòng)售票系統(tǒng)程序設(shè)計(jì)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)地鐵自動(dòng)售票系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
題目描述:
地鐵是當(dāng)今城市較為流行的一種鐵路運(yùn)輸?shù)男问剑罔F能避免城市地面擁擠,充分利用空間,具有運(yùn)量大、準(zhǔn)時(shí)、正點(diǎn)率較其他公交高、速度快等優(yōu)點(diǎn)。請?jiān)O(shè)計(jì)一個(gè)簡易的、帶菜單的地鐵自動(dòng)售票機(jī)系統(tǒng)。
功能需求:
(1)設(shè)計(jì)一個(gè)地鐵路線類Router,包含路線編號,途中的各個(gè)站點(diǎn)。
(2)設(shè)計(jì)一個(gè)地圖類Map,可以顯示所有可以乘坐的地鐵站名,以及線路信息。
(3)根據(jù)用戶輸入的起點(diǎn)和終點(diǎn)和人次信息,可以自動(dòng)計(jì)算應(yīng)付金額;根據(jù)用戶輸入的金額,計(jì)算找零信息。
程序設(shè)計(jì):
#include <iostream>
#include <vector>
using namespace std;
?
class Router {
?
?? ?private:
?
?? ??? ?string port[100];//經(jīng)過站點(diǎn)
?? ??? ?int id;//路線編號
?? ??? ?int count=0; //站點(diǎn)數(shù)目
?
?? ?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;//每站價(jià)格
?? ??? ?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<<"----------歡迎來到地鐵售票系統(tǒng)-----------"<<endl;
?? ??? ?cout<<"----------1、路線查詢-----------"<<endl;
?? ??? ?cout<<"----------2、購票-----------"<<endl;
?? ??? ?cin>>m;
?? ??? ?if(m==1) {
?? ??? ??? ?map.show();
?? ??? ?} else if(m==2) {
?? ??? ??? ?
?? ??? ??? ?cout<<"請輸入起點(diǎn):"<<endl;
?? ??? ??? ?string s;
?? ??? ??? ?cin>>s;
?? ??? ??? ?cout<<"請輸入終點(diǎn):"<<endl;
?? ??? ??? ?string e;
?? ??? ??? ?cin>>e;
?? ??? ??? ?cout<<"請輸入人數(shù):"<<endl;
?? ??? ??? ?int c;
?? ??? ??? ?cin>>c;
?? ??? ??? ?int d=map.buy(s,e);
?? ??? ??? ?if(d>0) {
?? ??? ??? ??? ?double rs=(double)c*(double)d*map.charge;
?? ??? ??? ??? ?cout<<"您需要支付的費(fèi)用為:";
?? ??? ??? ??? ?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;
}程序運(yùn)行演示:


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言中find_package()的搜索路徑的實(shí)現(xiàn)
本文主要介紹了C語言中find_package()的搜索路徑的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
C和C++如何實(shí)現(xiàn)互相調(diào)用詳解
在學(xué)習(xí)c++中用到一些古老的c語言庫時(shí),在工作中我們經(jīng)常要使用C和C++混合編程,下面這篇文章主要給大家介紹了關(guān)于C和C++如何實(shí)現(xiàn)互相調(diào)用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
如何用C寫一個(gè)web服務(wù)器之CGI協(xié)議
本文主要介紹了如何用C寫一個(gè)web服務(wù)器之CGI協(xié)議,對C語言和web感興趣的同學(xué),可以詳細(xì)看下,并且試驗(yàn)一下。2021-05-05
C++/Php/Python 語言執(zhí)行shell命令的方法(推薦)
下面小編就為大家?guī)硪黄狢++/Php/Python 語言執(zhí)行shell命令的方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
C++中實(shí)現(xiàn)fibonacci數(shù)列的幾種方法
本文主要介紹了C++中實(shí)現(xiàn)fibonacci數(shù)列的幾種方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
詳解C語言 三大循環(huán) 四大跳轉(zhuǎn) 和判斷語句
這篇文章主要介紹了詳解C語言 三大循環(huán) 四大跳轉(zhuǎn) 和判斷語句的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

