在 C 中创建多个家谱

问题描述 投票:0回答:1

我的文本文件结构如下:

NAME son
MOTHER mother
FATHER father

NAME father
MOTHER grandmother 

NAME otherson
FATHER otherfather
MOTHER other mother

作为输出我应该得到这样的东西:

grandmother 
mother    --mother and son can be in different order
father
son

othermother
otherfather
otherson

任务是按照家庭中最年长的人的降序打印家谱。分开不同的家庭。人员描述始终以 NAME 开头。程序应该用 C 编写。输出可以在控制台中打印,也可以在新的文本文件中打印。

我尝试编写程序,从文件中获取输入并为每个人(包括母亲和父亲)创建节点。将所有节点放入数组中,现在思考下一步应该做什么。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE_LENGTH 63

typedef struct node
{
    char* name;
    struct node *mom; //left
    struct node *dad; //right
} t_node;

t_node* createNode(char* name) {
    t_node* newNode = malloc(sizeof(t_node));
    if (newNode == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        exit(1);
    }
    newNode->name = strdup(name); // Allocate memory for the name and copy it
    newNode->mom = NULL;
    newNode->dad = NULL;

    return newNode;
}

void freeMemory(t_node* nodeArray[], int numNodes){
    for (int i = 0; i < numNodes; i++) {
        free(nodeArray[i]->name);
        free(nodeArray[i]);
    }
}

void printNodes(t_node* nodeArray[], int numNodes){
    printf("Nodes created:\n");
    for (int i = 0; i < numNodes; i++) {
        printf("%s\n", nodeArray[i]->name);
    }
}

int main() {
    FILE *fin;
    char line[MAX_LINE_LENGTH];
    char* name;

    fin = fopen("input.txt", "r");
    if(fin == NULL) {
        printf("Error opening input file.\n");
        return 1;
    }

    t_node* nodeArray[10000]; // Array to store pointers to created nodes
    int numNodes = 0;

    while (fgets(line, MAX_LINE_LENGTH, fin) != NULL) {
        if (strstr(line, "VARDS")) {
            name = strstr(line, "NAME") + 6;
            name[strcspn(name, "\n")] = 0; // Remove newline character

            // Create a node for the person
            t_node* person = createNode(name);
            nodeArray[numNodes++] = person;
        } else if (strstr(line, "FATHER")) {
            char* father = strstr(line, "TEVS") + 5;
            father[strcspn(father, "\n")] = 0;
        
            // Create a node for the father if not already created
            int found = 0;
            for (int i = 0; i < numNodes; i++) {
                if (strcmp(nodeArray[i]->name, father) == 0) {
                    found = 1;
                    break;
                }
            }
            if (!found) {
                t_node* fatherNode = createNode(father);
                nodeArray[numNodes++] = fatherNode;
            }
        } else if (strstr(line, "MOTHER")) {
            char* mother = strstr(line, "MATE") + 5;
            mother[strcspn(mother, "\n")] = 0;

            // Create a node for the mother if not already created
            int found = 0;
            for (int i = 0; i < numNodes; i++) {
                if (strcmp(nodeArray[i]->name, mother) == 0) {
                    found = 1;
                    break;
                }
            }
            if (!found) {
                t_node* motherNode = createNode(mother);
                nodeArray[numNodes++] = motherNode;
            }
        }
        //one iteration through 1 person with mother and father
    }

    fclose(fin);

   printNodes(nodeArray, numNodes);
   freeMemory(nodeArray, numNodes);

    return 0;
}

我需要帮助来建立节点之间的关系。不知道该怎么做。 还需要以某种方式防止骑自行车(A是B的父亲,B是A的父亲),不要接受错误的输入,例如,如果一个人有2个母亲。

c data-structures
1个回答
0
投票

您要创建的是一个数据结构,其中每个节点都有两个前辈(两个父节点)。此外,一个节点有一个子节点列表(或一个数组)。 建立一个结构和“添加”功能,牢记这一点。

例如,您可以定义如下结构:

typedef struct node
{
    char* name;
    struct node *mom;
    struct node *dad;
    struct node **children; //Array of children
 } t_node;

这样,当你添加一个新节点时,你可以将其附加到其子节点或父节点,同时,你可以考虑按“级别”打印整个树。

清楚了吗?

© www.soinside.com 2019 - 2024. All rights reserved.