第十二週任務3

/* (程序頭部註釋開始)      
* 程序的版權和版本聲明部分      
* Copyright (c) 2011, 煙臺大學計算機學院學生       
* All rights reserved.      
* 文件名稱:                                    
* 作    者:   王明星                     
* 完成日期:   2012   年  4 月 16 日      
* 版 本 號:                
* 對任務及求解方法的描述部分      
* 輸入描述:      
* 問題描述:實現分數類中的<<和>>運算符重載,實現分數的輸入和輸出     
* 程序頭部的註釋結束      
*/    
#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); //構造函數,初始時,當前速度總爲0且處在停車狀態
 void start();  //由rest狀態到running, 初速爲1
 void stop(); //由running狀態到rest, 當前速度小於5時,才允許停車
 void speed_up();  //加速,調用1次,速度加1
 void slow_down(); //減速,調用1次,速度減1,速度爲0時,停車
};
vehicle::vehicle(int maxS,int w)
{
 maxSpeed=maxS;
 currentSpeed=0;
 status=rest;
}
void vehicle::start()//由rest狀態到running, 初速爲1
{
 if(status==running)
 {
  cout<<"您的車已經啓動,無需重新啓動!"<<endl;
 }
 else
 {
  
  status=running;
  currentSpeed=1;
 }
}
void vehicle::stop()//由running狀態到rest, 當前速度小於5時,才允許停車
{
 if(currentSpeed>=5)
 {
  cout<<"車速過大,請減速後停車! "<<endl;
 }
 else
 {
  currentSpeed=0;
  status=rest;
 }
}
void vehicle::speed_up()//加速,調用1次,速度加1
{
 if(currentSpeed>=maxSpeed)
 {
  cout<<"爲了您的安全,您已被限制加速!"<<endl;
 }
 else
 {
  currentSpeed++;
 }
}
void vehicle::slow_down()//減速,調用1次,速度減1,速度爲0時,停車

{
 if(currentSpeed>0)
  currentSpeed--;
}


class bicycle :virtual public vehicle//(1)自行車類的虛基類爲車輛類
{ 
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//(2)機動車類的虛基類也爲車輛類
{ 
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爲負數。當然車上乘客至少有1個(司機)。上下車時要保證安全……
};
motorcar::motorcar(int maxS,int w,int s,int p):vehicle(maxS,w){seatNum=s;passengerNum=p;}
void motorcar::addPassenger(int p)
{
 stop();
 if(currentSpeed==0)
 {
  passengerNum+=p;
  if(passengerNum>seatNum)
  {
   cout<<"對不起,該車次已經滿員!"<<endl;
   passengerNum=seatNum;
   
  }
 }
 
}


class motorcycle: public bicycle,public motorcar //(3)摩托車類的基類爲自行車類和機動車類
{ 
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(s,p,h),motorcar(maxS,w,s,p){}
void motorcycle::show()
{
 cout<<"最大車速:"<<maxSpeed<<endl;
 cout<<"當前車速:"<<currentSpeed<<endl;
 cout<<"車重:"<<weight<<endl;
 cout<<"車輛狀態:"<<status<<endl;
 cout<<"乘客數量:"<<passengerNum<<endl;
 cout<<"車高:"<<height<<endl;
}


int main( )
{
 motorcycle m;
 bool end=false;
 while (!end){
  cout<<"請操作:1-啓動  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;
}

上機感言:注意在上下車時應該先調用停車函數!!

發佈了59 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章