用C语言+实现电子家谱以实现查看祖先和子孙个人信息、插入家族成员、删除家族成员等功能。
时间: 2024-06-08 07:08:31 浏览: 131
好的,实现电子家谱可以分为以下几个模块:
1. 家族成员信息存储模块:使用结构体或链表来存储每个家族成员的信息,包括姓名、性别、出生日期、死亡日期、父亲、母亲等。
2. 成员关系建立模块:根据输入的成员信息,建立成员之间的关系,包括父子关系、兄弟关系、配偶关系等。
3. 成员信息查询模块:根据输入的成员姓名或ID,查询该成员的详细信息,包括姓名、性别、出生日期、死亡日期、父亲、母亲、配偶、子女等。
4. 成员信息修改模块:根据输入的成员姓名或ID,修改该成员的详细信息。
5. 成员信息删除模块:根据输入的成员姓名或ID,删除该成员的详细信息及其相关关系。
下面是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
typedef struct _family_member {
char name[MAX_NAME_LEN];
char gender;
int birth_year;
int death_year;
struct _family_member *father;
struct _family_member *mother;
struct _family_member *spouse;
struct _family_member *children;
} FamilyMember;
FamilyMember *root = NULL;
FamilyMember *find_member(char *name) {
// 根据姓名查找成员
FamilyMember *p = root;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
return p;
} else {
if (p->children != NULL) {
FamilyMember *q = find_member(name, p->children);
if (q != NULL) {
return q;
}
}
p = p->spouse;
}
}
return NULL;
}
void add_member(char *name, char gender, int birth_year, int death_year, char *father_name, char *mother_name) {
// 添加一个新成员
FamilyMember *father = find_member(father_name);
FamilyMember *mother = find_member(mother_name);
FamilyMember *p = (FamilyMember *)malloc(sizeof(FamilyMember));
strcpy(p->name, name);
p->gender = gender;
p->birth_year = birth_year;
p->death_year = death_year;
p->father = father;
p->mother = mother;
p->spouse = NULL;
p->children = NULL;
if (father != NULL) {
if (father->children == NULL) {
father->children = p;
} else {
FamilyMember *q = father->children;
while (q->spouse != NULL) {
q = q->spouse;
}
q->spouse = p;
}
}
if (mother != NULL) {
if (mother->children == NULL) {
mother->children = p;
} else {
FamilyMember *q = mother->children;
while (q->spouse != NULL) {
q = q->spouse;
}
q->spouse = p;
}
}
if (root == NULL) {
root = p;
}
}
void delete_member(char *name) {
// 删除一个成员及其相关关系
FamilyMember *p = find_member(name);
if (p == NULL) {
printf("Member not found.\n");
return;
}
if (p->father != NULL) {
FamilyMember *q = p->father->children;
if (q == p) {
p->father->children = p->spouse;
} else {
while (q->spouse != p) {
q = q->spouse;
}
q->spouse = p->spouse;
}
}
if (p->mother != NULL) {
FamilyMember *q = p->mother->children;
if (q == p) {
p->mother->children = p->spouse;
} else {
while (q->spouse != p) {
q = q->spouse;
}
q->spouse = p->spouse;
}
}
if (p == root) {
root = NULL;
}
free(p);
}
void print_member(FamilyMember *p) {
// 打印成员信息
printf("Name: %s\n", p->name);
printf("Gender: %c\n", p->gender);
printf("Birth Year: %d\n", p->birth_year);
printf("Death Year: %d\n", p->death_year);
if (p->father != NULL) {
printf("Father: %s\n", p->father->name);
}
if (p->mother != NULL) {
printf("Mother: %s\n", p->mother->name);
}
if (p->spouse != NULL) {
printf("Spouse: %s\n", p->spouse->name);
}
if (p->children != NULL) {
printf("Children:\n");
FamilyMember *q = p->children;
while (q != NULL) {
printf(" %s\n", q->name);
q = q->spouse;
}
}
}
void modify_member(char *name, char gender, int birth_year, int death_year) {
// 修改成员信息
FamilyMember *p = find_member(name);
if (p == NULL) {
printf("Member not found.\n");
return;
}
p->gender = gender;
p->birth_year = birth_year;
p->death_year = death_year;
}
int main() {
add_member("Grandfather", 'M', 1920, 2000, NULL, NULL);
add_member("Grandmother", 'F', 1925, 2010, NULL, NULL);
add_member("Father", 'M', 1950, 2020, "Grandfather", "Grandmother");
add_member("Mother", 'F', 1955, 2030, NULL, NULL);
add_member("Son", 'M', 1980, 2050, "Father", "Mother");
add_member("Daughter", 'F', 1985, 2060, "Father", "Mother");
FamilyMember *p = find_member("Father");
if (p != NULL) {
printf("Father:\n");
print_member(p);
}
delete_member("Mother");
modify_member("Son", 'M', 1980, 2070);
return 0;
}
```
在这个示例代码中,使用了结构体来存储每个家族成员的信息,包括姓名、性别、出生日期、死亡日期、父亲、母亲、配偶、子女等。使用链表来表示成员之间的关系,包括父子关系、兄弟关系、配偶关系等。通过添加、删除、查询、修改等操作,可以实现电子家谱的基本功能。
阅读全文