用C语言设计并实现一个简单的家谱管理系统,要求:建立家族关系并能储存到文件中,实现家族成员的添加、删除功能,可以查询家族成员的双亲、祖先、兄弟、孩子和后代等信息,按树的遍历操作输出家谱信息、以树形结构输出家谱管理资料等功能
时间: 2024-01-03 17:05:56 浏览: 93
好的,这是一个比较复杂的项目,需要一步步来实现。首先,我们需要定义一个家族成员的数据结构,包括姓名、性别、出生日期、父亲、母亲、兄弟、孩子等信息。然后,我们可以通过链表或者树来管理这些成员。
以下是一个简单的结构体定义:
```c
#define NAME_LEN 20
typedef struct _family_member {
char name[NAME_LEN];
char gender; // 'M' or 'F'
char birth[11]; // YYYY-MM-DD
struct _family_member *father;
struct _family_member *mother;
struct _family_member *sibling;
struct _family_member *child;
} family_member;
```
接下来,我们需要实现一些基本的操作,比如添加成员、删除成员、查询成员信息等。这些操作可以通过遍历家谱树来实现。以下是一些示例代码:
```c
// 添加成员
family_member *add_member(char *name, char gender, char *birth, family_member *father, family_member *mother) {
family_member *new_member = (family_member*)malloc(sizeof(family_member));
strcpy(new_member->name, name);
new_member->gender = gender;
strcpy(new_member->birth, birth);
new_member->father = father;
new_member->mother = mother;
new_member->sibling = NULL;
new_member->child = NULL;
// 将新成员加入到兄弟链表中
if (father != NULL && mother != NULL) {
family_member *sibling = father->child;
if (sibling == NULL) {
father->child = new_member;
} else {
while (sibling->sibling != NULL) {
sibling = sibling->sibling;
}
sibling->sibling = new_member;
}
}
return new_member;
}
// 删除成员
void delete_member(family_member *member) {
// 删除与父母的关系
if (member->father != NULL) {
family_member *sibling = member->father->child;
if (sibling == member) {
member->father->child = member->sibling;
} else {
while (sibling->sibling != member) {
sibling = sibling->sibling;
}
sibling->sibling = member->sibling;
}
}
if (member->mother != NULL) {
family_member *sibling = member->mother->child;
if (sibling == member) {
member->mother->child = member->sibling;
} else {
while (sibling->sibling != member) {
sibling = sibling->sibling;
}
sibling->sibling = member->sibling;
}
}
// 删除子孙关系
family_member *child = member->child;
while (child != NULL) {
family_member *next_child = child->sibling;
delete_member(child);
child = next_child;
}
// 释放内存
free(member);
}
// 查询成员信息
family_member *find_member(char *name, family_member *root) {
if (root == NULL) {
return NULL;
}
if (strcmp(name, root->name) == 0) {
return root;
}
family_member *child = root->child;
while (child != NULL) {
family_member *member = find_member(name, child);
if (member != NULL) {
return member;
}
child = child->sibling;
}
return NULL;
}
```
接下来,我们可以实现一些高级的操作,比如输出家谱信息、以树形结构输出家谱等。这些操作同样可以通过遍历家谱树来实现。以下是一些示例代码:
```c
// 输出家谱信息
void print_family_tree(family_member *root) {
printf("%s\n", root->name);
family_member *child = root->child;
while (child != NULL) {
printf("|--");
print_family_tree(child);
child = child->sibling;
}
}
// 以树形结构输出家谱
void print_family_tree_graph(family_member *root, int depth) {
for (int i = 0; i < depth; i++) {
printf(" ");
}
printf("%s\n", root->name);
family_member *child = root->child;
while (child != NULL) {
print_family_tree_graph(child, depth + 1);
child = child->sibling;
}
}
```
最后,我们需要将家谱信息保存到文件中,以便下次打开程序时能够恢复家谱。这可以通过文件读写操作来实现。以下是一些示例代码:
```c
// 保存家谱信息到文件
void save_family_tree(char *filename, family_member *root) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error: cannot open file %s.\n", filename);
return;
}
save_member(fp, root);
fclose(fp);
}
void save_member(FILE *fp, family_member *member) {
fprintf(fp, "%s %c %s", member->name, member->gender, member->birth);
if (member->father != NULL) {
fprintf(fp, " father %s", member->father->name);
}
if (member->mother != NULL) {
fprintf(fp, " mother %s", member->mother->name);
}
fprintf(fp, "\n");
family_member *child = member->child;
while (child != NULL) {
save_member(fp, child);
child = child->sibling;
}
}
// 从文件中读取家谱信息
family_member *load_family_tree(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error: cannot open file %s.\n", filename);
return NULL;
}
family_member *root = NULL;
char line[1024];
while (fgets(line, sizeof(line), fp) != NULL) {
char name[NAME_LEN];
char gender;
char birth[11];
char father_name[NAME_LEN];
char mother_name[NAME_LEN];
if (sscanf(line, "%s %c %s father %s mother %s", name, &gender, birth, father_name, mother_name) == 5) {
family_member *father = find_member(father_name, root);
family_member *mother = find_member(mother_name, root);
family_member *new_member = add_member(name, gender, birth, father, mother);
if (root == NULL) {
root = new_member;
}
} else if (sscanf(line, "%s %c %s father %s", name, &gender, birth, father_name) == 4) {
family_member *father = find_member(father_name, root);
family_member *new_member = add_member(name, gender, birth, father, NULL);
if (root == NULL) {
root = new_member;
}
} else if (sscanf(line, "%s %c %s mother %s", name, &gender, birth, mother_name) == 4) {
family_member *mother = find_member(mother_name, root);
family_member *new_member = add_member(name, gender, birth, NULL, mother);
if (root == NULL) {
root = new_member;
}
} else if (sscanf(line, "%s %c %s", name, &gender, birth) == 3) {
family_member *new_member = add_member(name, gender, birth, NULL, NULL);
if (root == NULL) {
root = new_member;
}
}
}
fclose(fp);
return root;
}
```
这样,一个简单的家谱管理系统就完成了。当然,这只是一个基础的版本,你可以根据自己的需求添加更多的功能。
阅读全文