家谱管理系统C++实现
时间: 2023-09-22 21:05:56 浏览: 126
家谱管理系统是一个非常好的C++项目,可以帮助用户管理家族的信息,包括家族成员、家族树、家族历史等等。下面是一个简单的家谱管理系统的C++实现。
首先,我们需要定义家族成员的结构体:
```
struct Member {
string name;
int age;
string gender;
string spouse;
vector<Member*> children;
};
```
其中,name、age、gender和spouse分别表示成员的姓名、年龄、性别和配偶,children是一个指向成员子节点的指针数组。
接下来,我们需要定义一个类来管理家族成员:
```
class FamilyTree {
private:
Member* root;
public:
FamilyTree() {
root = NULL;
}
void addMember(Member* parent, Member* child) {
parent->children.push_back(child);
}
void printTree(Member* member, int depth) {
for (int i = 0; i < depth; i++) {
cout << " ";
}
cout << member->name << endl;
for (int i = 0; i < member->children.size(); i++) {
printTree(member->children[i], depth + 1);
}
}
};
```
在这个类中,我们定义了一个指向根节点的指针root,并实现了两个方法:addMember和printTree。addMember用于向家谱中添加成员,printTree用于打印整个家谱。
最后,我们可以在main函数中进行家谱的管理:
```
int main() {
FamilyTree familyTree;
// 添加家族成员
Member* grandpa = new Member{"Grandpa", 80, "Male", ""};
Member* grandma = new Member{"Grandma", 78, "Female", ""};
Member* father = new Member{"Father", 50, "Male", "Mother"};
Member* mother = new Member{"Mother", 48, "Female", "Father"};
Member* son = new Member{"Son", 25, "Male", ""};
Member* daughter = new Member{"Daughter", 23, "Female", ""};
familyTree.addMember(grandpa, father);
familyTree.addMember(grandma, father);
familyTree.addMember(father, son);
familyTree.addMember(father, daughter);
familyTree.addMember(mother, son);
familyTree.addMember(mother, daughter);
// 打印家谱
familyTree.printTree(grandpa, 0);
return 0;
}
```
在这个例子中,我们添加了六个家族成员,并打印了整个家谱。结果如下:
```
Grandpa
Father
Son
Daughter
Grandma
Father
Mother
Son
Daughter
```
这就是一个简单的家谱管理系统的C++实现。
阅读全文