貓狗隊列

功能要求:

  • 用戶可以調用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.pswp.cn/news/392098.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392098.shtml
英文地址,請注明出處:http://en.pswp.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…