forked from xperzy/careerup150
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-7.cpp
126 lines (109 loc) · 2.56 KB
/
3-7.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <ctime>
#include <list>
#include <string>
using namespace std;
class Animal{
int order;
string name;
string type;
public:
Animal(string str): name(str) {}
virtual void setOrder(int o){order=o;}
virtual void setType(string typ){type=typ;}
virtual string getType(){return type;}
virtual int getOrder(){return order;}
virtual string getName(){return name;}
bool operator> (const Animal& ani) const {
return order>ani.order;
}
bool operator< (const Animal& ani) const {
return order<ani.order;
}
};
class Cat:public Animal{
public:
Cat(string str):Animal(str){Animal::setType("Cat");};
};
class Dog:public Animal{
public:
Dog(string str):Animal(str){Animal::setType("Dog");}
};
class Shelter{
list<Cat> cats;
list<Dog> dogs;
int order;
public:
Shelter():order(0){}
void enqueue(Animal ani){
order++;
ani.setOrder(order);
if (ani.getType()=="Cat"){
cats.push_back(*(Cat*) &ani);
}
if (ani.getType()=="Dog"){
dogs.push_back(*(Dog*) &ani);
}
cout << "Enqueue: " << ani.getName() << " on time: " << ani.getOrder() << endl;
}
Animal dequeueAny(){
if (cats.empty() && dogs.empty()){cerr<<"Empty Shelter!!" << endl;}
else{
if (!cats.empty() && dogs.empty()){
cout << "No dogs in shelter" << endl;
return dequeueCat();
}
if (cats.empty() && !dogs.empty()){
cout << "No cats in shelter" << endl;
return dequeueDog();
}
if (!cats.empty() && !dogs.empty()){
//if (cats.front().getOrder()>dogs.front().getOrder()){
if (cats.front()>dogs.front()){
return dequeueDog();
}else{
return dequeueCat();
}
}
}
};
Dog dequeueDog(){
if (!dogs.empty()){
Dog ani = dogs.front();
dogs.pop_front();
return ani;
}else{
cerr<< "Empty dogs" << endl;
}
};
Cat dequeueCat(){
if (!cats.empty()){
Cat ani = cats.front();
cats.pop_front();
return ani;
}else{
cerr<< "Empty cats" << endl;
}
}
};
int main(){
Shelter sh;
Cat c1("Cat1");
Cat c2("Cat2");
Cat c3("Cat3");
Dog d1("Dog1");
Dog d2("Dog2");
Dog d3("Dog3");
sh.enqueue(c1);
sh.enqueue(d1);
sh.enqueue(d2);
sh.enqueue(c2);
sh.enqueue(c3);
sh.enqueue(d3);
Animal ani = sh.dequeueAny();
cout << "DeuqeAny: " << ani.getName() << endl;
Cat cc = sh.dequeueCat();
cout << "DeuqeCat: " << cc.getName() << endl;
Dog dd = sh.dequeueDog();
cout << "DeuqeDog: " << dd.getName() << endl;
}