猫狗队列

功能要求:

  • 用户可以调用push方法将cat类或dog类的实例放入队列中;
  • 用户可以调用pollAll方法,将队列中所有的实例按照进队列的先后顺序依次弹出;
  • 用户可以调用pollDog方法,将队列中dog类的实例按照进队列的先后顺序依次弹出;
  • 用户可以调用pollCat方法,将队列中cat类的实例按照进队列的先后顺序依次弹出;
  • 用户可以调用isEmpty方法,检查队列中是否还有dog或cat的实例;
  • 用户可以调用isDogEmpty方法,检查队列中是否有dog类的实例;
  • 用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例。

 

Pet.h

#ifndef _PET_H
#define _PET_H
#include <string>
using namespace std;class Pet{
private:string type;public:Pet(){}Pet(string type){this->type = type;}string getPetType(){return type;}
};class Dog :public Pet{
public:Dog() :Pet("dog") {}
};class Cat :public Pet{
public:Cat() :Pet("cat") {}
};#endif

PetEnterQueue.h

#ifndef _PETENTERQUEUE_H
#define _PETENTERQUEUE_H
#include "Pet.h"
using namespace std;class PetEnterQueue {
private:Pet pet;long count;
public:PetEnterQueue(Pet pet, long count) {this->pet = pet;this->count = count;}Pet getPet() {return this->pet;}long getCount() {return this->count;}string getEnterPetType() {return this->pet.getPetType();}
};
#endif

CatDogQueue.h

#ifndef _CATDOGQUEUE_H
#define _CATDOGQUEUE_H
#include"PetEnterQueue.h"
#include"Pet.h"
#include<iostream>
#include<queue>
using namespace std;class CatDogQueue {
private:queue<PetEnterQueue> dogQ;queue<PetEnterQueue> catQ;long count;public:void push(Pet pet){if (pet.getPetType() == "dog") {this->dogQ.push(PetEnterQueue(pet, count++));}else if(pet.getPetType()=="cat"){this->catQ.push(PetEnterQueue(pet, count++));}}Pet popAll() {if (!catQ.empty() && !dogQ.empty()){if (dogQ.front().getCount() < catQ.front().getCount()){Pet tmp = dogQ.front().getPet();dogQ.pop();return tmp;}else{Pet tmp = catQ.front().getPet();catQ.pop();return tmp;}}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;}else{throw runtime_error("Error empty queue.");}}Dog popDog(){if (!dogQ.empty()){Pet tmpP = dogQ.front().getPet();Dog tmpD;Pet* pd = &tmpD;*pd = tmpP;dogQ.pop();return tmpD;}else{throw runtime_error("Error empty dog queue.");}}Cat popCat(){if (!catQ.empty()){Pet tmpP = catQ.front().getPet();Cat tmpC;Pet* pc = &tmpC;*pc = tmpP;catQ.pop();return tmpC;}else{throw runtime_error("Error empty cat queue.");}}bool isEmpty(){return dogQ.empty() && catQ.empty();}bool isDogEmpty(){return dogQ.empty();}bool isCatEmpty(){return catQ.empty();}
};#endif

test.cpp

#include <iostream>
#include "CatDogQueue.h"using namespace std;
int main()
{CatDogQueue test;if (test.isEmpty())cout << "All queue is empty!" << endl;test.push(Dog());if (!test.isEmpty())cout << "All queue is not empty!" << endl;if (!test.isDogEmpty())cout << "Dog queue is not empty!" << endl;if (test.isCatEmpty())cout << "Cat queue is Empty!" << endl;for (int i = 0; i < 2; i++){test.push(Cat());test.push(Dog());}cout << "popAll:" << test.popAll().getPetType() << endl;cout << "popDog:" << test.popDog().getPetType() << endl;cout << "popCat:" << test.popCat().getPetType() << endl;cout << "popAll:" << test.popAll().getPetType() << endl;cout << "popAll:" << test.popAll().getPetType() << endl;if (test.isEmpty())cout << "All queue is empty!" << endl;return 0;

 

转载于:https://www.cnblogs.com/MuZiShiYe/p/11255490.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392098.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何使用HTML5,JavaScript和Bootstrap构建自定义文件上传器

by Prashant Yadav通过Prashant Yadav 如何使用HTML5&#xff0c;JavaScript和Bootstrap构建自定义文件上传器 (How to build a custom file uploader with HTML5, JavaScript, & Bootstrap) In this short article, we’ll learn how to create custom file uploader wit…

monkey测试===通过monkey测试检查app内存泄漏和cpu占用

最近一直在研究monkey测试。网上资料很多&#xff0c;但都是一个抄一个的。原创的很少 我把检查app内存泄漏的情况梳理一下&#xff1a; 参考资料&#xff1a; Monkey测试策略&#xff1a;https://testerhome.com/topics/597 Android Monkey测试详细介绍&#xff1a;http://www…

数据挖掘—主成分分析法降维和最小最大规范化

算法步骤:1)将原始数据按列组成n行m列矩阵X2)特征中心化。即每一维的数据都减去该维的均值&#xff0c;使每一维的均值都为03)求出协方差矩阵4)求出协方差矩阵的特征值及对应的特征向量5)将特征向量按对应的特征值大小从上往下按行排列成矩阵&#xff0c;取前k行组成矩阵p6)YPX…

用户使用说明c语言,(C语言使用指南.docx

(C语言使用指南Turbo C(V2.0)使用指南(本文的许多命令或方法同样适用于TC3) 在开始看本文以前&#xff0c;我先说明一下C语言的安装和使用中最应该注意的地方&#xff1a;许多网友在下载Turbo C 2.0和Turbo C 3.0后&#xff0c;向我问得最多的是在使用过程中碰到如下问题&…

三维空间两直线/线段最短距离、线段计算算法 【转】

https://segmentfault.com/a/1190000006111226d(ls,lt)|sj−tj||s0−t0(be−cd)u⃗ −(ae−bd)v⃗ ac−bd(ls,lt)|sj−tj||s0−t0(be−cd)u⃗ −(ae−bd)v⃗ ac−b2|具体实现代码如下&#xff08;C#实现&#xff09;&#xff1a; public bool IsEqual(double d1, double d2) { …

【慎思堂】之JS牛腩总结

一 JS基础 1-定义 Javascript是一种脚本语言/描述语言&#xff0c;是一种解释性语言。用于开发交互式web网页&#xff0c;使得网页和用户之间实现了一种实时性的、动态的、交互性的关系&#xff0c;使网页包含更多活跃的元素和更加精彩的内容。 主要用于&#xff1a;表单验证 …

vuejs 轮播_如何在VueJS中设计和构建轮播功能

vuejs 轮播by Fabian Hinsenkamp由Fabian Hinsenkamp设计 A carousel, slideshow, or slider — however you call it this class of UI — has become one of the core elements used in modern web development. Today, it’s almost impossible to find any Website or UI …

iOS绘圆形图-CGContextAddArc各参数说明

2019独角兽企业重金招聘Python工程师标准>>> 1.使用 UIGraphicsGetCurrentContext() 画圆 CGContextAddArc(<#CGContextRef _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFlo…

c语言中if和goto的用法,C语言中if和goto的用法.doc

C语言中if和goto的用法C语言中&#xff0c;if是一个条件语句&#xff0c;用法??if(条件表达式) 语句如果满足括号里面表达式&#xff0c;表示逻辑为真于是执行后面的语句&#xff0c;否则不执行(表达式为真则此表达式的值不为0&#xff0c;为假则为0&#xff0c;也就是说&…

数据挖掘—K-Means算法(Java实现)

算法描述 &#xff08;1&#xff09;任意选择k个数据对象作为初始聚类中心 &#xff08;2&#xff09;根据簇中对象的平均值&#xff0c;将每个对象赋给最类似的簇 &#xff08;3&#xff09;更新簇的平均值&#xff0c;即计算每个对象簇中对象的平均值 &#xff08;4&#xf…

自我价值感缺失的表现_不同类型的缺失价值观和应对方法

自我价值感缺失的表现Before handling the missing values, we must know what all possible types of it exists in the data science world. Basically there are 3 types to be found everywhere on the web, but in some of the core research papers there is one more ty…

[收藏转载]C# GDI+ 简单绘图(一)

最近对GDI这个东西接触的比较多&#xff0c;也做了些简单的实例&#xff0c;比如绘图板&#xff0c;仿QQ截图等&#xff0e; 废话不多说了&#xff0c;我们先来认识一下这个GDI&#xff0c;看看它到底长什么样. GDI&#xff1a;Graphics Device Interface Plus也就是图形设备接…

mybaties总结+hibernate总结

一、对原生态jdbc程序中问题总结 1.1 jdbc程序 需求&#xff1a;使用jdbc查询mysql数据库中用户表的记录 statement:向数据库中发送一个sql语句 预编译statement&#xff1a;好处&#xff1a;提高数据库性能。 预编译statement向数据库中发送一个sql语句&#xff0c;数据库编译…

客户旅程_我如何充分利用freeCodeCamp的旅程

客户旅程by Catherine Vassant (aka Codingk8)由凯瑟琳瓦森(Catherine Vassant)(又名Codingk8) 我如何充分利用freeCodeCamp的旅程 (How I made the most out of my freeCodeCamp journey) 我的路线图&#xff1f; ️超越课程范围的reeCodeCamp (My road map ?️ to freeCode…

Python14 函数

函数 面向对象编程&#xff1a; 类----class 面向过程编程&#xff1a;过程---def 函数式编程&#xff1a;函数---def def test(x):描述x 1return x#def是定义函数的关键字#test是函数名称#&#xff08;x&#xff09;是参数#x1是 函数体&#xff0c;是一段逻辑代码#return 定义…

学习sql注入:猜测数据库_面向数据科学家SQL:学习简单方法

学习sql注入:猜测数据库We don’t pick a hammer and look for nails — that would be an unusual way of solving problems. The usual way of doing business is to identify the problem first, then look for appropriate tools.我们不用锤子找钉子&#xff0c;那是解决问…

android 百度地图3.0,android 百度地图3.0

一&#xff1a;为地图设置事件注意新版本中要有一个getMapmMapView.getMap().setOnMapStatusChangeListener(listener);OnMapStatusChangeListener listener newOnMapStatusChangeListener() {/*** 手势操作地图&#xff0c;设置地图状态等操作导致地图状态开始改变。* param s…

(摘录)sockaddr与sockaddr_in,sockaddr_un结构体详细讲解

struct sockaddr { unsigned short sa_family; /* address family, AF_xxx */ char sa_data[14]; /* 14 bytes of protocol address */ }; sa_family是地址家族&#xff0c;一般都是“AF_xxx”的形式。好像通常大多用的是都是AF_INET。 sa_data是14字节协议…

数据挖掘—K-中心点聚类算法(Java实现)

K-中心点聚类算法 &#xff08;1&#xff09;任意选择k个对象作为初始的簇中心点 &#xff08;2&#xff09;指派每个剩余对象给离他最近的中心点所表示的簇 &#xff08;3&#xff09;选择一个未被选择的中心点直到所有的中心点都被选择过 &#xff08;4&#xff09;选择一个…

使用akka构建高并发程序_如何使用Akka Cluster创建简单的应用程序

使用akka构建高并发程序If you read my previous story about Scalachain, you probably noticed that it is far from being a distributed system. It lacks all the features to properly work with other nodes. Add to it that a blockchain composed by a single node is…