-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenePool.cpp
More file actions
84 lines (74 loc) · 2.1 KB
/
Copy pathGenePool.cpp
File metadata and controls
84 lines (74 loc) · 2.1 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
#include "GenePool.h"
#include <sstream>
#include <iostream>
// GenePool Member Functions
GenePool::GenePool(std::istream& stream) {
std::string curr_person;
while(std::getline(stream, curr_person)) {
if (curr_person[0] == '#' || curr_person.length() < 3) {
continue;
}
std::istringstream iss(curr_person);
std::string name;
std::string gender;
std::string mom;
std::string dad;
Person* temp = new Person;
if (std::getline(iss, name, '\t')) { // get name
temp->name(name);
}
if (std::getline(iss, gender, '\t')) { // get gender
if (gender == "male") {
temp->gender(Gender::MALE);
} else {
temp->gender(Gender::FEMALE);
}
}
if (std::getline(iss, mom, '\t')) { // get mother
if (mom == "???") {
temp->mother(nullptr);
} else {
temp->mother(find(mom));
if (find(mom) != nullptr) {
find(mom)->add_child(temp);
}
}
}
if (std::getline(iss, dad)) { // get father
if (dad[dad.length()-1] == '\n') {
dad = dad.substr(0,dad.length()-1);
}
if (dad == "???") {
temp->father(nullptr);
} else {
temp->father(find(dad));
if (find(dad) != nullptr) {
find(dad)->add_child(temp);
}
}
}
//store in map
genepool[temp->name()] = temp;
temp = nullptr;
}
}
GenePool::~GenePool() {
for (auto person : genepool) {
delete person.second;
}
}
std::set<Person*> GenePool::everyone() const {
std::set<Person*> all;
for (auto person : genepool) {
all.insert(person.second);
}
return all;
}
Person* GenePool::find(const std::string& name) const {
for (auto person : genepool) {
if (person.first == name) {
return person.second;
}
}
return nullptr;
}