/* (程式頭部注釋開始)
* 程式的著作權和版本聲明部分
* Copyright (c) 2011, 煙台大學電腦學院學生
* All rights reserved.
* 檔案名稱:
* 作 者: 鮑增凱
* 完成日期: 2012 年 5月 21 日
* 版 本 號:
* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述:
* 程式輸出:
* 程式頭部的注釋結束
*/
#include <iostream>#include<conio.h>#include <windows.h>using namespace std;enum vehicleStaus {rest, running}; //車輛狀態:泊車、行進class vehicle //車輛類{protected:int maxSpeed;//最大車速int currentSpeed;//當前速度int weight;//車重vehicleStaus status; //rest-泊車狀態;running-行進狀態public:vehicle(int maxS, int w); //建構函式,初始時,當前速度總為且處在停車狀態void start(); //由rest狀態到running, 初速為void stop(); //由running狀態到rest, 當前速度小於時,才允許停車void speed_up(); //加速,調用次,速度加void slow_down(); //減速,調用次,速度減,速度為時,停車};//建構函式,初始時,當前速度總為且處在停車狀態vehicle::vehicle(int maxS, int w):maxSpeed(maxS), currentSpeed(0),weight(w), status(rest){}//啟動:由rest狀態到running, 初速為void vehicle::start(){if (status==rest){status=running;currentSpeed=1;}elsecout<<"車輛已經行駛!"<<endl;}//由running狀態到rest, 當前速度小於時,才允許停車void vehicle::stop(){if (status==running)if(currentSpeed<5){status=rest;currentSpeed=0;}elsecout<<"車速太快!先減速再停車……"<<endl;elsecout<<"車輛未啟動!"<<endl;}//加速,調用次,速度加void vehicle::speed_up(){if (status==running)if(currentSpeed<maxSpeed)++currentSpeed;elsecout<<"請不要超速行駛……"<<endl;elsecout<<"車輛未啟動!"<<endl;}//減速,調用次,速度減,速度為時,停車void vehicle::slow_down(){if (status==running){if(currentSpeed>0)--currentSpeed;}elsecout<<"車輛未啟動!"<<endl;if(currentSpeed==0)status=rest;}class bicycle :virtual public vehicle //()單車類{ protected:double height; //車高public:bicycle(int maxS=10, int w=50, int h=0.7); //定義建構函式};bicycle::bicycle(int maxS, int w, int h):vehicle(maxS, w),height(h){} class motorcar : virtual public vehicle//()機動車類{ protected:int seatNum; //座位元int passengerNum; //乘客人數public:motorcar(int maxS=150, int w=1500, int s=5, int p=1); //定義建構函式void addPassenger(int p=1); //搭載乘客,超員要拒載,有人下車時,p為負數。當然車上乘客至少有個(司機)。上下車時要保證安全……};//定義建構函式motorcar::motorcar(int maxS, int w, int s, int p): vehicle(maxS, w),seatNum(s),passengerNum(p){} //搭載乘客,超員要拒載,有人下車時,p為負數。當然車上乘客至少有個(司機)。上下車時要保證安全……void motorcar::addPassenger(int p){if (status==running){cout<<"車輛正在行駛,停車後再上下車!"<<endl;}else{passengerNum+=p;if(passengerNum>seatNum){passengerNum=seatNum;cout<<"涉嫌超員,已清理後達到滿員!"<<endl;}else if (passengerNum<1){passengerNum=1;cout<<"請司機不要離開崗位!"<<endl;}}}class motorcycle: public bicycle, public motorcar //()>機車類{ public:motorcycle(int maxS=90, int w=100, int s=3, int p=1, int h=0.7); //定義建構函式void show(); //顯示>機車的運行狀態};//定義建構函式motorcycle::motorcycle(int maxS, int w, int s, int p, int h):vehicle(maxS, w),bicycle(maxS, w, h),motorcar(maxS, w, s, p){}//顯示>機車的運行狀態void motorcycle::show(){cout<<"狀態:";if(status==rest)cout<<"泊車;\t";elsecout<<"行進;\t";cout<<"車速:"<<currentSpeed<<" / "<< maxSpeed <<"\t當前乘員:"<<passengerNum<<" / "<< seatNum << endl;}int main( ){motorcycle m;bool end=false;while (!end){cout<<"請操作:-啟動 2-加速 3-減速 4-有人上車 5-有人下車 6-停車0-結束"<<endl;char keydown= _getch(); //_getch()返回鍵盤上讀取的字元,應包含標頭檔<conio.h>switch(keydown){case '1': cout<<"操作(啟動)\t"; m.start(); break;case '2': cout<<"操作(加速)\t"; m.speed_up(); break;case '3': cout<<"操作(減速)\t"; m.slow_down(); break;case '4': cout<<"操作(有人上車)\t"; m.addPassenger(); break;case '5': cout<<"操作(有人下車)\t"; m.addPassenger(-1); break;case '6': cout<<"操作(停車)\t"; m.stop(); break;case '0': end=true; break;}m.show();cout<<endl;Sleep(200); //要包含標頭檔<windows.h>}system("pause");return 0;}