用C语言设计并实现一个简单的家谱管理系统。 【基本要求】 (1)建立家族关系并能存储到文件中。 (2)实现家族成员的添加、删除功能。 (3)可以查询家族成员的双亲、祖先、兄弟、 孩子和后代等信息。 (4)按某种顺序输出家谱信息(树的遍历操作)、以树型结构输出家谱资料等功能。
时间: 2023-12-19 07:05:05 浏览: 188
好的,下面是一个简单的家谱管理系统的设计和实现,希望能帮到您。
首先,我们需要定义一个家庭成员的结构体,包括成员的姓名、性别、出生日期、死亡日期、父亲、母亲、兄弟、儿子、女儿等信息。同时,我们需要定义一个家族成员的链表,用于存储所有的家庭成员信息。
```
typedef struct FamilyMember {
char name[20];
char gender[4];
char birth[12];
char death[12];
struct FamilyMember *father;
struct FamilyMember *mother;
struct FamilyMember *brother;
struct FamilyMember *son;
struct FamilyMember *daughter;
} FamilyMember;
typedef struct Family {
FamilyMember *root;
int count;
} Family;
```
其中,root表示家谱的根节点,count表示家族成员的数量。
接下来,我们需要实现家族关系的建立和存储。可以通过读取和解析输入文件来实现家族关系的建立,并将所有成员信息存储在链表中,可以使用文件操作函数来实现信息的存储和读取。
```
Family *createFamily() {
Family *family = (Family *)malloc(sizeof(Family));
family->root = NULL;
family->count = 0;
return family;
}
FamilyMember *createMember(char *name, char *gender, char *birth, char *death) {
FamilyMember *member = (FamilyMember *)malloc(sizeof(FamilyMember));
strcpy(member->name, name);
strcpy(member->gender, gender);
strcpy(member->birth, birth);
strcpy(member->death, death);
member->father = NULL;
member->mother = NULL;
member->brother = NULL;
member->son = NULL;
member->daughter = NULL;
return member;
}
void addMember(Family *family, FamilyMember *member) {
if (family->root == NULL) {
family->root = member;
} else {
FamilyMember *father = member->father;
FamilyMember *mother = member->mother;
if (father != NULL) {
if (father->son == NULL) {
father->son = member;
} else {
FamilyMember *p = father->son;
while (p->brother != NULL) {
p = p->brother;
}
p->brother = member;
}
}
if (mother != NULL) {
if (mother->daughter == NULL) {
mother->daughter = member;
} else {
FamilyMember *p = mother->daughter;
while (p->brother != NULL) {
p = p->brother;
}
p->brother = member;
}
}
}
family->count++;
}
void loadFamilyFromFile(Family *family, char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file %s\n", filename);
return;
}
char buf[1024];
while (fgets(buf, 1024, fp) != NULL) {
char *name = strtok(buf, ",");
char *gender = strtok(NULL, ",");
char *birth = strtok(NULL, ",");
char *death = strtok(NULL, ",");
char *fatherName = strtok(NULL, ",");
char *motherName = strtok(NULL, ",");
FamilyMember *member = createMember(name, gender, birth, death);
if (fatherName != NULL) {
FamilyMember *father = findMember(family, fatherName);
if (father == NULL) {
father = createMember(fatherName, "", "", "");
addMember(family, father);
}
member->father = father;
}
if (motherName != NULL) {
FamilyMember *mother = findMember(family, motherName);
if (mother == NULL) {
mother = createMember(motherName, "", "", "");
addMember(family, mother);
}
member->mother = mother;
}
addMember(family, member);
}
fclose(fp);
}
```
其中,createFamily用于创建一个新的家族,createMember用于创建一个新的家庭成员,addMember用于将一个新的家庭成员加入到家族中,loadFamilyFromFile用于从文件中读取家族信息并建立家族关系。
接下来,我们需要实现家族成员的添加和删除功能。可以通过链表的插入和删除操作来实现。添加成员时,需要先确定其父亲或母亲,然后将其加入到父亲或母亲的儿子或女儿链表中。删除成员时,需要将其从链表中删除,并将其从父亲或母亲的儿子或女儿链表中移除。
```
void addMember(Family *family, FamilyMember *member) {
// 省略代码
family->count++;
}
void deleteMember(Family *family, FamilyMember *member) {
if (member == family->root) {
family->root = NULL;
} else {
FamilyMember *father = member->father;
FamilyMember *mother = member->mother;
if (father != NULL) {
if (father->son == member) {
father->son = member->brother;
} else {
FamilyMember *p = father->son;
while (p->brother != member) {
p = p->brother;
}
p->brother = member->brother;
}
}
if (mother != NULL) {
if (mother->daughter == member) {
mother->daughter = member->brother;
} else {
FamilyMember *p = mother->daughter;
while (p->brother != member) {
p = p->brother;
}
p->brother = member->brother;
}
}
}
free(member);
family->count--;
}
```
其中,addMember用于添加一个新的家庭成员,deleteMember用于删除一个家庭成员。
接下来,我们需要实现查询功能,包括查询成员的双亲、祖先、兄弟、孩子和后代等信息。可以通过遍历链表来实现这些查询操作,如查询成员的父亲和母亲可以直接访问其结构体中的信息,查询成员的祖先、兄弟、孩子和后代可以通过递归遍历实现。
```
FamilyMember *findMember(Family *family, char *name) {
FamilyMember *p = family->root;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
return p;
}
p = p->brother;
}
return NULL;
}
void printParents(FamilyMember *member) {
if (member->father != NULL) {
printf("Father: %s\n", member->father->name);
}
if (member->mother != NULL) {
printf("Mother: %s\n", member->mother->name);
}
}
void printAncestors(FamilyMember *member) {
if (member->father != NULL) {
printf("%s's father: %s\n", member->name, member->father->name);
printAncestors(member->father);
}
if (member->mother != NULL) {
printf("%s's mother: %s\n", member->name, member->mother->name);
printAncestors(member->mother);
}
}
void printSiblings(FamilyMember *member) {
FamilyMember *p = member->father->son;
while (p != NULL) {
if (strcmp(p->name, member->name) != 0) {
printf("Sibling: %s\n", p->name);
}
p = p->brother;
}
}
void printChildren(FamilyMember *member) {
FamilyMember *p = member->son;
while (p != NULL) {
printf("Child: %s\n", p->name);
p = p->brother;
}
}
void printDescendants(FamilyMember *member) {
FamilyMember *p = member->son;
while (p != NULL) {
printf("%s's child: %s\n", member->name, p->name);
printDescendants(p);
p = p->brother;
}
}
```
其中,findMember用于查找指定的家庭成员,printParents用于输出指定成员的双亲,printAncestors用于输出指定成员的祖先,printSiblings用于输出指定成员的兄弟,printChildren用于输出指定成员的孩子,printDescendants用于输出指定成员的后代。
最后,我们需要按照某种顺序输出家谱信息,可以使用树的遍历操作来实现。可以先将所有成员按照出生日期排序,然后按照某种遍历方式(如前序、中序、后序遍历)输出家谱信息。同时,可以使用树型结构输出家谱资料,以便更直观地展示家族成员之间的关系。
```
void inorderTraversal(FamilyMember *member, int level) {
if (member != NULL) {
inorderTraversal(member->son, level + 1);
int i;
for (i = 0; i < level; i++) {
printf("\t");
}
printf("%s\n", member->name);
inorderTraversal(member->brother, level);
}
}
void printFamilyTree(Family *family) {
inorderTraversal(family->root, 0);
}
```
其中,inorderTraversal用于按照中序遍历输出家谱信息,printFamilyTree用于输出树型结构的家谱资料。
完整代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct FamilyMember {
char name[20];
char gender[4];
char birth[12];
char death[12];
struct FamilyMember *father;
struct FamilyMember *mother;
struct FamilyMember *brother;
struct FamilyMember *son;
struct FamilyMember *daughter;
} FamilyMember;
typedef struct Family {
FamilyMember *root;
int count;
} Family;
Family *createFamily() {
Family *family = (Family *)malloc(sizeof(Family));
family->root = NULL;
family->count = 0;
return family;
}
FamilyMember *createMember(char *name, char *gender, char *birth, char *death) {
FamilyMember *member = (FamilyMember *)malloc(sizeof(FamilyMember));
strcpy(member->name, name);
strcpy(member->gender, gender);
strcpy(member->birth, birth);
strcpy(member->death, death);
member->father = NULL;
member->mother = NULL;
member->brother = NULL;
member->son = NULL;
member->daughter = NULL;
return member;
}
void addMember(Family *family, FamilyMember *member) {
if (family->root == NULL) {
family->root = member;
} else {
FamilyMember *father = member->father;
FamilyMember *mother = member->mother;
if (father != NULL) {
if (father->son == NULL) {
father->son = member;
} else {
FamilyMember *p = father->son;
while (p->brother != NULL) {
p = p->brother;
}
p->brother = member;
}
}
if (mother != NULL) {
if (mother->daughter == NULL) {
mother->daughter = member;
} else {
FamilyMember *p = mother->daughter;
while (p->brother != NULL) {
p = p->brother;
}
p->brother = member;
}
}
}
family->count++;
}
void deleteMember(Family *family, FamilyMember *member) {
if (member == family->root) {
family->root = NULL;
} else {
FamilyMember *father = member->father;
FamilyMember *mother = member->mother;
if (father != NULL) {
if (father->son == member) {
father->son = member->brother;
} else {
FamilyMember *p = father->son;
while (p->brother != member) {
p = p->brother;
}
p->brother = member->brother;
}
}
if (mother != NULL) {
if (mother->daughter == member) {
mother->daughter = member->brother;
} else {
FamilyMember *p = mother->daughter;
while (p->brother != member) {
p = p->brother;
}
p->brother = member->brother;
}
}
}
free(member);
family->count--;
}
FamilyMember *findMember(Family *family, char *name) {
FamilyMember *p = family->root;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
return p;
}
p = p->brother;
}
return NULL;
}
void printParents(FamilyMember *member) {
if (member->father != NULL) {
printf("Father
阅读全文