实现一种猫狗队列的结构,要求如下:
1.用户可以调用add方法将cat类或者dog类的实例放入队列中;
2.用户可以调用pollAll方法,将队列中所有的实例按照进队列的先后顺序依次弹出;
3.用户可以调用pollDog方法,将队列中的dog类的实例按照进队列的先后循序一次弹出;
4.用户可以调用pollCat方法,将队列中的cat类的实例按照进队列的先后循序依次弹出;
5.用户可以调用isEmpty方法,检查队列中是否还有dog或者cat的实例;
6.用户可以调用isDogEmpty方法,检查队列中是否有dog类的实例;
7.用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例。
代码:
pet.h:
#define _PET_H
#include <string>
using namespace std;
{
public:
Pet(string type){
}
string getPetType(){
}
string type;
};
Dog():Pet("dog"){
std::cout << "doging !!!" << ' ';
}
};
public:
Cat():Pet("cat"){
std::cout << "cating !!!" << ' ';
}
};
#endif
#define _PETSTAMP_H
#include <iostream>
#include "pet.h"
public:
PetEnterQueue(Pet p,long count):pet(p){
this->pet = p;
this->count = count;
}
return this->pet;
}
Pet pet;
long count;
};
#endif
#define _CATDOGQUE_H
#include <iostream>
#include "pet.h"
#include "PetEnterQueue.h"
#include "pet.h"
#include <queue>
public:
DogCatQueue();
void push(Pet pet);
Pet pollAll();
Pet pollDog();
Pet pollCat();
bool isEmpty();
bool isDogEmpty();
bool isCatEmpty();
bool dogQempty();
bool catQempty();
private:
queue <PetEnterQueue> dogQ;
queue <PetEnterQueue> catQ;
long count;
};
#include <stdbool.h>
count = 0;
};
if("dog"==pet.getPetType())
{
dogQ.push(PetEnterQueue(pet,++count));
}
else{
catQ.push(PetEnterQueue(pet,++count));
}
return ;
};
if(dogQ.front().getCount()<catQ.front().getCount())
{
Pet tem = dogQ.front().getPet();
dogQ.pop();
return tem;
}
else{
Pet tm = catQ.front().getPet();
catQ.pop();
return tm;
}
}
{
Pet tmp = catQ.front().getPet();
catQ.pop();
return tmp;
}
else if (!dogQ.empty())
{
Pet tmp = dogQ.front().getPet();
dogQ.pop();
return tmp;
}
};
Pet tem_1 = dogQ.front().getPet();
dogQ.pop();
return tem_1;
}
else{
std::cout << "isDogEmpty!!!" << ' ';
}
};
if(!isCatEmpty()){
Pet tem_2 = catQ.front().getPet();
catQ.pop();
return tem_2;
}
else{
std::cout << "isCatEmpty!!!" << ' ';
}
}
if(dogQ.empty()&&catQ.empty()){
return true;
}
else{
return false;
}
}
if(dogQ.empty()){
return true;
}
else{
return false;
}
}
if(catQ.empty()){
return true;
}
else{
return false;
}
#include <iostream>
#include "DogCatQue.cpp"
using namespace std;
if(dogQ.empty()){
return true;
}
else{
return false;
}
};
if(catQ.empty()){
return true;
}
else{
return false;
}
};
int main(){
DogCatQueue DC;
if(DC.dogQempty()){
std::cout << "dogQ is empty!!!" << ' ';
}
if(DC.catQempty()){
std::cout << "catQ is empty!!!" << ' ';
}
for(int i=0;i<2;i++){
DC.push(Dog());
DC.push(Cat());
}
cout << "popAll:" << DC.pollAll().getPetType() << endl;
cout << "popCat:" << DC.pollCat().getPetType() << endl;
cout << "popAll:" << DC.pollAll().getPetType() << endl;
if(DC.isEmpty()){
cout << "All queue is empty!" << endl;
cin.get();
}
return 0;