C语言家谱管理系统

在这里插入图片描述
在这里插入图片描述
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//printf(“企鹅联系329-----369-----2778”);
#define MAX_NAME_LEN 20

// 家族成员结构体
typedef struct _family_member {
char name[MAX_NAME_LEN]; // 姓名
char gender; // 性别,M代表男性,F代表女性
struct _family_member *father; // 父亲
struct _family_member *mother; // 母亲
struct _family_member *partner; // 配偶
struct _family_member *child; // 孩子链表头指针
struct _family_member *sibling; // 兄弟链表头指针
} family_member;

// 添加孩子
void add_child(family_member *parent, family_member *child) {
child->father = parent;
if (parent->child == NULL) {
parent->child = child;
} else {
family_member *last_child = parent->child;
while (last_child->sibling != NULL) {
last_child = last_child->sibling;
}
last_child->sibling = child;
}
}

// 添加兄弟
void add_sibling(family_member *older_sibling, family_member *younger_sibling) {
younger_sibling->father = older_sibling->father;
if (older_sibling->sibling == NULL) {
older_sibling->sibling = younger_sibling;
} else {
family_member *last_sibling = older_sibling->sibling;
while (last_sibling->sibling != NULL) {
last_sibling = last_sibling->sibling;
}
last_sibling->sibling = younger_sibling;
}
}

// 查找成员
family_member *find_member(family_member *root, char *name) {
if (root == NULL) {
return NULL;
}
if (strcmp(root->name, name) == 0) {
return root;
}
family_member *child = root->child;
while (child != NULL) {
family_member *result = find_member(child, name);
if (result != NULL) {
return result;
}
child = child->sibling;
}
return NULL;
}

// 输出家谱
void print_family_tree(family_member *member, int generation) {
int i;
for (i = 0; i < generation; i++) {
printf(" “);
}
printf(”%s(%c)\n", member->name, member->gender);
family_member *child = member->child;
while (child != NULL) {
print_family_tree(child, generation + 1);
child = child->sibling;
}
}

// 输出某个成员的祖先
void print_ancestors(family_member *member) {
if (member->father == NULL && member->mother == NULL) {
printf(“%s没有祖先\n”, member->name);
return;
}
printf(“%s的祖先:\n”, member->name);
if (member->father != NULL) {
printf(“%s的父亲:%s\n”, member->name, member->father->name);
print_ancestors(member->father);
}
if (member->mother != NULL) {
printf(“%s的母亲:%s\n”, member->name, member->mother->name);
print_ancestors(member->mother);
}
}

// 输出某个成员的子孙
void print_descendants(family_member *member, int generation) {
family_member *child = member->child;
while (child != NULL) {
int i;
for (i = 0; i < generation; i++) {
printf(" “);
}
printf(”%s(%c)\n", child->name, child->gender);
print_descendants(child, generation + 1);
child = child->sibling;
}
}

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言可以用来开发各种类型的应用程序,包括家谱管理系统家谱管理系统是一种用于记录和管理家族成员信息的软件。下面是一个简单的C语言家谱管理系统的示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 struct Person { char name[50]; int age; struct Person *father; struct Person *mother; }; struct Person family[MAX_SIZE]; int count = 0; void addPerson() { if (count >= MAX_SIZE) { printf("家谱已满,无法添加新成员。\n"); return; } struct Person newPerson; printf("请输入成员姓名:"); scanf("%s", newPerson.name); printf("请输入成员年龄:"); scanf("%d", &newPerson.age); if (count > 0) { int fatherIndex, motherIndex; printf("请输入父亲的编号:"); scanf("%d", &fatherIndex); printf("请输入母亲的编号:"); scanf("%d", &motherIndex); newPerson.father = &family[fatherIndex]; newPerson.mother = &family[motherIndex]; } else { newPerson.father = NULL; newPerson.mother = NULL; } family[count] = newPerson; count++; printf("成员添加成功。\n"); } void displayFamily() { printf("家谱成员列表:\n"); for (int i = 0; i < count; i++) { printf("编号:%d,姓名:%s,年龄:%d", i, family[i].name, family[i].age); if (family[i].father != NULL) { printf(",父亲:%s", family[i].father->name); } if (family[i].mother != NULL) { printf(",母亲:%s", family[i].mother->name); } printf("\n"); } } int main() { int choice; while (1) { printf("\n家谱管理系统\n"); printf("1. 添加成员\n"); printf("2. 显示家谱\n"); printf("3. 退出\n"); printf("请选择操作:"); scanf("%d", &choice); switch (choice) { case 1: addPerson(); break; case 2: displayFamily(); break; case 3: printf("谢谢使用,再见!\n"); exit(0); default: printf("无效的选择,请重新输入。\n"); } } return 0; } ``` 这个家谱管理系统使用结构体来表示每个家庭成员,包括姓名、年龄、父亲和母亲。用户可以选择添加成员或显示家谱。每个成员都有一个唯一的编号,可以通过编号来指定父亲和母亲。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值