1. 程式人生 > >資料結構課程實驗->族譜

資料結構課程實驗->族譜

  族譜管理
    族譜(或稱家譜)是一種以表譜形式,記載一個以血緣關係為主體的家族世系繁衍和重要人物事蹟的特殊圖書體裁。族譜是中國特有的文化遺產,是中華民族的三大文獻(國史,地誌,族譜)之一,屬珍貴的人文資料,對於歷史學、民俗學、人口學、社會學和經濟學的深入研究,均有其不可替代的獨特功能。本題對族譜管理進行簡單的模擬,以實現檢視祖先和子孫個人資訊、插入家族成員、刪除家族成員、修改家族成員資訊、按事蹟查詢家族成員、列出全部家族成員及關係等功能。家族成員至少包括姓名、性別、生卒年月、配偶、簡介等資訊,資料結構可自行設計。

 

/*族譜管理
實現檢視祖先和子孫個人資訊、插入家族成員、刪除家族成員、修改家族成員資訊、
按事蹟查詢家族成員、列出全部家族成員及關係等功能。
家族成員至少包括姓名、性別、生卒年月、配偶、簡介等資訊,資料結構可自行設計。*/
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

typedef struct Tree{

    char name[99];
    char sex[10];
    char brith[99];
    char wife[99];
    char other[99];
    int  ranking;
    struct Tree *parent;
    struct Tree *brother;
    struct Tree *child;

}Tree;

char Member[99],Parent[99];

Tree *CreateGenealogy(Tree *&g)//建立族譜
{
    Tree *t;
    t = new Tree;
    t->brother=NULL;
    t->child=NULL;
    t->parent=NULL;
    cout<<"請輸入祖先的姓名"<<endl;
    cin>>t->name;
    cout<<"請輸入祖先的性別"<<endl;
    cin>>t->sex;
    cout<<"請輸入祖先的生日"<<endl;
    cin>>t->brith;
    cout<<"請輸入祖先的配偶"<<endl;
    cin>>t->wife;
    cout<<"請輸入祖先的個人簡介"<<endl;
    cin>>t->other;
    t->ranking=1;
    g=t;
    return g;
}

Tree *Find(Tree *b,char cname[])
{
    Tree *p;
    if(b==NULL)
    {
        //cout<<"NULL"<<endl;
        return NULL;
    }
    else if(strcmp(b->name,cname)==0)
    {
        return b;
    }
    else
    {
        p=Find(b->child,cname);
        if(p!=NULL)
        {
            return p;
        }
        else
        {
            return Find(b->brother,cname);
        }
    }
}

void addMember(Tree *&g,char Parent[],char Member[])
{
    Tree *p,*q;
    p=Find(g,Parent);
    if(p==NULL)
    {
        cout<<"NULL"<<endl;
        return ;
    }
    //else
    q = new Tree;
    strcpy(q->name,Member);
    cout<<"請輸入插入人的性別"<<endl;
    cin>>q->sex;
    cout<<"請輸入插入人的生日"<<endl;
    cin>>q->brith;
    cout<<"請輸入插入人的配偶"<<endl;
    cin>>q->wife;
    cout<<"請輸入插入人的個人簡介"<<endl;
    cin>>q->other;
    q->ranking=p->ranking+1;
    q->child=NULL;
	q->brother=NULL;
	q->parent=p;
    if(p->child==NULL)
    {
        p->child=q;
    }
    else
    {
        p=p->child;
        while(p->brother!=NULL)
        {
            p=p->brother;
        }
        p->brother = q;
    }
}

void deleteMember(Tree *&g)
{
    Tree *p,*s;
    char ch[99];
    cout<<"輸入你想要刪除的人姓名"<<endl;
    cin>>ch;
    p = Find(g,ch);
    if(p==NULL)
    {
        cout<<"查無此人,無法刪除!"<<endl;
        return ;
    }
    /*Tree *q;
    s=p;
    q=p->parent;
    else if(q->child==p)
	{
		if(p->brother!=NULL)
		{
			q->child=p->brother;
		}
		delete s;
	}*/
	else
    {
        p->ranking=0;
    }
}

void ChangeMsg(Tree *&g)
{
    char xm[99];
    cout<<"輸入你想要修改的人姓名"<<endl;
    cin>>xm;
    Tree *p,*q;
    p = Find(g,xm);
    if(p==NULL)
    {
        cout<<"查無此人,無法修改!"<<endl;
    }
    else
    {
        cout<<"請重新輸入你想要修改的個人資訊"<<endl;
        cout<<"請輸入姓名"<<endl;
        cin>>p->name;
        cout<<"請輸入性別"<<endl;
        cin>>p->sex;
        cout<<"請輸入生日"<<endl;
        cin>>p->brith;
        cout<<"請輸入其配偶"<<endl;
        cin>>p->wife;
        cout<<"請輸入個人簡介"<<endl;
        cin>>p->other;
    }
}

void LookGenealogy(Tree *b)
{
    if(b!=NULL)//此處忘加導致bug
    {
        for(int i=0;i<b->ranking;i++)
            cout<<" ";
        cout<<"輩分     "<<b->ranking<<";  ";
        cout<<"姓名:      "<<b->name<<";  ";
        cout<<"性別:       "<<b->sex<<";  ";
        cout<<"出生年月: "<<b->brith<<";  ";
        cout<<"配偶:      "<<b->wife<<";  ";
        cout<<"個人簡介: "<<b->other<<";  "<<endl;
        cout<<endl;
        if(b->brother!=NULL||b->child!=NULL)
        {
            LookGenealogy(b->child);
            LookGenealogy(b->brother);
        }
    }
}

void InfoSearch(Tree *b,string str)
{

    if(b!=NULL)
    {
        string s;
        s=b->other;
        if(s.find(str) < s.length())
        {
            cout<<"姓名:      "<<b->name<<";  "<<endl;
            cout<<"個人簡介: "<<b->other<<";  "<<endl;
        }
        else if (b->child!=NULL || b->brother!=NULL)
		{
            InfoSearch(b->child,str);
            InfoSearch(b->brother,str);
		}
    }
}

void menu()
{

	printf("\n");
	printf("--------------------------家譜系統---------------------------\n");
	printf("\n");
	printf("                       1.新增家族成員                        \n");
	printf("\n");
	printf("                       2.刪除家族成員                        \n");
	printf("\n");
	printf("                       3.查詢個人資訊                        \n");
	printf("\n");
	printf("                       4.修改個人資訊                        \n");
	printf("\n");
	printf("                       5.檢視詳細族譜                        \n");
	printf("\n");
	printf("                       6.根據事蹟查詢                        \n");
	printf("\n");
	printf("                       7.建立族譜                            \n");
	printf("\n");
	printf("                       0.退出系統                            \n");
	printf("\n");
	printf("-------------------------------------------------------------\n");
	printf("                    請輸入(0-7)進行操作:\n");
	printf("-------------------------------------------------------------\n");
	printf("\n");

}

int main()
{
    int n,m;
    Tree *g;
    char cname[99];
    menu();
    while(1&&n)
    {
        cin>>n;
        switch(n)
        {

            case 5: LookGenealogy(g); break;
            case 4: ChangeMsg(g);     break;
            case 2: deleteMember(g);  break;
            case 1: cout<<"請輸入需要插入人員的長輩姓名"<<endl;
                    cin>>Parent;
                    cout<<"請輸入需要插入人員的姓名"<<endl;
                    cin>>Member;
                    addMember(g,Parent,Member);
                    break;
            case 7: CreateGenealogy(g); break;
            case 3: cout<<"請輸入需要查詢人的姓名"<<endl;
                    cin>>cname;
                    //cout<<"..."<<g->name<<endl;why????????????
                    Tree *t ;
                    t = Find(g,cname);
                    if(t)
                    {
                        cout<<"姓名  "<<t->name<<endl;
                        cout<<"性別  "<<t->sex<<endl;
                        cout<<"出生年月  "<<t->brith<<endl;
                        cout<<"配偶  "<<t->wife<<endl;
                        cout<<"個人簡介  "<<t->other<<endl;
                        cout<<"輩分  "<<t->ranking<<endl;
                        if(t->parent==NULL)
                        {
                            cout<<"此人為祖先"<<endl;
                        }
                        else
                        {
                            cout<<t->name<<"的爸爸是  "<<t->parent->name<<endl;
                        }
                    }
                    else
                    {
                        cout<<"查無此人"<<endl;
                    }
                    break;
            case 6: string str;
                    cout<<"請輸入個人簡歷關鍵字"<<endl;
                    cin>>str;
                    InfoSearch(g,str);
                    break;
            //case 0: break;
        }
    }
    cout<<"感謝使用!"<<endl;
    return 0;
}