老早之前做的一个关于家谱的数据结构实训

老早之前做的一个关于家谱的数据结构实训,现在给大家开放一看,增加了一个显示全部家谱的功能,想要源代码文件和可执行文件,可以点我的头像,然后点资源就可以找到了。

实训1.cpp

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
#define MAX 10
typedef struct node//定义data存储结构,存放个人信息
{       char name[MAX]; //姓名
        char sex;//性别
        int generation;//代
}node;
typedef struct treenode//创建结构体
{
        struct node l; //家谱中直系家属
        struct treenode *brother;//用来指向兄弟
        struct treenode *child;//用来指向孩子
}treenode;
void screen();
using namespace std;


template<class T>
T fun(T x)
{
    if(x!=2)
        throw x;
    else
        return x;
}
treenode *root; //root是指向结构体treenode的指针



treenode *search(treenode *p,char ch[])  
{
    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;//没有找到
}
 

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;
}

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;
}

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;
            }
 
 
        }
 
    }
}

void InitTree() //初始化(创建)
{
    system("cls");
    //system("color 4f");
 
    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=(treenode *)malloc(sizeof(treenode)); 
                                            
    scanf("%s %c",&b,&c);//输入姓名,性别
    while(c!='x'&&c!='n')
    {
        try
        {
            fun(c);
        }
        catch(...)
        {
            cout<<"输入性别有误!请重新输入!(女士:n;先生:x):"<<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");
    screen();
}

void Add() //添加
{
    system("cls");
    //system("color 4f");
    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);
    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!='x'&&c!='n')
    {
        try
        {
            fun(c);
        }
        catch(...)
        {
            cout<<"输入性别有误!请重新输入!(女士:n;先生:x):"<<endl;
        }
        scanf("%s %c",&b,&c);//输入姓名,性别
    }
        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!='x'&&c!='n')
    {
        try
        {
            fun(c);
        }
        catch(...)
        {
            cout<<"输入性别有误!请重新输入!(女士:n;先生:x):"<<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");
    screen();
 
}

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");
    screen();
}

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!='x'&&c!='n')
    {
        try
        {
            fun(c);
        }
        catch(...)
        {
            cout<<"输入性别有误!请重新输入!(女士:n;先生:x):"<<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");
    screen();
}

 


void outall(treenode *t, int i)
{
	for(int j=0;j<i;j++)
	{
		printf("\t");
	}
	printf("%s\n",t->l.name);
	int c;
	c=i+1;
	if(t->child!=NULL)
	{
		outall(t->child,c);
	}
	if(t->brother!=NULL)
	{
		outall(t->brother,c);
	}
}

void screen()//最初显示界面
{
    system("cls");
    //system("color 4f");
    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<<"                                   0 退出"<<endl<<endl;
    cout<<endl;
    int a;
    cout<<"                                 请输入数字:";
    cin>>a;
    while(!cin)
    {
        cout<<"                                 输入错误,请重新输入:";
        cin.sync();
        cin.clear();
        cin>>a;
    }
    while(a<1||a>6)
    {
        try
        {
            fun(a);
        }
        catch(...)
        {
            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:
		outall(root,0);
    case 0:
      exit(0);
       break;
    }
}

实训2.cpp

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include "windows.h"

void screen();
using namespace std;



void proc()
{
	char buf[103];
	memset(buf, ' ', sizeof(buf));
	buf[0] = '[';
	buf[101] = ']';
	buf[102] = '\0';
	int i = 0;
	char index[6] = "-\\|/\0";
	while (i <= 100)
	{
		buf[i] = '=';
		printf("%s [%d%%][%c]\r", buf, i, index[i % 4]);
		fflush(stdout);//刷新缓冲区
		Sleep(66);
		i++;
	}
 
	printf("\n");
}
//进度条
int main()
{
 
    proc();
	screen();
    return 0;
 
}
发布了25 篇原创文章 · 获赞 14 · 访问量 5445

猜你喜欢

转载自blog.csdn.net/qq_40568770/article/details/85927866