#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义家谱节点
typedef struct FamilyTreeNode {
char name[50];
int age;
char gender;
struct FamilyTreeNode *father;
struct FamilyTreeNode *leftChild;
struct FamilyTreeNode *rightSibling;
} FamilyTreeNode;
// 创建家谱节点
FamilyTreeNode *createFamilyTreeNode(const char *name, int age, char gender) {
FamilyTreeNode *newNode = (FamilyTreeNode *)malloc(sizeof(FamilyTreeNode));
strcpy(newNode->name, name);
newNode->age = age;
newNode->gender = gender;
newNode->father = NULL;
newNode->leftChild = NULL;
newNode->rightSibling = NULL;
return newNode;
}
// 创建家谱
FamilyTreeNode *createFamilyTree() {
printf("请输入家谱创建者的姓名:");
char creatorName[50];
scanf("%s", creatorName);
int creatorAge;
printf("请输入家谱创建者的年龄:");
scanf("%d", &creatorAge);
char creatorGender;
printf("请输入家谱创建者的性别(M/F):");
scanf(" %c", &creatorGender);
FamilyTreeNode *root = createFamilyTreeNode(creatorName, creatorAge, creatorGender);
return root;
}
// 查询成员信息
FamilyTreeNode *searchMember(FamilyTreeNode *root, const char *name) {
if (root == NULL) {
return NULL;
}
if (strcmp(root->name, name) == 0) {
return root;
}
FamilyTreeNode *leftResult = searchMember(root->leftChild, name);
if (leftResult != NULL) {
return leftResult;
}
FamilyTreeNode *rightResult = searchMember(root->rightSibling, name);
return rightResult;
}
// 添加孩子
void addChild(FamilyTreeNode *parent, const char *childName, int childAge, char childGender) {
FamilyTreeNode *child = createFamilyTreeNode(childName, childAge, childGender);
child->father = parent;
FamilyTreeNode = parent->leftchild;
if (parent->leftChild == NULL) {
parent->leftChild = child;
} else {
FamilyTreeNode *lastChild = parent->leftChild;
if (lastChild->rightSibling != NULL) {
lastChild = lastChild->rightSibling;
}
if (lastChild->rightSibling == NULL) {
lastChild->rightSibling = child;
} else {
lastChild = lastChild->rightSibling;
while (lastChild->rightSibling != NULL) {
lastChild = lastChild->rightSibling;
}
lastChild->rightSibling = child;
}
}
}
// 修改成员信息
void modifyMember(FamilyTreeNode *node, const char *newName, int newAge, char newGender) {
strcpy(node->name, newName);
node->age = newAge;
node->gender = newGender;
}
// 显示家谱
void displayFamilyTree(FamilyTreeNode *root, int generation) {
if (root == NULL) {
return;
}
printf("第%d代 成员:%s,年龄:%d,身份:%s\n", generation, root->name, root->age,
(root->gender == 'M') ? "男性" : "女性");
displayFamilyTree(root->leftChild, generation + 1);
displayFamilyTree(root->rightSibling, generation + 1);
}
// 主菜单
void displayMenu() {
printf("\n家谱管理系统\n");
printf("1. 创建家谱\n");
printf("2. 查询成员信息\n");
printf("3. 添加孩子\n");
printf("4. 修改成员信息\n");
printf("5. 显示家谱\n");
printf("0. 退出\n");
printf("请选择操作:");
}
int main() {
FamilyTreeNode *root = NULL;
while (1) {
displayMenu();
int choice;
scanf("%d", &choice);
switch (choice) {
case 1: {
root = createFamilyTree();
printf("家谱创建成功!\n");
break;
}
case 2: {
char name[50];
printf("请输入成员姓名:");
scanf("%s", name);
FamilyTreeNode *result = searchMember(root, name);
FamilyTreeNode *sibling = result->rightSibling;
if (result != NULL) {
printf("姓名:%s,年龄:%d,性别:%s\n", result->name, result->age,
(result->gender == 'M') ? "男性" : "女性");
if (result->father != NULL) {
printf("长辈:%s\n", result->father->name);
}
if (result->leftChild != NULL) {
printf("孩子:%s ", result->leftChild->name);
}
if (result->rightSibling != NULL) {
printf("同辈:%s ", result->rightSibling->name);
}
printf("\n");
} else {
printf("未找到成员 %s\n", name);
}
break;
}
case 3: {
char parentName[50], childName[50];
int childAge;
char childGender;
printf("请输入父亲姓名:");
scanf("%s", parentName);
FamilyTreeNode *parent = searchMember(root, parentName);
if (parent != NULL) {
printf("请输入孩子姓名:");
scanf("%s", childName);
printf("请输入孩子年龄:");
scanf("%d", &childAge);
printf("请输入孩子性别(M/F):");
scanf(" %c", &childGender);
addChild(parent, childName, childAge, childGender);
printf("添加成功!\n");
} else {
printf("未找到父亲 %s\n", parentName);
}
break;
}
case 4: {
char name[50], newName[50];
int newAge;
char newGender;
printf("请输入成员姓名:");
scanf("%s", name);
FamilyTreeNode *member = searchMember(root, name);
if (member != NULL) {
printf("请输入新姓名:");
scanf("%s", newName);
printf("请输入新年龄:");
scanf("%d", &newAge);
printf("请输入新性别(M/F):");
scanf(" %c", &newGender);
modifyMember(member, newName, newAge, newGender);
printf("修改成功!\n");
} else {
printf("未找到成员 %s\n", name);
}
break;
}
case 5: {
printf("\n家谱如下:\n");
displayFamilyTree(root, 1);
break;
}
case 0: {
printf("感谢使用家谱管理系统,再见!\n");
exit(0);
}
default:
printf("无效的选择,请重新输入。\n");
}
}
return 0;
}
这是代码,出了两个问题
一个是这个第几代人判断有问题,他判断某个节点的上一个节点为他的父亲,但是我把兄弟都放在右sibling指针上,导致他把兄弟也识别成父亲,每加一个人都加一代
一个是输出家谱的时候 他没法把那个同辈都输出完
求教怎么修改