C++實現(xiàn)地鐵自動售票系統(tǒng)程序設(shè)計
本文實例為大家分享了C++實現(xiàn)地鐵自動售票系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
題目描述:
地鐵是當(dāng)今城市較為流行的一種鐵路運(yùn)輸?shù)男问?,地鐵能避免城市地面擁擠,充分利用空間,具有運(yùn)量大、準(zhǔn)時、正點(diǎn)率較其他公交高、速度快等優(yōu)點(diǎn)。請設(shè)計一個簡易的、帶菜單的地鐵自動售票機(jī)系統(tǒng)。
功能需求:
(1)設(shè)計一個地鐵路線類Router,包含路線編號,途中的各個站點(diǎn)。
(2)設(shè)計一個地圖類Map,可以顯示所有可以乘坐的地鐵站名,以及線路信息。
(3)根據(jù)用戶輸入的起點(diǎn)和終點(diǎn)和人次信息,可以自動計算應(yīng)付金額;根據(jù)用戶輸入的金額,計算找零信息。
程序設(shè)計:
#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;//每站價格 ?? ??? ?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++利用windows函數(shù)實現(xiàn)計時示例
這篇文章主要介紹了c++利用windows函數(shù)實現(xiàn)計時示例,需要的朋友可以參考下2014-05-05C++ 17轉(zhuǎn)發(fā)一個函數(shù)調(diào)用的完美實現(xiàn)
這篇文章主要給大家介紹了關(guān)于C++ 17如何轉(zhuǎn)發(fā)一個函數(shù)調(diào)用的完美實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C++17具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08如何用c++表驅(qū)動替換if/else和switch/case語句
本文將介紹使用表驅(qū)動法,替換復(fù)雜的if/else和switch/case語句,想了解詳細(xì)內(nèi)容,請看下文2021-08-08