65,210
社区成员
发帖
与我相关
我的任务
分享
class ParkingLotBase
{
protected:
bool parkinglots[8][16];
public:
ParkingLotBase()
{
for(short i = 0; i < 8; i++)
for(short j = 0; j < 16; j++)
{
parkinglots[i][j] = false;
}
}
// 在<row, column>的位置停车
virtual bool parking(short row, short column)
{
parkinglots[row][column] = true;
return true;
}
// 判断<row, column>车位是否被占
virtual bool isoccupied(short row, short column)
{
if (parkinglots[row][column])
return true;
else
return false;
}
};
class ParkingLotLevel1 : public ParkingLotBase
{
// 其他成员变量或函数如:费率,收费;
// 也可以将上述成员变量用protected,成员函数virtual函数抽象到基类中
};
class ParkingLotLevel2 : public ParkingLotBase
{
// 其他成员变量或函数如:费率,收费;
// 也可以将上述成员变量用protected,成员函数virtual函数抽象到基类中
};
class ParkingLotLevel3 : public ParkingLotBase
{
// 其他成员变量或函数如:费率,收费;
// 也可以将上述成员变量用protected,成员函数virtual函数抽象到基类中
};
int main(void)
{
ParkingLotBase* pPLB1 = new ParkingLotLevel1();
// 在Level1停车场的<0, 0>位置停车
pPLB1->parking(0, 0);
if(pPLB1->isoccupied(0, 0))
cout << "Position 0 x 0 is occupied!" << endl;
else
cout << "Position 0 x 0 is not occupied!" << endl;
// 在Level2停车场的<0, 0>位置停车
ParkingLotBase* pPLB2 = new ParkingLotLevel2();
pPLB2->parking(1, 0);
if(pPLB2->isoccupied(1, 0))
cout << "Position 1 x 0 is occupied!" << endl;
else
cout << "Position 1 x 0 is not occupied!" << endl;
return 0;
}
class parkingslot
{
private:
int level; //parking level
bool flag; //indicate whether the slot is occupied
car *pCar; //which car is parking here
public:
parkingslot(int _level=-1); //Initialize the slot
void setlevel(int _level); //change the level for the slot
void lockslot(car *pCar); //this slot is occupied
car* unlockslot(); //this slot is released and can be taken by another car
bool checktaken()const; //check whether the slot is taken
};
class carpark
{
parkingslot slots[3][8*20]; //or slots[3][8][20]
public:
carpark();
void car_enter(car *pCar, int lev=0); //a new car arrived, find a slot to park it!
void car_leave(car *pCar); //a car is leaving
void car_leave(int level, int pos); //a car parked in the special position is leaving
int find_slot(int level); //find a slot for a specific level
};
short locateslot(short level, short index1, short index2); //locate a slot for one car
short checktaken(short level, short index1, short index2); //check whether the slot is taken
short locateslot(short level, short index1, short index2){
if(checktaken(level, index1, index2) == -1){
printf("It's taken! level: %d, index1: %d, index2: %d", level, index1, index2);
return -1;//fail
}
else{
slot[level][index1][index2] = 'y';//注意,应该改成三维数组,因为有3个level,每个level又有8×20个位置
}
return 0;//success
}
short checktaken(short level, short index1, short index2){
if(level < 0 || level >2){//注意level为0,1,2,因为程序实现起来节省空间开销。
printf("Error level: %d", level);
return -2;//严重错误
}
if(index1 < 0 || index1 >7){//不能超过7
printf("Error index1: %d", index1);
return -2;//严重错误
}
if(index2 < 0 || index2 > 19){
printf("Error index2: %d", index2);
return -2;//严重错误
}
if(slot[level][index1][index2] != 'y'){//未被占位
return 0;
}
return -1;//被占位
}