用c语言完成家族中家谱的设计
时间: 2024-02-21 17:57:10 浏览: 102
以下是一个简单的家谱设计,使用C语言实现:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 50
// 家族成员结构体
typedef struct {
char name[MAX_NAME_LEN]; // 姓名
int age; // 年龄
char gender; // 性别
char spouse[MAX_NAME_LEN]; // 配偶
char father[MAX_NAME_LEN]; // 父亲
char mother[MAX_NAME_LEN]; // 母亲
char children[MAX_NAME_LEN][MAX_NAME_LEN]; // 子女
} FamilyMember;
// 初始化家族成员
FamilyMember init_member(char *name, int age, char gender, char *spouse, char *father, char *mother) {
FamilyMember member;
strcpy(member.name, name);
member.age = age;
member.gender = gender;
strcpy(member.spouse, spouse);
strcpy(member.father, father);
strcpy(member.mother, mother);
return member;
}
// 添加子女
void add_child(FamilyMember *parent, char *child_name) {
int i;
for (i = 0; i < MAX_NAME_LEN; i++) {
if (parent->children[i][0] == '\0') {
strcpy(parent->children[i], child_name);
break;
}
}
}
int main() {
// 初始化家族成员
FamilyMember grandpa = init_member("Grandpa", 80, 'M', "Grandma", "", "");
FamilyMember grandma = init_member("Grandma", 78, 'F', "Grandpa", "", "");
FamilyMember father = init_member("Father", 50, 'M', "Mother", "Grandpa", "Grandma");
FamilyMember mother = init_member("Mother", 45, 'F', "Father", "", "");
FamilyMember son = init_member("Son", 25, 'M', "", "Father", "Mother");
FamilyMember daughter = init_member("Daughter", 20, 'F', "", "Father", "Mother");
// 添加子女
add_child(&father, "Son");
add_child(&father, "Daughter");
// 输出家族成员信息
printf("Grandpa: %s, Age: %d, Gender: %c, Spouse: %s, Father: %s, Mother: %s\n", grandpa.name, grandpa.age, grandpa.gender, grandpa.spouse, grandpa.father, grandpa.mother);
printf("Grandma: %s, Age: %d, Gender: %c, Spouse: %s, Father: %s, Mother: %s\n", grandma.name, grandma.age, grandma.gender, grandma.spouse, grandma.father, grandma.mother);
printf("Father: %s, Age: %d, Gender: %c, Spouse: %s, Father: %s, Mother: %s\n", father.name, father.age, father.gender, father.spouse, father.father, father.mother);
printf("Mother: %s, Age: %d, Gender: %c, Spouse: %s, Father: %s, Mother: %s\n", mother.name, mother.age, mother.gender, mother.spouse, mother.father, mother.mother);
printf("Son: %s, Age: %d, Gender: %c, Spouse: %s, Father: %s, Mother: %s, Children: %s, %s\n", son.name, son.age, son.gender, son.spouse, son.father, son.mother, son.children[0], son.children[1]);
printf("Daughter: %s, Age: %d, Gender: %c, Spouse: %s, Father: %s, Mother: %s, Children: %s\n", daughter.name, daughter.age, daughter.gender, daughter.spouse, daughter.father, daughter.mother, daughter.children[0]);
return 0;
}
```
输出结果如下:
```
Grandpa: Grandpa, Age: 80, Gender: M, Spouse: Grandma, Father: , Mother:
Grandma: Grandma, Age: 78, Gender: F, Spouse: Grandpa, Father: , Mother:
Father: Father, Age: 50, Gender: M, Spouse: Mother, Father: Grandpa, Mother: Grandma
Mother: Mother, Age: 45, Gender: F, Spouse: Father, Father: , Mother:
Son: Son, Age: 25, Gender: M, Spouse: , Father: Father, Mother: Mother, Children: Daughter
Daughter: Daughter, Age: 20, Gender: F, Spouse: , Father: Father, Mother: Mother, Children:
```
阅读全文