Created
July 3, 2014 03:25
-
-
Save ivycheung1208/905a819cc6f56aee717a to your computer and use it in GitHub Desktop.
CC150 3.7 Solution 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 ANIMALQUEUESLN_H | |
#define ANIMALQUEUESLN_H | |
#include <iostream> | |
#include <string> | |
#include <list> | |
using namespace std; | |
struct Animal { | |
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(); | |
bool isDog() { return type == 'D'; } | |
bool isCat() { return type == 'C'; } | |
void setOrder(int d) { order = d; } | |
int getOrder() { return order; } | |
private: | |
char type; | |
string name; | |
double age; | |
int order = 0; | |
bool isEmpty() { return type == '\0'; } | |
}; | |
void Animal::display() | |
{ | |
if (isEmpty()) return; | |
cout << "No " << getOrder() << ": "; | |
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> dogQueue; | |
list<Animal> catQueue; | |
int animalNumber = 0; | |
}; | |
void AnimalQueue::enqueue(Animal animalIn) | |
{ | |
animalIn.setOrder(++animalNumber); | |
if (animalIn.isDog()) | |
dogQueue.push_back(animalIn); | |
else if (animalIn.isCat()) | |
catQueue.push_back(animalIn); | |
else | |
cerr << "Cannot distinguish animal type!" << endl; | |
} | |
Animal AnimalQueue::dequeueAny() | |
{ | |
if (dogQueue.empty() && catQueue.empty()) { // both queues are empty | |
cerr << "No animal available!" << endl; | |
return Animal(); | |
} | |
if (dogQueue.empty()) // one of two queues is empty, dequeue the other one | |
return dequeueCat(); | |
else if (catQueue.empty()) | |
return dequeueDog(); | |
if ((dogQueue.begin())->getOrder() < (catQueue.begin())->getOrder()) | |
return dequeueDog(); | |
else | |
return dequeueCat(); | |
} | |
Animal AnimalQueue::dequeueDog() | |
{ | |
if (dogQueue.empty()) { | |
cerr << "No dog available!" << endl; | |
return Animal(); | |
} | |
Animal dogDeque = *dogQueue.begin(); | |
dogQueue.erase(dogQueue.begin()); | |
return dogDeque; | |
} | |
Animal AnimalQueue::dequeueCat() | |
{ | |
if (catQueue.empty()) { | |
cerr << "No cat available!" << endl; | |
return Animal(); | |
} | |
Animal catDeque = *catQueue.begin(); | |
catQueue.erase(catQueue.begin()); | |
return catDeque; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment