-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask1.cpp
143 lines (135 loc) · 4.13 KB
/
task1.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include "reading.cpp"
int main()
{
reading();
std::vector<Animal *> anims;
std::vector<Person> owners;
std::ifstream file("input.txt");
if (!file.is_open())
{
std::cerr << "File is not open!";
exit(1);
}
if (file.peek() == EOF)
{
std::cerr << "The file is empty!";
exit(1);
}
std::string owner_name = "";
std::string str_OwnerAge = "";
std::string animal_type = "";
std::string breed = "";
std::string animal_name = "";
std::string animal_age = "";
int ownerAge;
int animalAge;
while (!file.eof())
{
getline(file, owner_name, ',');
getline(file, str_OwnerAge, ';');
getline(file, animal_type, ',');
getline(file, breed, ';');
getline(file, animal_name, ';');
getline(file, animal_age);
ownerAge = stoi(str_OwnerAge);
animalAge = stoi(animal_age);
if (breed == "!")
{
breed = "None";
}
if (animal_type == "cat")
{
anims.push_back(new Cat(animal_name, animalAge, breed));
Person pr(owner_name, ownerAge, anims[anims.size() - 1]);
bool isFind = 0;
for (auto &ps : owners)
{
if (ps.GetOwnerName() == owner_name && ps.GetOwnerAge() == ownerAge)
{
ps.Push_Animal(anims[anims.size() - 1]);
isFind = 1;
break;
}
}
if (!isFind)
{
owners.push_back(pr);
}
}
if (animal_type == "dog")
{
anims.push_back(new Dog(animal_name, animalAge, breed));
Person pr(owner_name, ownerAge, anims[anims.size() - 1]);
bool isFind = 0;
for (auto &ps : owners)
{
if (ps.GetOwnerName() == owner_name && ps.GetOwnerAge() == ownerAge)
{
ps.Push_Animal(anims[anims.size() - 1]);
isFind = 1;
break;
}
}
if (!isFind)
{
owners.push_back(pr);
}
}
if (animal_type == "fish")
{
anims.push_back(new Fish(animal_name, animalAge, breed));
Person pr(owner_name, ownerAge, anims[anims.size() - 1]);
bool isFind = 0;
for (auto &ps : owners)
{
if (ps.GetOwnerName() == owner_name && ps.GetOwnerAge() == ownerAge)
{
ps.Push_Animal(anims[anims.size() - 1]);
isFind = 1;
break;
}
}
if (!isFind)
{
owners.push_back(pr);
}
}
std::string owner_name = "";
std::string str_OwnerAge = "";
std::string animal_type = "";
std::string breed = "";
std::string animal_name = "";
std::string animal_age = "";
int ownerAge = 0;
int animalAge = 0;
}
std::cout << "------------------------------------------------\nThe first task:\n";
std::set<std::string> set_animal;
for (int i = 0; i < owners.size(); ++i)
{
std::vector<Animal *> animls = owners[i].GetVectorAnimal();
for (Animal *anim : animls)
{
if (Cat *c = dynamic_cast<Cat *>(anim))
{ // динамическое приведение типов (dynamic_cast)
set_animal.insert("cat");
}
if (Dog *d = dynamic_cast<Dog *>(anim))
{
set_animal.insert("fish");
}
if (Fish *c = dynamic_cast<Fish *>(anim))
{
set_animal.insert("dog");
}
}
std::cout << "Owner " << owners[i].GetOwnerName() << "(" << owners[i].GetOwnerAge() << " years old) has " << set_animal.size() << " different animals\n";
animls.clear();
set_animal.clear();
}
for (Animal *anim : anims) // освобождение памяти
{
delete anim;
}
}