猫狗队列
程序员文章站
2022-03-08 08:06:43
...
题目
实现一种猫狗队列的结构,具体要求可参考书本。实现同参考原书
实现过程中遇到的一些问题
首先根据书中类的设计准则,一个入队的新类型要包括当前入队的宠物类型以及入队的时间戳,在出队时要根据时间戳来依次出队。
在popDog()函数设计返回时,由于返回的需要是dog类型,可以初始化一个基类的指针指向派生类的引用,然后再可以让基类指针指向pop类型,返回结果。
代码
#include<iostream>
#include<queue>
#include<string>
using namespace std;
/*猫狗类原型*/
class Pet {
private:
string type;
public:
Pet() {}
Pet(string type)
{
this->type = type;
}
string getPetType()
{
return this->type;
}
};
class dog : public Pet {
public:
dog():Pet("dog") {}
};
class cat : public Pet {
public:
cat() :Pet("cat") {}
};
class petEnterQueue {
private:
Pet pet1;
long count;
public:
petEnterQueue(Pet p, long count)
{
this->pet1 = p;
this->count = count;
}
Pet getPet()
{
return this->pet1;
}
long getCount()
{
return this->count;
}
string getEnterPetType()
{
return this->pet1.getPetType();
}
};
class catDogQueue
{
private:
queue<petEnterQueue> dogQ;
queue<petEnterQueue> catQ;
long count;
public:
catDogQueue()
{
this->count = 0;
}
void push(Pet pet);
Pet popAll();
dog popDog();
cat popCat();
bool isEmpty();
bool isDogQueueEmpty();
bool isCatQueueEmpty();
};
void catDogQueue::push(Pet pet)
{
if (pet.getPetType() == "dog")
dogQ.push(petEnterQueue(pet, this->count++));
else if (pet.getPetType() == "cat")
catQ.push(petEnterQueue(pet, this->count++));
else
printf("Error, not dog or cat!\n");
}
Pet catDogQueue::popAll()
{
if (!dogQ.empty() && !catQ.empty())
{
if (dogQ.front().getCount() < catQ.front().getCount())
{
Pet tmp;
tmp = dogQ.front().getPet();
dogQ.pop();
return tmp;
}
else
{
Pet tmp;
tmp = catQ.front().getPet();
catQ.pop();
return tmp;
}
}
else if (!dogQ.empty())
{
Pet tmp;
tmp = dogQ.front().getPet();
dogQ.pop();
return tmp;
}
else if (!catQ.empty())
{
Pet tmp;
tmp = catQ.front().getPet();
catQ.pop();
return tmp;
}
else
printf("The catDogQueue is empty!\n");
}
dog catDogQueue::popDog()
{
if (!dogQ.empty())
{
Pet tmpP;
tmpP = dogQ.front().getPet();
dog tmp;
Pet* p = &tmp;
*p = tmpP;
dogQ.pop();
return tmp;
}
else
printf("The dogQueue is empty!\n");
}
cat catDogQueue::popCat()
{
if (!catQ.empty())
{
Pet tmpP;
tmpP = catQ.front().getPet();
cat tmp;
Pet *p = &tmp;
*p = tmpP;
catQ.pop();
return tmp;
}
else
printf("The catQueue is empty!\n");
}
bool catDogQueue::isEmpty()
{
return catQ.empty() && dogQ.empty();
}
bool catDogQueue::isCatQueueEmpty()
{
return catQ.empty();
}
bool catDogQueue::isDogQueueEmpty()
{
return dogQ.empty();
}
int main()
{
catDogQueue cdq;
if (cdq.isEmpty())
cout << "The whole queue is empty!" << endl;
cdq.push(dog());
if (!cdq.isDogQueueEmpty())
cout << "Dog queue is not empty!" << endl;
if (!cdq.isCatQueueEmpty())
cout << "Cat queue is not empty!" << endl;
for (int i = 0; i < 2; i++)
{
cdq.push(cat());
cdq.push(dog());
}
cout << "popAll:" << cdq.popAll().getPetType() << endl;
cout << "popDog:" << cdq.popDog().getPetType() << endl;
cout << "popCat:" << cdq.popCat().getPetType() << endl;
cout << "popAll:" << cdq.popAll().getPetType() << endl;
cout << "popAll:" << cdq.popAll().getPetType() << endl;
if (cdq.isEmpty())
cout << "All queue is empty!" << endl;
getchar();
return 0;
}
上一篇: 网页版简历
下一篇: 简单聊聊队列是怎么一回事