用c语言写一个程序,实现家谱管理系统的设计与实现 ,设计并实现一个简单的家谱管理系统,要求建立家族关系并能存储到文件中, 实现家族成员的添加、删除功能,可以查询家族成员的双亲、祖先、兄弟、 孩子和后代等信息,按某种顺序输出家谱信息(树的遍历操作)、以树型结构输出家谱资料等功能
时间: 2024-03-15 16:43:51 浏览: 65
好的,下面是一个简单的家谱管理系统程序示例(使用C语言实现):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
// 家族成员结构体
typedef struct member {
char name[MAX_NAME_LEN]; // 姓名
char gender; // 性别
char birthdate[11]; // 出生日期
struct member *father; // 父亲节点指针
struct member *mother; // 母亲节点指针
struct member *spouse; // 配偶节点指针
struct member *sibling; // 兄弟姐妹节点指针
struct member *child; // 孩子节点指针
} Member;
// 根据姓名查找家族成员节点
Member *find_member(char *name, Member *root);
// 添加家族成员
void add_member(Member **root);
// 删除家族成员
void delete_member(Member **root);
// 输出家族成员信息
void print_member(Member *member);
// 先序遍历家谱
void preorder_traversal(Member *root);
// 中序遍历家谱
void inorder_traversal(Member *root);
// 后序遍历家谱
void postorder_traversal(Member *root);
// 以树型结构输出家谱
void print_family_tree(Member *root, int level);
int main() {
Member *root = NULL;
int choice;
while (1) {
printf("1. 添加家族成员\n");
printf("2. 删除家族成员\n");
printf("3. 查询家族成员\n");
printf("4. 先序遍历家谱\n");
printf("5. 中序遍历家谱\n");
printf("6. 后序遍历家谱\n");
printf("7. 以树型结构输出家谱\n");
printf("0. 退出程序\n");
printf("请输入操作选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_member(&root);
break;
case 2:
delete_member(&root);
break;
case 3: {
char name[MAX_NAME_LEN];
printf("请输入要查询的家族成员姓名:");
scanf("%s", name);
Member *member = find_member(name, root);
if (member == NULL) {
printf("家族成员 %s 不存在\n", name);
} else {
printf("%s 的信息如下:\n", member->name);
print_member(member);
}
break;
}
case 4:
printf("先序遍历家谱:\n");
preorder_traversal(root);
printf("\n");
break;
case 5:
printf("中序遍历家谱:\n");
inorder_traversal(root);
printf("\n");
break;
case 6:
printf("后序遍历家谱:\n");
postorder_traversal(root);
printf("\n");
break;
case 7:
printf("以树型结构输出家谱:\n");
print_family_tree(root, 0);
break;
case 0:
printf("程序已退出\n");
return 0;
default:
printf("无效的操作选项,请重新输入\n");
}
}
}
Member *find_member(char *name, Member *root) {
if (root == NULL) {
return NULL;
}
if (strcmp(name, root->name) == 0) {
return root;
}
Member *member = find_member(name, root->sibling);
if (member != NULL) {
return member;
}
return find_member(name, root->child);
}
void add_member(Member **root) {
Member *member = (Member *) malloc(sizeof(Member));
printf("请输入家族成员姓名:");
scanf("%s", member->name);
printf("请输入家族成员性别(M/F):");
scanf(" %c", &member->gender);
printf("请输入家族成员出生日期(YYYY-MM-DD):");
scanf("%s", member->birthdate);
member->father = NULL;
member->mother = NULL;
member->spouse = NULL;
member->sibling = NULL;
member->child = NULL;
if (*root == NULL) {
*root = member;
} else {
char parent_name[MAX_NAME_LEN];
printf("请输入家族成员的父亲姓名(如果没有请直接回车):");
scanf("%s", parent_name);
if (strlen(parent_name) > 0) {
Member *father = find_member(parent_name, *root);
if (father == NULL) {
printf("家族成员 %s 不存在\n", parent_name);
free(member);
return;
}
printf("请输入家族成员的母亲姓名(如果没有请直接回车):");
scanf("%s", parent_name);
if (strlen(parent_name) > 0) {
Member *mother = find_member(parent_name, *root);
if (mother == NULL) {
printf("家族成员 %s 不存在\n", parent_name);
free(member);
return;
}
member->father = father;
member->mother = mother;
if (father->child == NULL) {
father->child = member;
} else {
Member *sibling = father->child;
while (sibling->sibling != NULL) {
sibling = sibling->sibling;
}
sibling->sibling = member;
}
if (mother->child == NULL) {
mother->child = member;
} else {
Member *sibling = mother->child;
while (sibling->sibling != NULL) {
sibling = sibling->sibling;
}
sibling->sibling = member;
}
} else {
member->father = father;
if (father->child == NULL) {
father->child = member;
} else {
Member *sibling = father->child;
while (sibling->sibling != NULL) {
sibling = sibling->sibling;
}
sibling->sibling = member;
}
}
} else {
printf("请输入家族成员的母亲姓名(如果没有请直接回车):");
scanf("%s", parent_name);
if (strlen(parent_name) > 0) {
Member *mother = find_member(parent_name, *root);
if (mother == NULL) {
printf("家族成员 %s 不存在\n", parent_name);
free(member);
return;
}
member->mother = mother;
if (mother->child == NULL) {
mother->child = member;
} else {
Member *sibling = mother->child;
while (sibling->sibling != NULL) {
sibling = sibling->sibling;
}
sibling->sibling = member;
}
} else {
printf("家族成员 %s 没有父母关系\n", member->name);
}
}
}
printf("家族成员 %s 添加成功\n", member->name);
}
void delete_member(Member **root) {
char name[MAX_NAME_LEN];
printf("请输入要删除的家族成员姓名:");
scanf("%s", name);
Member *member = find_member(name, *root);
if (member == NULL) {
printf("家族成员 %s 不存在\n", name);
return;
}
if (member->child != NULL) {
printf("家族成员 %s 有子女,不能删除\n", name);
return;
}
if (member == *root) {
*root = member->sibling;
} else {
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;
}
}
printf("家族成员 %s 已删除\n", member->name);
free(member);
}
void print_member(Member *member) {
printf("姓名:%s\n", member->name);
printf("性别:%c\n", member->gender);
printf("出生日期:%s\n", member->birthdate);
if (member->father != NULL) {
printf("父亲:%s\n", member->father->name);
}
if (member->mother != NULL) {
printf("母亲:%s\n", member->mother->name);
}
if (member->spouse != NULL) {
printf("配偶:%s\n", member->spouse->name);
}
if (member->sibling != NULL) {
printf("兄弟姐妹:");
Member *sibling = member->sibling;
while (sibling != NULL) {
printf("%s ", sibling->name);
sibling = sibling->sibling;
}
printf("\n");
}
if (member->child != NULL) {
printf("孩子:");
Member *child = member->child;
while (child != NULL) {
printf("%s ", child->name);
child = child->sibling;
}
printf("\n");
}
}
void preorder_traversal(Member *root) {
if (root != NULL) {
print_member(root);
preorder_traversal(root->child);
preorder_traversal(root->sibling);
}
}
void inorder_traversal(Member *root) {
if (root != NULL) {
inorder_traversal(root->child);
print_member(root);
inorder_traversal(root->sibling);
}
}
void postorder_traversal(Member *root) {
if (root != NULL) {
postorder_traversal(root->child);
postorder_traversal(root->sibling);
print_member(root);
}
}
void print_family_tree(Member *root, int level) {
if (root != NULL) {
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%s\n", root->name);
print_family_tree(root->child, level + 1);
print_family_tree(root->sibling, level);
}
}
```
这个程序实现了家族成员的添加、删除、查询功能,以及先序、中序、后序遍历和树型输出家谱等功能。需要注意的是,这只是一个简单的示例程序,还有很多可以改进和优化的地方。
阅读全文