一个问题^^

xjtlu_joe 2009-05-14 10:36:25
有个停车场系统,要在车位上停车,车位有三个level,1,2,3,每个level有8*20个车位,现在要在里面停车,还有判断车位是否有车,忘高手指教。。。。

类这样:
class parkingslot: public car// the class for the parking slots
{
private:
int level; //parking level
char slot[8][20];//8*20 slot per level
bool flag;//indicate whether the slot is occupied
public:
void locateslot(); //locate a slot for one car
void checktaken(); //check whether the slot is taken
};

要写几个参数。。。
...全文
143 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
pathuang68 2009-05-15
  • 打赏
  • 举报
回复
下面的代码供楼主参考:

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;
}


可以完善的地方:
ParkingLotBase* pPLB1 = new ParkingLotLevel1();可以考虑用Factory Method模式来实现
ltc_mouse 2009-05-15
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 xjtlu_joe 的回复:]
设计?谁能给出个样例。。。。
[/Quote]
这样是否好一点?停车场拥有不同等级,不同数量的停车位...

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
};
建筑师 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 crst_zh 的回复:]
车位继承车?有这样的设计?
[/Quote]


哈哈哈哈。。。。。。
xianyuxiaoqiang 2009-05-14
  • 打赏
  • 举报
回复
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;//被占位
}



lingyin55 2009-05-14
  • 打赏
  • 举报
回复
车位有三个level,1,2,3,每个level有8*20个车位

int level; //parking level
char slot[8][20];//8*20 slot per level

这里的slot和level本来就不能对应。比如slot用来保存level 1,那level 2和3要怎么办?
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
你想表达的意思是车位分三种level
每种level有8×20个车位?
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
根据你的问题,根本就不需要class car这个类
这句话理解不了:
车位有三个level,1,2,3,每个level有8*20个车位

递归?呵呵

xjtlu_joe 2009-05-14
  • 打赏
  • 举报
回复
设计?谁能给出个样例。。。。
liliangbao 2009-05-14
  • 打赏
  • 举报
回复
up
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
觉得class car的信息在这里可以忽略。
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
设计合理才有好的实现,本身设计就有问题,为什么不先考虑设计呢?
星羽 2009-05-14
  • 打赏
  • 举报
回复
这个类的设计本身就有问题,不应该是题目的一部分吧 - -
xjtlu_joe 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]
可以这样考虑,比如slot[0][0]这个位置没车就标为0,有车就标为1.
[/Quote]

具体怎么实现,写点代码我好参考下。。。^^
xjtlu_joe 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 crst_zh 的回复:]
车位继承车?有这样的设计?
[/Quote]

那个无所谓啦,只是要做那两个函数。。
datacode 2009-05-14
  • 打赏
  • 举报
回复
无聊...
ltc_mouse 2009-05-14
  • 打赏
  • 举报
回复
class parkingslot: public car
---------------------
呵呵,这样的类设计不好吧~
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
车位继承车?有这样的设计?
pengzhixi 2009-05-14
  • 打赏
  • 举报
回复
可以这样考虑,比如slot[0][0]这个位置没车就标为0,有车就标为1.

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧