二叉树建立族谱

#include <iostream>
#include <cstring>
#include <string>
#include <Stdlib.h>
using namespace std;
 struct person//个人信息
{
    int n;
    string name;
    char sex;
    int birthday;
    string spouse;
    string introdution;
};
typedef struct tree//
{
public:
    person people;
    tree* brother;
    tree* children;
    tree* parent;

}tree,* treep;


treep head;
void createFamily(treep );
int  sunday(string src,string des);
void  searchbything(treep head);
treep addChild(treep ancestor,treep parent);
treep addbrother(treep newchild,treep parent);
void  print(treep head);
void  printall(treep head);
void  printChild(treep child);
void  printbrother(treep parent);
void  print_one_member(treep head );
void  addmenber(treep member);
treep FindNode(treep Node,string name);
void  change_information(treep head);
void  deletemmember(treep head);
treep Findintrodution(treep head,string tt);
int isexit(string b,char a);
void printintrodution(treep head,string str);
/
void printintrodution(treep head,string str)//按事迹查找,将关键字传入和节点传入,匹配字符串
{
        treep p=NULL;
if((p=Findintrodution(head,str))!=NULL)print(p);//找到,即输出该人的简介
        if(p->brother!=NULL)printintrodution(p->brother,str);//寻找兄弟节点
        if(p->children!=NULL)printintrodution(p->children,str);//寻找孩子节点
}
/
int isexit(string b,char a)//判断字符串是否匹配,匹配返回字符串的位置,否则返回-1
{
    int lenb=b.length();
    int i=lenb-1;
    while(i>=0)
    {
        if(b[i]==a)return lenb-i;//返回字符串移动的长度
        i--;
    }
    return -1;
}
/

treep Findintrodution(treep Node,string tt)//
{
    treep p=NULL;
    if(Node!=NULL)
    {
        if(sunday(Node->people.introdution,tt)!=-1)
        p=Node;
        else
       {
         p=Findintrodution(Node->children,tt);
         if(p!=NULL)return p;
         else
         p=Findintrodution(Node->brother,tt);
       }
    }
    return p;

}

/

void  searchbything(treep head)
{
    cout<<"       事迹查询        "<<endl;
    cout<<"请输入关键字"<<endl;
    string str;
cin>>str;
    cout<<"查找结果。。。。。"<<endl;
printintrodution(head,str);
}
/

int sunday(string a,string b)
{
    int lena=a.length();
    int lenb=b.length();
    int i=0;
    int start=0;
    int pos;
    if(lenb>lena||lenb==0||lena==0)return -1;
    while(start+lenb<=lena)
    {
        while(a[i+start]==b[i]&&i<lenb)
        {
         i++;
        }
        if(i==lenb){return start;break;}
        else
        {
            if(pos=isexit(b,a[lenb+start])!=-1)start+=pos;
            else start+=(lenb+1);
        }
    }
    return -1;
}
/
void  deletemmember(treep head)
{
    system("cls");
    cout<<"请输入要删除的成员的名称"<<endl;
    string name;
    cin>>name;
    treep p=FindNode(head,name);
    if(p==NULL)cout<<"未找到此人!"<<endl;
    else
    {
        p->people.name+="(已删除)";
        //for(int i=0;i<200;i++)
        p->people.introdution=" ";
        p->people.birthday=00000000;
        p->people.sex=' ';
        p->people.spouse=" ";
        cout<<"删除成功!"<<endl;
    }


}
/
void  change_information(treep head)
{
    system("cls");
    int num=1;
    cout<<"请输入要改变的成员的名称"<<endl;
    string name;
    cin>>name;
    treep p=FindNode(head,name);
    if(p==NULL){cout<<"未找到此人!"<<endl;system("cls");}
    else
    {
        while(num==1)
       {
        cout<<"1.修改该成员的名字"<<endl;
        cout<<"2.修改该成员的配偶"<<endl;
        cout<<"3.修改该成员的生辰"<<endl;
        cout<<"4.修改该成员的性别"<<endl;
        int n;
        cin>>n;
        switch(n)
        {
        case 1:
            {
                cout<<"请输入新的名称"<<endl;
                string newname;
                cin>>newname;
                p->people.name=newname;
                break;
            }
        case 2:
            {
                cout<<"请输入配偶的名称"<<endl;
                string spouse;
                cin>>spouse;
                p->people.spouse=spouse;
                break;
            }
        case 3:
            {
                 cout<<"请输入生日"<<endl;
                int birthday;
                cin>>birthday;
                p->people.birthday=birthday;
                break;
            }
        case 4:
            {
                cout<<"请输入性别(m为男,g为女)"<<endl;
                char sex;
                cin>>sex;
                p->people.sex=sex;
                break;
            }
        }
        cout<<"修改成功,继续请按1,退出请按0"<<endl;
        cin>>num;
       }
        system("cls");
    }
}
/
treep FindNode(treep Node,string name)
{
    treep p=NULL;
    if(Node!=NULL)
    {
        if(Node->people.name==name)p=Node;
        else
       {
         p=FindNode(Node->children,name);
         if(p!=NULL)return p;
         else
         p=FindNode(Node->brother,name);
       }
    }
    return p;
}
/
void  addmember(treep head)
{
    treep member=new tree;
    treep p=new tree;
    cout<<"请输入该成员的父亲名称"<<endl;
    string name;
    cin>>name;
    p=FindNode(head,name);
    if(p==NULL){cout<<"未找到此人!"<<endl;system("cls");}
    else
    {
        cout<<"请输入该成员的名称"<<endl;
        string newname;
        cin>>newname;
        member->people.name=newname;
        cout<<"请输入"<<member->people.name<<"的性别(m为男,g为女)"<<endl;
        char sex;
        cin>>sex;
        member->people.sex=sex;
        cout<<"请输入"<<member->people.name<<"的生日"<<endl;
        int birthday;
        cin>>birthday;
        member->people.birthday=birthday;
        cout<<member->people.name<<"是否已婚(yes or no)"<<endl;
        string pan;
        cin>>pan;
        if(pan=="yes")
        {
            cout<<"请输入配偶的名称"<<endl;
            string spouse_name;
            cin>>spouse_name;
            member->people.spouse=spouse_name;
        }
        cout<<"请输入"<<member->people.name<<"的简介"<<endl;
        string introdution;


        cin>>introdution;


        int length=introdution.length();


        member->people.introdution=introdution;


        member->people.n=p->people.n+1;
        member->children=NULL;
        member->brother=NULL;
        member->parent=p;
        if(p->children==NULL)p->children=member;
        else
        {
         p=p->children;
         if(p->brother==NULL)p->brother=member;
         else
         {
             while(p->brother!=NULL)
             {p=p->brother;}
              p->brother=member;
         }


       }
    }
    cout<<"添加成功!"<<endl;
}
/
void mainmenu(treep head)
{
    cout<<"                族谱程序                 "<<endl;
    cout<<"             1.创建族谱                  "<<endl;
    cout<<"             2.删除家族成员              "<<endl;
    cout<<"             3.输出全部家族成员          "<<endl;
    cout<<"             4.修改家族成员信息          "<<endl;
    cout<<"             5.增加家族成员              "<<endl;
    cout<<"             6.输出某一个家族成员的信息  "<<endl;
    cout<<"             7.按照事迹查询家族成员      "<<endl;
    cout<<"             8.退出系统                  "<<endl;
    cout<<"                                         "<<endl;
    int n;
    cin>>n;
    switch(n)
    {
        case 1:system("cls");createFamily(head);break;
        case 2:
            {
            system("cls");
            deletemmember(head);
            int n;
            cout<<"输入0退出"<<endl;
            cin>>n;
            if(n==0)system("cls");
            break;
            }
        case 3:
            {
                system("cls");
                printall(head);
                int n;
                cout<<"输入0退出"<<endl;
                cin>>n;
                if(n==0)system("cls");
                break;
            }
        case 4:
            {
                system("cls");
                change_information(head);
                break;
            }
        case 5:
            {
                system("cls");
                addmember(head);
                int n;
                cout<<"输入0退出"<<endl;
                cin>>n;
                if(n==0)system("cls");
                break;
            }
        case 6:
            {
                system("cls");
                print_one_member(head);
                //system("cls");
                int n;
                cout<<"输入0退出"<<endl;
                cin>>n;
                if(n==0)system("cls");
                break;
            }
        case 7:
            {
              system("cls");
              searchbything(head);
              int n;
              cout<<"输入0退出"<<endl;
              cin>>n;
              if(n==0)system("cls");
              break;
            }
        case 8:exit(0);
    }
}
/
treep init()
{
   treep ancestor;
   ancestor=new tree;
   ancestor->people.n=1;
   cout<<"                                   请输入祖先的信息"<<endl<<endl;
   cout<<"请输入祖先的名字"<<endl;
   cin>>ancestor->people.name;
   cout<<"请输入祖先的生辰(格式为19940517)"<<endl;
   cin>>ancestor->people.birthday;
   cout<<"请输入祖先的性别(m为男,g为女)"<<endl;
   cin>>ancestor->people.sex;
   cout<<"请输入祖先的配偶"<<endl;
   cin>>ancestor->people.spouse;
   cout<<"请输入祖先的简介(不得超过200字)"<<endl;
   cin>>ancestor->people.introdution;
   ancestor->brother=NULL;
   ancestor->children=NULL;
   ancestor->parent=NULL;
   system("cls");
   return ancestor;
}

void createFamily(treep ancestor)//创建族谱
{
    cout<<"给"<<ancestor->people.name<<"添加后代"<<endl;
    ancestor->children=addChild(ancestor->children,ancestor);//调用添加子代的函数
}
/

treep addbrother(treep brother,treep newchild) 

//添加兄弟函数

{
        brother=new tree;
brother->parent=newchild->parent;//将传入的的
brother->people.n=newchild->people.n;
string name;
string pan1;
string pan2;
string pan3;
cout<<"请输入"<<newchild->people.name<<"弟弟或妹妹的名字"<<endl;
        cin>>brother->people.name;


        cout<<"请输入"<<brother->people.name<<"的出生年月"<<endl;
cin>>brother->people.birthday;


cout<<"请输入"<<brother->people.name<<"的性别(m为男,g为女)"<<endl;
cin>>brother->people.sex;


cout<<"请输入"<<brother->people.name<<"的简介"<<endl;
cin>>brother->people.introdution;


cout<<brother->people.name<<"是否有配偶(yes or no)"<<endl;
cin>>pan1;
if(pan1=="yes")
    {
        cin>>brother->people.spouse;
        cout<<brother->people.name<<"是否有子女(yes or no)"<<endl;
        cin>>pan2;
  if(pan2=="yes")brother->children=addChild(brother->children,brother);
  else if(pan2=="no")brother->children=NULL;
    }
if(pan1=="no")
    {
        brother->people.spouse="无";
        brother->children=NULL;
    }
cout<<"是否继续添加弟弟或妹妹(yes or no)"<<endl;
    cin>>pan3;
if(pan3=="yes")brother->brother=addbrother(brother->brother,brother);
else
    if(pan3=="no")brother->brother=NULL;
return brother;
}
//
treep addChild(treep newchild,treep parent)
{
    string pan;
    string pan1;
    string pan2;
    string spouse_name;
    newchild =new tree;
    newchild->parent=parent;
newchild->people.n=parent->people.n+1;


    cout<<"请输入"<<parent->people.name<<"的孩子的名字"<<endl;
    cin>>newchild->people.name;


    cout<<"请输入"<<newchild->people.name<<"的出生年月"<<endl;
    cin>>newchild->people.birthday;


    cout<<"请输入"<<newchild->people.name<<"的性别(m为男,g为女)"<<endl;
    cin>>newchild->people.sex;


    cout<<"请输入"<<newchild->people.name<<"的简介"<<endl;
    cin>>newchild->people.introdution;


    cout<<newchild->people.name<<"是否有配偶(yes or no)"<<endl;
    cin>>pan;
    if(pan=="yes")
    {
        cout<<"请输入配偶名称"<<endl;
        cin>>spouse_name;
        cout<<newchild->people.name<<"是否有孩子(yes or no)"<<endl;
        cin>>pan1;
        if(pan1=="yes")
         {
          system("cls");
          createFamily(newchild);
          }
        else newchild->children=NULL;
    }
    if(pan=="no")
    {
        newchild->people.spouse="无";
        newchild->children=NULL;
    }
    cout<<newchild->people.name<<"是否有兄弟(yes or no)"<<endl;
    cin>>pan2;
    if(pan2=="yes")
    {
      newchild->brother=addbrother(newchild->brother,newchild);
    }
    if(pan2=="no")newchild->brother=NULL;


    system("cls");
    return newchild;
}

void print_one_member(treep head)
{
      system("cls");
      cout<<"请输入该人的名字"<<endl;
      string name;
      cin>>name;
     treep p;
     p=FindNode(head,name);
     if(p==NULL)cout<<"未找到此人!"<<endl;
     else
     print(p);


}

void printChild(treep child)
{
    cout<<child->people.name<<"  ";
    if(child->brother!=NULL)
    printChild(child->brother);
    cout<<endl;
}
/
void printbrother(treep parent)
{
    cout<<parent->children->people.name<<"   ";
    printChild(parent->children);
}
/
void print(treep head)
{
   cout<<"——————————————————"<<endl;
   cout<<"姓名: "<<head->people.name<<endl;


   cout<<"辈分:  第"<<head->people.n<<"代"<<endl;


   cout<<"性别:   ";
   if(head->people.sex=='m')cout<<"男"<<endl;
   if(head->people.sex=='g')cout<<"女"<<endl;
   cout<<endl;
   cout<<"生日:  "<<head->people.birthday<<endl;
   if(head!=NULL&&head->parent!=NULL)


   cout<<"父母:  "<<head->parent->people.name<<' '<<head->parent->people.spouse <<endl;


   else
   cout <<"父母: "<<endl;
   if(head->people.spouse=="无")
   cout<<"配偶:    未婚"<<endl;


   if(head->people.spouse!="无")
   cout<<"配偶:  "<<head->people.spouse<<endl;


   if(head->children!=NULL)
   {
       cout<<"孩子:  ";
       printChild(head->children);
   }
   if(head->children==NULL)
       cout<<"孩子:   无孩子"<<endl;


   if(head->brother==NULL)
   cout<<"兄弟:  "<<"无兄弟"<<endl;


   if(head->brother!=NULL)
   {
       cout<<"兄弟:  "<<head->brother->people.name<<endl;
       printbrother(head->parent);
   }
   cout<<"简介:"<<head->people.introdution<<endl;
   cout<<"——————————————————"<<endl;
}
/
void printall(treep head)
{
    if(head!=NULL)print(head);
    if(head->brother!=NULL)printall(head->brother);
    if(head->children!=NULL)printall(head->children);
}
/
int main()
{
    head=init();
    while(1)
    {
        mainmenu(head);
    }


    return 0;

}

(1).首先初始化族谱,即建立一个头结点

(2).选择创建族谱,即用二叉树建立链表,添加子节点。

(3).采用先序遍历,各种操作基本都是在遍历到某一节点再进行操作

(4).按事迹查找使用关键字搜索,即遍历各节点的简介,将关键字与简介匹配。字符串匹配采用Sunday算法,简洁明了。

(5).程序有BUG,在输出兄弟时会重复输出,未来得及优化

  • 5
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
家谱管理系统,主要用来管理家族成员的基本信息 1、确定整个程序的功能模块。实现程序的主界面,要对主界面的功能选择输入进行容错处理。 2、实现单个结点信息的录入。 3、对录入日期信息进行合法性检验。 4、采用改变字体颜色的方式突出显示主界面的功能项。 5、计算从出生日期到死亡日期的实际天数 6、若家谱树为空,则新建家谱树。实现成员节点的添加。基本功能中可以 强制要求所有成员不同名,即不考虑同名情况(符合小家族的实际情况)。 7、添加成员节点时,可以选择将新添加的节点作为整个家谱的上一代祖先, 或者将新添加的节点作为某个现有成员的孩子。 8、作为某个现有成员的孩子,根据给出的父节点的姓名将该结点添加到相 应位置,注意,针对某一父节点,添加第一个孩子和其它孩子的区别。 9、要求在孩子兄弟二叉树中按各个孩子的年龄进行排序。 10、将家谱树保存到二进制文件。注意,不能保存空白节点。 11、从文件读入家谱信息,重建孩子兄弟二叉树形式的家谱。 12.从文件中读出所有节点信息到一个数组中,然后按一年中生日的先后进 行快速排序。 13、按姓名查询家谱成员并显示该成员的各项信息。 14、给出某一成员的姓名,删除该成员和该成员的所有子孙。 15、成员信息的修改。信息修改时要给出选择界面让用户选择需要修改的信 息项。基本功能中可以限定不容许修改父亲姓名和本人姓名。对日期信 息进行修改时要进行检验。 16、实现层次递进的方式显示整个家谱,显示结果应该体现家谱树的结构。 17、按各种关键字进行查询,要求给出关键字选择界面,并显示符合查询条 件的节点信息。 18、信息统计基本要求包括:平均身高,平均寿命,男女成员各多少,平均 家庭人口数目(假定每个成员构成一个家庭,该家庭的家庭成员是指成 员本人和他的孩子,即家庭人口数=孩子数+1)。要给出统计项的选择界 面. 19、查询某一成员的所有直系亲属。 20、给出某一成员的所有嫡系祖先。 21、确定两人关系。若两人辈分不等,则应指出甲是乙的多少代长辈(晚辈), 甲是否是乙的直系长辈(晚辈),若辈分相同,则应指出是亲兄弟还是多 少代的堂兄弟。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值