数据结构课设之家谱管理系统

Copyright© 2017,烟台大学计算机与控制工程学院
All rights reserved.
文件名称:text.cpp
作者:黄某某
完成日期:2017年12月21日
版本:vc6.0
问题描述: 实现简单的族谱系统
d.h

#ifndef FAMILY_H_INCLUDED
#define FAMILY_H_INCLUDED
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#define MAX 10
typedef struct node//定义data存储结构,存放个人信息
{       char name[MAX]; //姓名
        char sex;//性别
        int generation;//代
}node;
typedef node ElemType; //将ElemType定义为结构体类型
typedef struct treenode//创建结构体
{
        ElemType l; //家谱中直系家属
        struct treenode *brother;//用来指向兄弟
        struct treenode *child;//用来指向孩子
}treenode;
 treenode *search(treenode *p,char ch[]);     // 搜索指针函数,搜索需要修改和获得的结点,输入头指针,姓名
 int generation(treenode *p,char ch[]); //获得搜索到的成员的代的返回值
 int generation1(treenode *p,char ch[]); //获得搜索到的成员的代的返回值
 void children(treenode *p,char b[],char c,int d);//建立家谱孩子结点,创建结点并对l赋值保存
 void output(treenode *n); //搜索到数据的输出
 void InitTree(); //初始化(创建)
 void Add(); //添加
 void Search(); //查找
 void Change(); //修改
#endif // FAMILY_H_INCLUDED
/***********************************************************
*版权所有(C)2017,
*
*文件名称:d.h
*文件标识:无
*内容摘要:定义结构体声明函数
*其它说明:无
*当前版本:V1.0
*作 者:  黄潇慧
*完成日期:2017.12.21
*
*版本号:V1.0
**********************************************************/

d.cpp

#include "d.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
void display0();
void Search1();
using namespace std;

/*********************************************************
*功能描述:容错的检验
*输入参数:x
*输出参数:无
*返回值:x-成功其他-失败
*其它说明:消息字段之间用分号(;)分隔
***********************************************************/
template<class T>//模板
T fun(T x)
{
    if(x!=2)
        throw x;
    else
        return x;
}
treenode *root;              //root是指向结构体treenode的指针



/*********************************************************
*功能描述:查找search家谱中某位成员
*输入参数: *p 头指针用来指向根节点(root),保证从根节点root进行遍历, ch[]用来储存姓名
*输出参数: 无
*返回值:q or NULL(返回指针)
*其它说明:消息字段之间用分号(;)分隔
            通过递归在兄弟、孩子中查找
/***********************************************************/
treenode *search(treenode *p,char ch[]) // 搜索指针函数,搜索需要修改和获得的结点,输入头指针,姓名ch[]
{                                        //用search寻找性别姓名信息
    treenode *q;
    if(p==NULL) return NULL;//没有家谱,头指针下为空
    if(strcmpi(p->l.name,ch)==0)//比较姓名,看是否重名或是否存在
        return p;//家谱不为空,头指针下有这个人
    if(p->brother)
    {
        //递归过程
        q=search(p->brother,ch);//在兄弟中找
        if(q)
            return q;//找到
    }
    if(p->child)
    {
        q=search(p->child,ch);//在孩子中找
        if(q!=NULL)
            return q; //找到
    }
    return NULL;//没有找到
}



/*********************************************************
*功能描述:获得搜索到的成员的第几代的返回值
*输入参数: *p 头指针用来指向root的根节点,ch[]用来储存姓名
*输出参数:被查找人的信息
*返回值:q->l.generation or NULL or 0
*其它说明:消息字段之间用分号(;)分隔
            通过递归在兄弟、孩子中查找
***********************************************************/
int generation(treenode *p,char ch[]) //获得搜索到的成员的代的返回值
{
    treenode *q;
    if(p==NULL)
        return 0;
    if(strcmpi(p->l.name,ch)==0) //比较姓名,看是否重名或是否存在  根据名字进行遍历
        return p->l.generation;//家谱不为空,头指针下有这个人
    if(p->brother)
    {
        q=search(p->brother,ch);//在兄弟中找
        if(q)
            return q->l.generation;//找到
    }
    if(p->child)
    {
        q=search(p->child,ch);//在孩子中找
        if(q!=NULL)
            return q->l.generation; //找到
    }
    return 0;
}


/*********************************************************
*功能描述:建立家谱孩子结点,创建结点并对l赋值保存
*输入参数: *p 头指针用来指向节点  b[] 孩子名字 c 性别  第几代d
*输出参数:无
*其它说明:消息字段之间用分号(;)分隔
***********************************************************/

void children(treenode *p,char b[],char c,int d)//建立家谱孩子结点,创建结点并对l赋值保存
{

    int i;
    for(i=0; i<MAX; i++)
        p->l.name[i]=b[i];
    p->l.sex=c;
    p->l.generation=d;
}


/*********************************************************
*功能描述:输出成员信息
*输入参数: *n 次节点用于储存该成员的信息
*输出参数:l.name 姓名 l.sex 性别 l.generation 第几代
*其它说明:消息字段之间用分号(;)分隔

***********************************************************/
void output(treenode *n) //搜索到数据的输出
{
    treenode *t=NULL;
    printf("此人姓名:%s 性别%c 为第%d代\n",n->l.name,n->l.sex,n->l.generation);
    printf("\n");
    printf("此人的子女:"); //子女输出
    if(n->child==NULL)
    {
        printf("此人无子女!");
    }
    else
    {
        if(n->child->brother==NULL)
        {
            printf("姓名:%s 性别:%c\t",n->child->l.name,n->child->l.sex);
        }
        else
        {
            printf("姓名:%s 性别:%c\t",n->child->l.name,n->child->l.sex);
            t=n->child->brother;
            while(t!=NULL)
            {
                printf("姓名:%s 性别:%c\t",t->l.name,t->l.sex);
                t=t->brother;
            }
        }
    }
    printf("\n");
    printf("此人的同辈成员:"); //同辈输出
    if(n->brother==NULL)
    {
        printf("此人无同辈成员!");
    }
    else
    {
        if(n->brother->brother==NULL)
        {
            printf("姓名:%s 性别:%c\t",n->brother->l.name,n->brother->l.sex);
        }
        else
        {
            printf("姓名:%s 性别:%c\t",n->brother->l.name,n->brother->l.sex);
            t=n->brother->brother;
            while(t!=NULL)
            {
                printf("姓名:%s 性别:%c\t",t->l.name,t->l.sex);
                t=t->brother;
            }

        }

    }
}



/******************************************************************
*功能描述:初始化家谱
*创建内存空间储存节点
*输入:输入家谱始代人的信息即祖先节点root,并为root开辟一块内存
*其它说明:消息字段之间用分号(;)分隔,判断输入性别正误的容错功能
********************************************************************/
void InitTree() //初始化(创建)
{
    system("cls");
    system("color 3F");
    cout<<endl<<endl<<endl<<endl;
    cout<<"                   ********************************************"<<endl;
    cout<<"                   *                                          *"<<endl;
    cout<<"                   *           现在开始初始化家族族谱         *"<<endl;
    cout<<"                   *                                          *"<<endl;
    cout<<"                   ********************************************"<<endl;
    cout<<endl<<endl<<endl;
    int a;
    char b[MAX],c;
    printf(" 请输入始祖的姓名性别:");
    free(root);//释放root (ft)空间
    root=(treenode *)malloc(sizeof(treenode)); // 创建一个treenode结构体大小的空间
    //然后强制转换为treenode *类型的指针然后赋值给root
    scanf("%s %c",&b,&c);//输入姓名,性别

    while(c!='m'&&c!='f')
    {
        try
        {
            fun(c);
        }
        catch(...)
        {
            cout<<"输入性别有误!请重新输入!(女士f男士m):"<<endl;
        }
        scanf("%s %c",&b,&c);//输入姓名,性别
    }



    a=1;//定义该祖先为第一代人
    root->child=NULL; //清空左右孩子
    root->brother=NULL;
    children(root,b,c,a);//存入结构
    printf("家谱初始化成功!\n");

    /*printf("\n按任意键返回主菜单..\n");
    getchar();*/
    system("pause");
    system("cls");
    display0();
}


/***************************************************************************************
*功能描述:添加成员信息
*输入参数:成员的信息    b[]子辈姓名   c性别    d[]父辈姓名
*其它说明:消息字段之间用分号(;)分隔
            先用search()进行递归判断输入姓名是否重复,找到要输入结点的parent,判断插入的位置
            
*******************************************************************************************/
void Add() //添加
{
    system("cls");
    system("color 3F");
    int a;
    cout<<endl<<endl<<endl<<endl;
    cout<<"                   ********************************************"<<endl;
    cout<<"                   *                                          *"<<endl;
    cout<<"                   *          现在开始添加家庭成员操作        *"<<endl;
    cout<<"                   *                                          *"<<endl;
    cout<<"                   ********************************************"<<endl;
    cout<<endl<<endl<<endl;
    treenode *n,*m,*t=NULL;
    char b[MAX],c,d[MAX];
    printf("请输入要添加子女的上一辈的姓名:");
    scanf("%s",&d);
    n=search(root,d);   //找到d的data域
    a=generation(root,d);
    while(n==NULL) //判断是否有重名
    {
        printf("此人不在家谱内,请重新输入姓名:");
        scanf("%s",&d);
        n=search(root,d);
    }
    if(n->child==NULL)//孩子信息添加,当满足该父辈无子女时,进入条件判断
    {
        printf(" 请输入所添加家庭成员的姓名与性别(注意不能重名):");
        scanf("%s %c",&b,&c);
        while(c!='m'&&c!='f')
        {
            try
            {
                fun(c);
            }
            catch(...)
            {
                cout<<"输入性别有误!请重新输入!(女士f男士m):"<<endl;
            }
            scanf("%s %c",&b,&c);//输入姓名,性别
        }
        a++;//此处a代表孩子的辈分
        m=search(root,b);//搜索族谱之中是否有与新添加成员重名的人,若有则添加失败
        if(m!=NULL)
        {
            printf("出现重名,添加失败!\n");

        }
        else
        {
            n->child=(treenode *)malloc(sizeof(treenode));
            n->child->brother=NULL;
            n->child->child=NULL;
            children(n->child,b,c,a);
            printf("添加成功!\n");
        }
    }
    else//当该父辈之前已经有子女的时候,则此新添加的成员为添加的前一个孩子的兄弟
    {
        n=n->child;
        while(n->brother!=NULL) //添加另一个孩子
            n=n->brother;
        printf("请输入所添加家庭成员的姓名与性别(注意不能重名):");
        scanf("%s %c",&b,&c);
        while(c!='m'&&c!='f')
        {
            try
            {
                fun(c);
            }
            catch(...)
            {
                cout<<"输入性别有误!请重新输入!(女士f男士m):"<<endl;
            }
            scanf("%s %c",&b,&c);//输入姓名,性别
        }
        a++;
        m=search(root,b);
        if(m!=NULL)
            printf("出现重名,添加失败!\n");
        else
        {
            t=(treenode *)malloc(sizeof(treenode));
            children(t,b,c,a);
            t->brother=NULL;
            t->child=NULL;
            n->brother=t;
            printf("添加成功!\n");
        }
    }
    printf("\n按任意键返回主菜单..\n");
    getch();
    system("cls");
    display0();

}


/*********************************************************
*功能描述:查找功能的实现
*输入参数:d[] 姓名
*输出参数:输出被查找人的信息
*其它说明:消息字段之间用分号(;)分隔
            search()函数与output()函数并用,通过search()函数查找结点位置,通过output()输出结点内容
***********************************************************/
void Search() //查找
{
    treenode *n;
    char d[MAX];
    printf("输入姓名,查找相关信息:");
    scanf("%s",&d);
    n=search(root,d);
    while(n==NULL)
    {
        printf("此人不存在,请再次输入:");
        scanf("%s",&d);
        n=search(root,d);
    }
    output(n);
    printf("\n");
    printf("\n按任意键返回主菜单..\n");
    getch();
    system("cls");
    display0();
}


/*********************************************************
*功能描述:修改成员信息
*输入参数: 要修改人的姓名(判断此人是否存在)
*输出参数:存在则输出此人信息,不存在则重新出入
*
*其它说明:消息字段之间用分号(;)分隔

***********************************************************/
void Change() //修改
{
    char a[MAX],r[MAX],c;
    treenode *n;
    int i;
    printf("请输入要修改人的姓名:");
    scanf("%s",&a);
    n=search(root,a);   //对姓名进行遍历
    while(n==NULL)
    {
        printf("此人不存在,请重新输入姓名:");
        scanf("%s",&a);
        n=search(root,a);     //遍历
    }
    printf("此人存在,请输入新信息:");
    scanf("%s %c",&r,&c);
    while(c!='m'&&c!='f')   //容错
    {
        try
        {
            fun(c);
        }
        catch(...)
        {
            cout<<"输入性别有误!请重新输入!(女士f男士m):"<<endl;
        }
        scanf("%s %c",&r,&c);//输入姓名,性别
    }
    for(i=0; i<MAX; i++)
        n->l.name[i]=r[i];
    n->l.sex=c;
    printf("修改成功!\n");
    printf("\n按任意键返回主菜单..\n");
    getch();
    system("cls");
    display0();
}



/*********************************************************
*功能描述:功能栏
*输入参数: 
*输出参数:
*其它说明:消息字段之间用分号(;)分隔

***********************************************************/
void display0()//最初显示界面
{
    system("cls");
    system("color 3F");
    cout<<endl;
    cout<<" ---------------------------------------------------------------------"<<endl;
    cout<<"        **     **   *********    **          **              ***      "<<endl;
    cout<<"        **     **   *********    **          **            **   **    "<<endl;
    cout<<"        **     **   **           **          **           **     **   "<<endl;
    cout<<"        **     **   **           **          **          **       **  "<<endl;
    cout<<"        *********   *********    **          **          **       **  "<<endl;
    cout<<"        *********   *********    **          **          **       **  "<<endl;
    cout<<"        **     **   **           **          **          **       **  "<<endl;
    cout<<"        **     **   **           **          **           **     **   "<<endl;
    cout<<"        **     **   *********    *********   *********     **   **    "<<endl;
    cout<<"        **     **   *********    *********   *********       ***      "<<endl;
    cout<<" ---------------------------------------------------------------------"<<endl;


    cout<<endl;
    cout<<"                     **************************************"<<endl;
    cout<<"                     *                                    *"<<endl;
    cout<<"                     *         欢迎进入家谱管理系统       *"<<endl;
    cout<<"                     *                                    *"<<endl;
    cout<<"                     **************************************"<<endl;
    cout<<endl<<endl;
    cout<<"                                   1 添加成员"<<endl<<endl;
    cout<<"                                   2 查询个人信息"<<endl<<endl;
    cout<<"                                   3 修改信息"<<endl<<endl;
    cout<<"                                   4 创建族谱"<<endl<<endl;
    cout<<"                                   5 退出"<<endl<<endl;
    cout<<endl;
    int a;
    cout<<"                                 请输入数字:";
    cin>>a;
    while(!cin)
    {
        cout<<"                                 输入错误,请重新输入:";
        cin.sync();
        cin.clear();
        cin>>a;
    }
    while(a<1||a>5)
    {
        try
        {
            fun(a);
        }
        catch(...)treenode *root;  
        {
            cout<<"                                 输入错误,请重新输入:";
        }
        cin>>a;
    }
    switch(a)
    {

    case 1:
        Add();
        system("pause");
        break;

    case 2:
        Search();
        system("pause");
        break;
    case 3:
        Change();            
    case 4:
        InitTree();
        system("pause");
        break;
    case 5:
        exit(0);
        break;
    }
}



main.cpp

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include "d.h"
void display0();
using namespace std;
int main()
{

    display0();
    return 0;

}


  • 20
    点赞
  • 194
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码。软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码。软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码。软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码。软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码软件工程数据库管理与信息系统课程设计家族族谱管理系统源代码。软件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值