C语言课程设计——家谱

本文介绍了使用C语言进行课程设计,重点在于实现一个家谱管理的程序。通过读取family.txt文件,该系统能够存储和展示家庭成员的结构和信息。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
#define MAXSIZE 100
typedef struct Infomation{
   
    char name[20];
    char birth[20];
    int wedding;
    char add[20];
    int health;
    char death_date[20];
}Info;
typedef struct node
{
   
    Info person;
    struct node *lchild,*rchild;
}Bnode,*tree;
typedef struct mode
{
   
    tree queue[MAXSIZE];
    int front,rear;
}Queue;
void init(Queue Q)
{
   
   Q.front=Q.rear=0;
}

void push(Queue *Q,tree e)//入队
{
   
    Q->rear=(Q->rear+1)%MAXSIZE;
    Q->queue[Q->front]=e;
}

void pop(Queue *Q,tree *e)//出队
{
   
    Q->front=(Q->front+1)%MAXSIZE;
    *e=Q->queue[Q->front];
}

int empty(Queue Q)//判队空
{
   
    if(Q.front==Q.rear)
    return 1;
    else return 0;
}
int full(Queue Q)//判队满
{
   
    if((Q.rear+1)%MAXSIZE==Q.front)
    return 1;
    else return 0;
}
void newleft(tree p,Info L)
{
   
    tree q;
    q=(tree)malloc(sizeof(Bnode));
    q->person=L;
    q->lchild=q->rchild=NULL;
    p->lchild=q;
}

void newright(tree p,Info L)
{
   
    tree q;
    q=(tree)malloc(sizeof(Bnode));
    q->person=L;
    q->lchild=q->rchild=NULL;
    p->rchild=q;
}

tree creat()
{
   
    FILE *fp;
    int i;
    Info human[11];
    if ((fp=fopen("family.txt","r+"))==NULL)
	{
   
		printf("不能打开家谱文件\n");
	}
	for(i=0;i<11;i++)
	{
   
	    fscanf(fp,"%s %s %d %s %d",human[i].name,human[i].birth,&human[i].wedding,human[i].add,&human[i].health);
	    fgets(human[i].death_date,20,fp);
	    printf("%-7s%-15s%-7d%-12s%-10d%-20s\n",human[i].name,human[i].birth,human[i].wedding,human[i].add,human[i].health,human[i].death_date);
	}
	fclose(fp);
    tree bt;
    bt=(tree)malloc(sizeof(Bnode));
    bt->person=human[0];
    bt->lchild=bt->rchild=NULL;

    newleft(bt,human[1]);
    newright(bt,human[2]);
    newleft(bt->lchild,human[3]);
    newright(bt->lchild,human[4]);
    newleft(bt->rchild,human[5]);
    newright(bt->rchild,human[6]);
    newleft(bt->lchild->lchild,human[7]);
    newright(bt->lchild->lchild,human[8]);
    newleft(bt->rchild->rchild,human[9]);
    newright(bt->rchild->rchild->lchild,human[10]);
    return bt;
}

void output(tree p)
{
   

  printf("%-7s%-15s%-7d%-12s%-10d%-20s\n",p->person.name,p->person.birth,p->person.wedding,p->person.add,p->person
  • 9
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
家谱管理系统可以使用树形结构进行存储和管理家谱信息。在C语言中,可以使用结构体来定义家谱节点,结构体的成员可以包括该节点的姓名、性别、出生日期、父亲节点和子节点等信息。具体实现可以参考以下代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_LEN 20 // 家谱节点结构体 typedef struct family_tree_node { char name[MAX_NAME_LEN]; // 姓名 char gender; // 性别 char birthday[11]; // 出生日期,格式为yyyy-mm-dd struct family_tree_node *father; // 父亲节点 struct family_tree_node *child; // 子节点 struct family_tree_node *sibling; // 兄弟节点 } FamilyTreeNode; // 创建家谱节点 FamilyTreeNode *createFamilyTreeNode(char *name, char gender, char *birthday) { FamilyTreeNode *node = (FamilyTreeNode *)malloc(sizeof(FamilyTreeNode)); if (node == NULL) { printf("Error: createFamilyTreeNode failed, out of memory.\n"); return NULL; } strcpy(node->name, name); node->gender = gender; strcpy(node->birthday, birthday); node->father = NULL; node->child = NULL; node->sibling = NULL; return node; } // 添加子节点 void addChild(FamilyTreeNode *parent, FamilyTreeNode *child) { if (parent == NULL || child == NULL) { printf("Error: addChild failed, invalid arguments.\n"); return; } if (parent->child == NULL) { parent->child = child; } else { FamilyTreeNode *sibling = parent->child; while (sibling->sibling != NULL) { sibling = sibling->sibling; } sibling->sibling = child; } child->father = parent; } // 输出家谱信息 void printFamilyTree(FamilyTreeNode *root) { if (root == NULL) { return; } printf("%s %c %s\n", root->name, root->gender, root->birthday); FamilyTreeNode *child = root->child; while (child != NULL) { printFamilyTree(child); child = child->sibling; } } int main() { // 创建家谱 FamilyTreeNode *root = createFamilyTreeNode("张三", 'M', "1980-01-01"); FamilyTreeNode *child1 = createFamilyTreeNode("张四", 'M', "2000-01-01"); FamilyTreeNode *child2 = createFamilyTreeNode("张五", 'F', "2002-01-01"); addChild(root, child1); addChild(root, child2); FamilyTreeNode *grandChild1 = createFamilyTreeNode("张六", 'M', "2020-01-01"); addChild(child1, grandChild1); FamilyTreeNode *grandChild2 = createFamilyTreeNode("张七", 'F', "2022-01-01"); addChild(child1, grandChild2); // 输出家谱信息 printFamilyTree(root); // 释放内存 free(grandChild2); free(grandChild1); free(child2); free(child1); free(root); return 0; } ``` 在上面的代码中,我们定义了一个FamilyTreeNode结构体来表示家谱节点,其中包含姓名、性别、出生日期、父亲节点和子节点等信息。使用createFamilyTreeNode函数可以创建一个家谱节点,使用addChild函数可以将一个节点添加到另一个节点的子节点列表中。最后,使用printFamilyTree函数可以输出整个家谱的信息。需要注意的是,释放内存的操作也需要在程序结束时进行,以避免内存泄漏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值