C语言:采用树表示一个家谱关系,采用链表(孩子表示法)作为存储结构,实现家谱关系的相关运算。每个节点信息包含有:姓名、性别、配偶姓名、身份证号、年龄、出生日期等。 功能要求: 1)输入家族信息(要求自己设计文件存储格式,存放当前所有家族信息)2)输出家族信息(要求界面输出格式直观明了) 3)查找某人的所有儿子 4)查找某人的双亲 输出形式要有合理的提示,每个功能可以设立菜单,根据提示,可以完成相关的功能要求。 存储结构: 学生自己根据系统功能要求自己设计,但是要求家谱系统的相关数据要存储在数据文件中。
时间: 2024-01-24 17:20:13 浏览: 60
c语言实现链表的基本操作,链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
以下是一个基于孩子表示法的家谱关系的C语言程序,实现了上述功能要求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 家族成员结构体定义
typedef struct Member {
char name[20]; // 姓名
char gender[5]; // 性别
char spouse[20]; // 配偶姓名
char id[20]; // 身份证号
int age; // 年龄
char birthday[20]; // 出生日期
struct Member *child;// 第一个孩子
struct Member *sibling;// 兄弟姐妹
} Member;
// 创建一个新的成员
Member* create_member(char *name, char *gender, char *spouse, char *id, int age, char *birthday) {
Member *new_member = (Member*)malloc(sizeof(Member));
strcpy(new_member->name, name);
strcpy(new_member->gender, gender);
strcpy(new_member->spouse, spouse);
strcpy(new_member->id, id);
new_member->age = age;
strcpy(new_member->birthday, birthday);
new_member->child = NULL;
new_member->sibling = NULL;
return new_member;
}
// 将一个成员添加为另一个成员的孩子
void add_child(Member *parent, Member *child) {
if (parent->child == NULL) {
parent->child = child;
} else {
Member *sibling = parent->child;
while (sibling->sibling != NULL) {
sibling = sibling->sibling;
}
sibling->sibling = child;
}
}
// 根据姓名查找成员
Member* find_member_by_name(Member *root, char *name) {
if (root == NULL) {
return NULL;
}
if (strcmp(root->name, name) == 0) {
return root;
}
Member *child = root->child;
while (child != NULL) {
Member *found = find_member_by_name(child, name);
if (found != NULL) {
return found;
}
child = child->sibling;
}
return NULL;
}
// 输出一个成员及其所有子孙的信息
void print_member(Member *member, int level) {
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%s (%s, %d岁)\n", member->name, member->gender, member->age);
Member *child = member->child;
while (child != NULL) {
print_member(child, level + 1);
child = child->sibling;
}
}
// 写入家族信息到文件中
void write_family_to_file(Member *root, char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("无法写入文件\n");
return;
}
fprintf(fp, "姓名,性别,配偶姓名,身份证号,年龄,出生日期\n");
fprintf(fp, "%s,%s,%s,%s,%d,%s\n", root->name, root->gender, root->spouse, root->id, root->age, root->birthday);
Member *child = root->child;
while (child != NULL) {
fprintf(fp, "%s,%s,%s,%s,%d,%s\n", child->name, child->gender, child->spouse, child->id, child->age, child->birthday);
child = child->sibling;
}
fclose(fp);
}
// 从文件中读取家族信息
Member* read_family_from_file(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("无法读取文件\n");
return NULL;
}
char line[256];
fgets(line, 256, fp);
char *token = strtok(line, ",");
Member *root = create_member(token, "", "", "", 0, "");
while (fgets(line, 256, fp) != NULL) {
token = strtok(line, ",");
Member *member = create_member(token, "", "", "", 0, "");
token = strtok(NULL, ",");
strcpy(member->gender, token);
token = strtok(NULL, ",");
strcpy(member->spouse, token);
token = strtok(NULL, ",");
strcpy(member->id, token);
token = strtok(NULL, ",");
member->age = atoi(token);
token = strtok(NULL, ",");
strcpy(member->birthday, token);
add_child(root, member);
}
fclose(fp);
return root;
}
// 主函数,实现家族关系的相关运算
int main() {
Member *root = NULL;
char filename[20];
printf("请输入家族信息文件名:");
scanf("%s", filename);
root = read_family_from_file(filename);
if (root == NULL) {
printf("读取家族信息失败\n");
return 0;
}
printf("家族信息如下:\n");
print_member(root, 0);
while (1) {
printf("请选择要进行的操作:\n");
printf("1. 查找某人的所有儿子\n");
printf("2. 查找某人的双亲\n");
printf("3. 退出程序\n");
int choice;
scanf("%d", &choice);
if (choice == 1) {
char name[20];
printf("请输入要查找的人的姓名:");
scanf("%s", name);
Member *member = find_member_by_name(root, name);
if (member == NULL) {
printf("找不到该成员\n");
} else if (member->child == NULL) {
printf("%s没有儿子\n", member->name);
} else {
printf("%s的儿子有:\n", member->name);
Member *child = member->child;
while (child != NULL) {
printf("%s\n", child->name);
child = child->sibling;
}
}
} else if (choice == 2) {
char name[20];
printf("请输入要查找的人的姓名:");
scanf("%s", name);
Member *member = find_member_by_name(root, name);
if (member == NULL) {
printf("找不到该成员\n");
} else if (member == root) {
printf("%s是家族根节点,没有双亲\n", member->name);
} else if (member->sibling == NULL && member->child == NULL) {
printf("%s没有双亲\n", member->name);
} else {
printf("%s的双亲有:\n", member->name);
Member *parent = root;
while (parent != NULL && parent->child != member) {
Member *sibling = parent->child;
while (sibling != NULL && sibling->sibling != member) {
sibling = sibling->sibling;
}
if (sibling != NULL && sibling->sibling == member) {
printf("%s\n", parent->name);
break;
}
parent = parent->sibling;
}
}
} else if (choice == 3) {
printf("程序已退出\n");
write_family_to_file(root, filename);
return 0;
} else {
printf("无效的选择,请重新输入\n");
}
}
}
```
该程序使用孩子表示法来表示家族关系,每个成员都是一个结构体,其中包含姓名、性别、配偶姓名、身份证号、年龄、出生日期等信息,同时还包含指向其第一个孩子和兄弟姐妹的指针。程序将家族信息存储在一个文件中,可以从文件中读取家族信息,并进行相关运算,如查找某人的所有儿子、查找某人的双亲等。程序的输出形式比较直观明了,根据提示可以完成相关功能要求。
阅读全文