ミッション3:オートバイ類
6661 ワード
/* ( )
*
* Copyright (c) 2011,
* All rights reserved.
* :
* :
* : 2012 5 9
* :
*
* : , , , 。(1)
* :
* :
*
*/
#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;
weight=w;
}
void vehicle::start()// rest running, 1
{
if(status==running)
{
cout<<" , !"<<endl;
}
else
{
status=running;
currentSpeed=1;
}
}
void vehicle::stop()// running rest, 5 ,
{
if(status==running)
if(currentSpeed>=5)
{
cout<<" , ! "<<endl;
}
else
{
currentSpeed=0;
status=rest;
}
else
cout<<" ?"<<endl;
}
void vehicle::speed_up()// , 1 , 1
{
if(currentSpeed>=maxSpeed)
{
cout<<" , !"<<endl;
}
else
{
currentSpeed++;
}
}
void vehicle::slow_down()// , 1 , 1, 0 ,
{
if(currentSpeed>0)
--currentSpeed;
if(currentSpeed==0)
status=rest;
}
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)
{
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 //(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<<" :";
if(status==rest)
cout<<" ;\t";
else
cout<<" ;\t";
cout<<" :"<<currentSpeed<<" / "<< maxSpeed <<"\t :"<<passengerNum<<" / "<< seatNum << 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;
}
結果:
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了操作(駐車)状態:駐車;車速:0/90現在乗員:1/3
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了操作(有人下車)運転手は職場を離れないでください!ステータス:駐車;車速:0/90現在乗員:1/3
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了操作(有人乗車)状態:駐車;車速:0/90現在乗員:2/3
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了操作(起動)状態:走行;車速:1/90現在乗員:2/3
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了操作(加速)状態:走行;車速:2/90現在乗員:2/3
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了操作(加速)状態:走行;車速:3/90現在乗員:2/3
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了操作(加速)状態:走行;車速:4/90現在乗員:2/3
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了操作(減速)状態:走行;車速:3/90現在乗員:2/3
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了操作(減速)状態:走行;車速:2/90現在乗員:2/3
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了操作(有人乗車)車両が走行しており、駐車してから乗降します!ステータス:進行;車速:2/90現在乗員:2/3
操作してください:1-起動2-加速3-減速4-有人乗車5-有人下車6-駐車0-終了状態:走行;車速:2/90現在乗員:2/3
まとめ:バイクも運転しにくいですね!ちょっと煩わしいです.また友好的なインターフェースが必要で、更によく顽张りましょう!