• 猫狗队列(C++实现)


    实现一种猫狗队列的结构,要求如下:

    1.用户可以调用add方法将cat类或者dog类的实例放入队列中;

    2.用户可以调用pollAll方法,将队列中所有的实例按照进队列的先后顺序依次弹出;

    3.用户可以调用pollDog方法,将队列中的dog类的实例按照进队列的先后循序一次弹出;

    4.用户可以调用pollCat方法,将队列中的cat类的实例按照进队列的先后循序依次弹出;

    5.用户可以调用isEmpty方法,检查队列中是否还有dog或者cat的实例;

    6.用户可以调用isDogEmpty方法,检查队列中是否有dog类的实例;

    7.用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例。

    代码:

    pet.h:

    #ifndef _PET_H
    #define _PET_H
    #include <iostream>
    #include <string>
    using namespace std;
    class Pet
    {
    public:
    Pet(string type){
      this->type = type;
    }
    string getPetType(){
      return this->type;
    }
    private:
    string type;
    };
    class Dog:public Pet{
    public:
      Dog():Pet("dog"){
        std::cout << "doging !!!" << ' ';
      }
    };
    class Cat:public Pet{
    public:
      Cat():Pet("cat"){
        std::cout << "cating !!!" << ' ';
      }
    };
    #endif
     
    PetEnterQueue.h
    #ifndef _PETSTAMP_H
    #define _PETSTAMP_H
    #include <iostream>
    #include "pet.h"
    class PetEnterQueue{
    public:
      PetEnterQueue(Pet p,long count):pet(p){
        this->pet = p;
        this->count = count;
      }
      Pet getPet(){
        return this->pet;
      }
    long getCount(){
      return this->count;
    }
    string getEnterPetType(){
      return this->pet.getPetType();
    }
    private:
     Pet pet;
     long count;
    };
    #endif
     
     
     
    DogCatQue.h:
    #ifndef _CATDOGQUE_H
    #define _CATDOGQUE_H
    #include <iostream>
    #include "pet.h"
    #include "PetEnterQueue.h"
    #include "pet.h"
    #include <queue>
    class DogCatQueue{
    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;
    };
    #endif
     
    DogCatQue.cpp
    #include "DogCatQue.h"
    #include <stdbool.h>
    DogCatQueue::DogCatQueue(){
      count = 0;
    };
    void DogCatQueue::push(Pet pet){
      if("dog"==pet.getPetType())
      {
        dogQ.push(PetEnterQueue(pet,++count));
      }
      else{
        catQ.push(PetEnterQueue(pet,++count));
      }
       return ;
    };
    Pet DogCatQueue::pollAll(){
    if(!isEmpty()){
      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;
      }
    }
    else if (!catQ.empty())
       {
           Pet tmp = catQ.front().getPet();
           catQ.pop();
           return tmp;
       }
    else if (!dogQ.empty())
       {
           Pet tmp = dogQ.front().getPet();
           dogQ.pop();
           return tmp;
       }
    };
    Pet DogCatQueue::pollDog(){
      if(!isDogEmpty()){
        Pet tem_1 = dogQ.front().getPet();
        dogQ.pop();
        return tem_1;
      }
      else{
        std::cout << "isDogEmpty!!!" << ' ';
      }
    };
    Pet DogCatQueue::pollCat(){
      if(!isCatEmpty()){
        Pet tem_2 = catQ.front().getPet();
        catQ.pop();
        return tem_2;
      }
      else{
        std::cout << "isCatEmpty!!!" << ' ';
      }
    }
    bool DogCatQueue::isEmpty(){
      if(dogQ.empty()&&catQ.empty()){
        return true;
      }
      else{
        return false;
      }
    }
    bool DogCatQueue::isDogEmpty(){
      if(dogQ.empty()){
        return true;
      }
      else{
        return false;
      }
    }
    bool DogCatQueue::isCatEmpty(){
      if(catQ.empty()){
        return true;
      }
      else{
        return false;
      }
    }
     
     
    test.cpp
    #include "DogCatQue.h"
    #include <iostream>
    #include "DogCatQue.cpp"
    using namespace std;
    bool DogCatQueue::dogQempty(){
      if(dogQ.empty()){
        return true;
      }
      else{
        return false;
      }
    };
    bool DogCatQueue::catQempty(){
      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;
       cin.get();
       if(DC.isEmpty()){
           cout << "All queue is empty!" << endl;
           cin.get();
         }
    return 0;
    }
    程序运行结果:
     
     
     
     
     
  • 相关阅读:
    JQuery上传插件Uploadify使用详解
    jquery easyui datagrid使用参考
    easyui datagrid使用(好)
    灵活运用 SQL SERVER FOR XML PATH
    C# HttpRequest 中文编码问题
    echarts简单使用
    [bootstrap] 修改字体
    css :not 选择器
    [win7] 带网络的安全模式,启动QQEIMPlatform第三方服务
    [mysql] 添加用户,赋予不同的管理权限
  • 原文地址:https://www.cnblogs.com/shiheyuanfang/p/13412581.html
Copyright © 2020-2023  润新知