-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.6(Animal).cpp
More file actions
104 lines (92 loc) · 1.78 KB
/
3.6(Animal).cpp
File metadata and controls
104 lines (92 loc) · 1.78 KB
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
#include <iostream>
#include <list>
using namespace std;
//https://github.com/wzhishen/cracking-the-coding-interview/blob/master/src/chap03/Q7.java
class Wrap{
class Animal{
public:
int timestamp;
string name;
Animal(string n){
name=n;
timestamp =0;
}
};
class Dog: public Animal{
public:
Dog(string n){
Animal(n);
}
int time(){
cout<<"Dog: "<<name;
return timestamp;
}
};
class Cat: public Animal{
public:
Cat(string n){
Animal(n);
}
int time(){
cout<<"Cat: "<<name;
return timestamp;
}
};
void enqueue(Animal a, char i){
if(i=='c'){
cats.push_back(a);
}else if(i=='d'){
dogs.push_back(a);
}
else{
throw invalid_argument ("Unknown type of animal!");
}
}
Dog dequequeDog(){
Dog temp;
if(noDog)
throw invalid_argument ("Dog isEmpty");
temp = dogs.front();
return dogs.pop_front();
}
Cat dequequeCat(){
Cat temp;
if(noCat)
throw invalid_argument ("Cat isEmpty");
temp = cats.front();
return cats.pop_front();
}
Animal dequeueAny() {
if (noAnimal()) {
throw invalid_argument("No animals!");
} else if (noDog()) {
return dequeueCat();
} else if (noCat()) {
return dequeueDog();
} else if (peekDog().timestamp < peekCat().timestamp) {
return dequeueDog();
} else {
return dequeueCat();
}
}
Dog peekDog() {
throw invalid_argument ("No Dog");
return dogs.front();
}
Cat peekCat() {
throw invalid_argument ("No cat");
return cats.front();
}
bool noDog() {
return dogs.empty();
}
bool noCat() {
return cats.empty();
}
bool noAnimal() {
return noDog() && noCat();
}
private:
list<Dog> dogs;
list<Cat> cats;
};