用c++实现电子家谱以实现查看祖先和子孙个人信息、插入家族成员、删除家族成员等功能。
时间: 2024-06-08 10:08:41 浏览: 127
精选_基于C++实现的家谱管理系统_源码打包
实现电子家谱可以采用树形数据结构,每个节点代表一个家庭成员。以下是一个简单的电子家谱的C++代码实现:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
class Person {
public:
int id;
string name;
string gender;
string birth_date;
vector<Person*> children;
Person(int id, string name, string gender, string birth_date) {
this->id = id;
this->name = name;
this->gender = gender;
this->birth_date = birth_date;
}
};
class FamilyTree {
public:
Person* root;
FamilyTree(string name, string gender, string birth_date) {
root = new Person(1, name, gender, birth_date);
}
void insert(int parent_id, int child_id, string name, string gender, string birth_date) {
Person* child = new Person(child_id, name, gender, birth_date);
Person* parent = search(root, parent_id);
if (parent != NULL) {
parent->children.push_back(child);
} else {
cout << "Parent not found." << endl;
}
}
void remove(int id) {
Person* target = search(root, id);
if (target == NULL) {
cout << "Person not found." << endl;
return;
}
Person* parent = getParent(root, id);
if (parent != NULL) {
for (int i = 0; i < parent->children.size(); i++) {
if (parent->children[i]->id == id) {
parent->children.erase(parent->children.begin() + i);
break;
}
}
} else {
cout << "Cannot remove root." << endl;
}
}
void printAncestors(int id) {
Person* target = search(root, id);
if (target == NULL) {
cout << "Person not found." << endl;
return;
}
Person* parent = getParent(root, id);
while (parent != NULL) {
cout << parent->name << endl;
parent = getParent(root, parent->id);
}
}
void printDescendants(int id) {
Person* target = search(root, id);
if (target == NULL) {
cout << "Person not found." << endl;
return;
}
queue<Person*> q;
q.push(target);
while (!q.empty()) {
Person* curr = q.front();
q.pop();
cout << curr->name << endl;
for (int i = 0; i < curr->children.size(); i++) {
q.push(curr->children[i]);
}
}
}
private:
Person* search(Person* curr, int id) {
if (curr->id == id) {
return curr;
}
for (int i = 0; i < curr->children.size(); i++) {
Person* result = search(curr->children[i], id);
if (result != NULL) {
return result;
}
}
return NULL;
}
Person* getParent(Person* curr, int id) {
for (int i = 0; i < curr->children.size(); i++) {
if (curr->children[i]->id == id) {
return curr;
} else {
Person* result = getParent(curr->children[i], id);
if (result != NULL) {
return result;
}
}
}
return NULL;
}
};
int main() {
FamilyTree ft("Tom", "Male", "1990-01-01");
ft.insert(1, 2, "Mary", "Female", "2010-01-01");
ft.insert(1, 3, "John", "Male", "2012-01-01");
ft.insert(2, 4, "Alice", "Female", "2030-01-01");
ft.insert(2, 5, "Bob", "Male", "2032-01-01");
ft.remove(3);
ft.printAncestors(5);
ft.printDescendants(1);
return 0;
}
```
在上面的代码中,`Person` 类代表一个家庭成员,其中包含了姓名、性别、出生日期等基本信息,以及子女列表。`FamilyTree` 类为家谱树类,包含了插入、删除、查找祖先和子孙等操作。可以通过 `insert` 方法向家谱树中添加新成员,通过 `remove` 方法删除指定成员,通过 `printAncestors` 和 `printDescendants` 方法查找指定成员的祖先和子孙。家谱树以根节点(即家族的始祖)为起点,通过遍历子节点构建整个家庭的关系。
阅读全文