设计一个家谱管理系统,为家族成员提供各种信息服务。建立至少 20 个成员信息数据, 以直观的方式显示结果。 [基本要求] (1)输入家谱中各成员信息,成员的信息中包括的内容有:姓名、出生日期、婚否、 地址、健在否等,也可以附加其他信息。 (2)实现成员数据的保存和读取。 (3)以树形方式显示家谱信息。 (4)显示第 n 代所有人的信息。 (5)按照姓名查询,输出成员信息(包括本人、双亲和孩子信息)。 (6)按照出生日期查询家族成员名单。 (7)输入两人姓名,确定其关系。 (8)修改某位家族成员信息。 (9)按出生日期对家族中所有人排序。请用C语言设计一套可运行的代码。
时间: 2024-03-20 15:40:12 浏览: 117
好的,我可以为您提供一份基于C语言的家谱管理系统代码。请注意,由于篇幅限制,以下代码仅为示例代码,实际开发中还需要根据需求进行更改和完善。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MEMBERS 20
#define MAX_NAME_LEN 20
#define MAX_ADDRESS_LEN 50
typedef struct {
char name[MAX_NAME_LEN]; // 姓名
char birthdate[11]; // 出生日期,格式为yyyy-mm-dd
char marriage_status[4]; // 婚姻状况,"是"或"否"
char address[MAX_ADDRESS_LEN]; // 地址
char living_status[4]; // 健在状况,"是"或"否"
int parent_index; // 父亲在数组中的下标
int child_index[MAX_MEMBERS]; // 孩子在数组中的下标
int child_num; // 孩子数量
} Member;
void init_members(Member members[]);
void input_member(Member *member);
void save_members(Member members[]);
void load_members(Member members[]);
void display_family_tree(Member members[], int index, int depth);
void display_generation(Member members[], int index, int depth, int n);
void search_by_name(Member members[], char *name);
void search_by_birthdate(Member members[], char *birthdate);
void determine_relationship(Member members[], char *name1, char *name2);
void modify_member(Member members[], char *name);
void sort_by_birthdate(Member members[]);
int main() {
Member members[MAX_MEMBERS];
int i, choice, index, n;
char name[MAX_NAME_LEN];
char birthdate[11];
char name1[MAX_NAME_LEN], name2[MAX_NAME_LEN];
init_members(members); // 初始化数据
while (1) {
printf("\n家谱管理系统菜单\n");
printf("1. 输入成员信息\n");
printf("2. 保存成员信息\n");
printf("3. 读取成员信息\n");
printf("4. 显示家谱树形结构\n");
printf("5. 显示第n代所有人信息\n");
printf("6. 按姓名查询成员信息\n");
printf("7. 按出生日期查询成员信息\n");
printf("8. 确定两人关系\n");
printf("9. 修改成员信息\n");
printf("10. 按出生日期排序\n");
printf("0. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
input_member(&members[MAX_MEMBERS-1]);
break;
case 2:
save_members(members);
break;
case 3:
load_members(members);
break;
case 4:
display_family_tree(members, MAX_MEMBERS-1, 0);
break;
case 5:
printf("请输入代数n:");
scanf("%d", &n);
display_generation(members, MAX_MEMBERS-1, 0, n);
break;
case 6:
printf("请输入成员姓名:");
scanf("%s", name);
search_by_name(members, name);
break;
case 7:
printf("请输入出生日期(格式为yyyy-mm-dd):");
scanf("%s", birthdate);
search_by_birthdate(members, birthdate);
break;
case 8:
printf("请输入两人姓名:");
scanf("%s %s", name1, name2);
determine_relationship(members, name1, name2);
break;
case 9:
printf("请输入要修改成员的姓名:");
scanf("%s", name);
modify_member(members, name);
break;
case 10:
sort_by_birthdate(members);
printf("按出生日期排序后的成员信息:\n");
for (i = 0; i < MAX_MEMBERS; i++) {
if (strlen(members[i].name) > 0) {
printf("姓名:%s,出生日期:%s,婚否:%s,地址:%s,健在否:%s\n",
members[i].name, members[i].birthdate, members[i].marriage_status,
members[i].address, members[i].living_status);
}
}
break;
case 0:
printf("谢谢使用!\n");
exit(0);
default:
printf("输入有误,请重新输入!\n");
}
}
return 0;
}
// 初始化家族成员数据
void init_members(Member members[]) {
int i;
for (i = 0; i < MAX_MEMBERS; i++) {
members[i].parent_index = -1;
members[i].child_num = 0;
memset(members[i].child_index, -1, sizeof(members[i].child_index));
}
// 以下为示例数据,实际开发中需要修改和完善
strcpy(members[0].name, "张三");
strcpy(members[0].birthdate, "1980-01-01");
strcpy(members[0].marriage_status, "是");
strcpy(members[0].address, "北京市朝阳区");
strcpy(members[0].living_status, "是");
members[1].parent_index = 0;
members[0].child_index[0] = 1;
members[0].child_num = 1;
strcpy(members[1].name, "李四");
strcpy(members[1].birthdate, "1982-02-02");
strcpy(members[1].marriage_status, "否");
strcpy(members[1].address, "北京市海淀区");
strcpy(members[1].living_status, "是");
members[2].parent_index = 0;
members[0].child_index[1] = 2;
members[0].child_num = 2;
strcpy(members[2].name, "王五");
strcpy(members[2].birthdate, "1985-03-03");
strcpy(members[2].marriage_status, "是");
strcpy(members[2].address, "上海市浦东新区");
strcpy(members[2].living_status, "否");
members[3].parent_index = 2;
members[2].child_index[0] = 3;
members[2].child_num = 1;
strcpy(members[3].name, "赵六");
strcpy(members[3].birthdate, "1988-04-04");
strcpy(members[3].marriage_status, "否");
strcpy(members[3].address, "上海市黄浦区");
strcpy(members[3].living_status, "是");
members[4].parent_index = 2;
members[2].child_index[1] = 4;
members[2].child_num = 2;
strcpy(members[4].name, "孙七");
strcpy(members[4].birthdate, "1990-05-05");
strcpy(members[4].marriage_status, "否");
strcpy(members[4].address, "广州市天河区");
strcpy(members[4].living_status, "是");
members[5].parent_index = 3;
members[3].child_index[0] = 5;
members[3].child_num = 1;
strcpy(members[5].name, "周八");
strcpy(members[5].birthdate, "1993-06-06");
strcpy(members[5].marriage_status, "是");
strcpy(members[5].address, "广州市越秀区");
strcpy(members[5].living_status, "否");
members[6].parent_index = 3;
members[3].child_index[1] = 6;
members[3].child_num = 2;
strcpy(members[6].name, "吴九");
strcpy(members[6].birthdate, "1995-07-07");
strcpy(members[6].marriage_status, "否");
strcpy(members[6].address, "深圳市南山区");
strcpy(members[6].living_status, "是");
members[7].parent_index = 4;
members[4].child_index[0] = 7;
members[4].child_num = 1;
strcpy(members[7].name, "郑十");
strcpy(members[7].birthdate, "1998-08-08");
strcpy(members[7].marriage_status, "否");
strcpy(members[7].address, "深圳市福田区");
strcpy(members[7].living_status, "是");
members[8].parent_index = 4;
members[4].child_index[1] = 8;
members[4].child_num = 2;
strcpy(members[8].name, "冯十一");
strcpy(members[8].birthdate, "2000-09-09");
strcpy(members[8].marriage_status, "否");
strcpy(members[8].address, "珠海市香洲区");
strcpy(members[8].living_status, "否");
}
// 输入成员信息
void input_member(Member *member) {
printf("请输入成员姓名:");
scanf("%s", member->name);
printf("请输入成员出生日期(格式为yyyy-mm-dd):");
scanf("%s", member->birthdate);
printf("请输入成员婚否状态(是或否):");
scanf("%s", member->marriage_status);
printf("请输入成员地址:");
scanf("%s", member->address);
printf("请输入成员健在状态(是或否):");
scanf("%s", member->living_status);
printf("请输入父亲姓名:");
char father_name[MAX_NAME_LEN];
scanf("%s", father_name);
int i;
for (i = 0; i < MAX_MEMBERS; i++) {
if (strcmp(father_name, members[i].name) == 0) {
member->parent_index = i;
members[i].child_index[members[i].child_num] = MAX_MEMBERS-1;
members[i].child_num++;
printf("已将该成员添加至%s的孩子中。\n", father_name);
break;
}
}
if (i == MAX_MEMBERS) {
printf("没有找到该成员的父亲,请检查输入是否有误。\n");
}
}
// 保存成员信息到文件
void save_members(Member members[]) {
FILE *fp;
fp = fopen("members.dat", "wb");
if (fp == NULL) {
printf("文件保存失败!\n");
return;
}
fwrite(members, sizeof(Member), MAX_MEMBERS, fp);
fclose(fp);
printf("文件保存成功!\n");
}
// 读取成员信息文件
void load_members(Member members[]) {
FILE *fp;
fp = fopen("members.dat", "rb");
if (fp == NULL) {
printf("读取文件失败!\n");
return;
}
fread(members, sizeof(Member), MAX_MEMBERS, fp);
fclose(fp);
printf("读取文件成功!\n");
}
// 以树形结构显示家谱信息
void display_family_tree(Member members[], int index, int depth) {
int i;
for (i = 0; i < depth; i++)
阅读全文