-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_code.cpp
117 lines (95 loc) · 3.19 KB
/
source_code.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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Individual {
private:
string name;
string birthdate;
vector<Individual*> children;
public:
Individual(string n, string b) : name(n), birthdate(b) {}
void addChild(Individual* child) {
children.push_back(child);
}
string getName() const {
return name;
}
string getBirthdate() const {
return birthdate;
}
const vector<Individual*>& getChildren() const {
return children;
}
};
class FamilyTree {
private:
Individual* root;
public:
FamilyTree(string rootName, string rootBirthdate) {
root = new Individual(rootName, rootBirthdate);
}
void addChild(Individual* parent, Individual* child) {
parent->addChild(child);
}
void visualizeTree(const Individual* node, int depth) {
if (node == nullptr) return;
for (int i = 0; i < depth; i++) {
cout << " "; // Indentation for a visual hierarchy
}
cout << node->getName() << " (" << node->getBirthdate() << ")" << endl;
for (const Individual* child : node->getChildren()) {
visualizeTree(child, depth + 1);
}
}
Individual* getRoot() const {
return root;
}
};
int main() {
FamilyTree family("DSA", "01/01/1990");
vector<Individual*> individuals;
individuals.push_back(family.getRoot());
while (true) {
cout << "\nFamily Tree Generator Menu:" << endl;
cout << "1. Add Family Member" << endl;
cout << "2. Visualize Family Tree" << endl;
cout << "3. Exit" << endl;
int choice;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
string parentName, childName, childBirthdate;
cout << "Enter parent's name: ";
cin.ignore();
getline(cin, parentName);
cout << "Enter child's name: ";
getline(cin, childName);
cout << "Enter child's birthdate: ";
getline(cin, childBirthdate);
bool parentFound = false;
for (Individual* parent : individuals) {
if (parent->getName() == parentName) {
Individual* child = new Individual(childName, childBirthdate);
family.addChild(parent, child);
individuals.push_back(child);
parentFound = true;
cout << "Family member added successfully." << endl;
break;
}
}
if (!parentFound) {
cout << "Parent not found in the family tree. Please check the name." << endl;
}
} else if (choice == 2) {
cout << "\nFamily Tree Visualization:" << endl;
family.visualizeTree(family.getRoot(), 0);
} else if (choice == 3) {
cout << "Exiting the program. Goodbye!" << endl;
break;
} else {
cout << "Invalid choice. Please select a valid option." << endl;
}
}
return 0;
}