Created
July 3, 2014 03:23
-
-
Save ivycheung1208/1e575a988fcc0e53df01 to your computer and use it in GitHub Desktop.
CC150 3.7 My Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* CC150 3.7 | |
* Create the data structure to mantain a animal shelter system | |
* and implement operations such as enqueue, dequeueAny, dequeueDog and dequeueCat | |
*/ | |
#ifndef ANIMALQUEUE_H | |
#define ANIMALQUEUE_H | |
#include <iostream> | |
#include <string> | |
#include <list> | |
using namespace std; | |
struct Animal { | |
friend class AnimalQueue; | |
Animal() : type('\0'), name(""), age(-1) {} | |
Animal(char c, string s) : type(c), name(s), age(0) {} | |
Animal(char c, string s, double d) : type(c), name(s), age(d) {} | |
void display(); | |
private: | |
char type; | |
string name; | |
double age; | |
int number = 0; | |
bool isEmpty() { return type == '\0'; } | |
bool isDog() { return type == 'D'; } | |
bool isCat() { return type == 'C'; } | |
}; | |
void Animal::display() | |
{ | |
if (isEmpty()) return; | |
cout << "No " << number << ": "; | |
if (isDog()) cout << "Dog, "; | |
else if (isCat()) cout << "Cat, "; | |
cout << "Name is " << name << ", Age is " << age << endl; | |
} | |
class AnimalQueue { | |
public: | |
void enqueue(Animal); | |
Animal dequeueAny(); | |
Animal dequeueDog(); | |
Animal dequeueCat(); | |
private: | |
list<Animal> animalQueue; | |
int animalNumber = 0; | |
}; | |
void AnimalQueue::enqueue(Animal animalIn) | |
{ | |
animalIn.number = ++animalNumber; | |
animalQueue.push_back(animalIn); | |
} | |
Animal AnimalQueue::dequeueAny() | |
{ | |
if (animalQueue.empty()) { | |
cerr << "No animal available!" << endl; | |
return Animal(); | |
} | |
Animal animalDeque = *animalQueue.begin(); | |
animalQueue.erase(animalQueue.begin()); | |
return animalDeque; | |
} | |
Animal AnimalQueue::dequeueDog() | |
{ | |
auto begin = animalQueue.begin(), end = animalQueue.end(); | |
while (begin != end && !(*begin).isDog()) ++begin; | |
if (begin == end) { | |
cerr << "No dog available!" << endl; | |
return Animal(); | |
} | |
Animal animalDeque = *begin; | |
animalQueue.erase(begin); | |
return animalDeque; | |
} | |
Animal AnimalQueue::dequeueCat() | |
{ | |
auto begin = animalQueue.begin(), end = animalQueue.end(); | |
while (begin != end && !(*begin).isCat()) ++begin; | |
if (begin == end) { | |
cerr << "No cat available!" << endl; | |
return Animal(); | |
} | |
Animal animalDeque = *begin; | |
animalQueue.erase(begin); | |
return animalDeque; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment