C language to achieve the establishment and operation of the family tree

In the case of considering only the immediate family and only consider within four generations of relatives, a family genealogy is actually a binary tree upside down.
Our mission is to:
1. The use of fourth-generation family tree preservation immediate family;
2. be able to take advantage of the program calculates the relationship between any two family members, and given the official name;
to take all appropriate format output genealogical tree information
this problem becomes simple again
Here Insert Picture Description
using the binary tree for storage, then find where the two layers of the input tree can get two generational size, re-use a particular function will be that they have to dig out the root of sub-tree, encoding the high-generational node in the tree generational low, can be obtained according to a correspondence relationship between the two encoding table.

#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
char q[10];
typedef struct tree
{
	char name[20];
	tree *left;
	tree *right;
}tree;
struct wenjian
{
	char a[20];
}d[50];
void guodu(wenjian d[50])
{
	FILE *fp;
	int i=0;
	if((fp=fopen("D:\\name123.txt","r"))==NULL)
	{
		printf("FILE open error");
		exit(0);
	}
	while(!feof(fp))
	{
	fgets(d[i].a,50,fp)==NULL
	printf("%s\n",d[i].a);
	i++;
	}
	fclose(fp);
}
tree *creattree(int &len,wenjain d[50])
{
	tree *T;
	T=(tree *)malloc(sizeof(tree));
	if(d[len].a[0]=='*')
		return NULL;
	else
	{
		strcpy(T->name,d[len].a);
		T->left=creattree(len+1,d);
		T->right=creattree(len+1,d);
	}
	return T;
}
void returnlevel(tree *T,char b[20],int s,int &flag)
{
	if(T)
	{
		if(strcmp(T->name,b)==0)
			flag=s;
		returnlevel(T->left,b,s+1,flag);
		returnlevel(T->right,b,s+1,flag);
	}
}
void find(tree *T,char b[20],tree *&z)
{
	if(T)
	{
		if(strcmp(T->name,b)==0)
		{
			z=T;
		}
		find(T->left,b,z);
		find(T->right,b,z);
	}
}
void code(tree *t,tree *v,int len)
{
	static char a[10];
	if(strcmp(t->name,v->name)==0)
	{
		for(int i=0;i<len;i++)
		{
			q[i]=a[i];
		}
	}
	else
	{
		if(t->left!=NULL)
		{
			a[len]='0';
			code(t->left,v,len+1);
		}
		if(t->right!=NULL)
		{
			a[len]='1';
			code(t->right,v,len+1);
		}
	}
	
}
void prints(tree *T)
{
	if(T)
	{
		printf("%s ",T->name);
		prints(T->left);
		prints(T->right);
	}
}
void print(tree *T,char b[20],char c[20])
{
	for(int i=0;i<20;i++)
		q[i]=0;
	int x=-1,y=-1;
	tree *m,*n;
	m=(tree *)malloc(sizeof(tree));
	n=(tree *)malloc(sizeof(tree));
	returnlevel(T,b,0,x);
	returnlevel(T,c,0,y);
	if(x==-1||y==-1)
		printf("error,您的输入有误请重新输入\n");
	find(T,b,m);
	find(T,c,n);
	if(x>y)
	{
		code(n,m,0);
		if(strcmp(q,"0")==0)
			printf("%s是%s的父亲\n",b,c);
		if(strcmp(q,"00")==0)
			printf("%s是%s的爷爷\n",b,c);
		if(strcmp(q,"000")==0)
			printf("%s是%s的太爷爷\n",b,c);
		if(strcmp(q,"001")==0)
			printf("%s是%s的太奶奶\n",b,c);
		if(strcmp(q,"01")==0)
			printf("%s是%s的奶奶\n",b,c);
		if(strcmp(q,"010")==0)
			printf("%s是%s的外太爷爷\n",b,c);
		if(strcmp(q,"011")==0)
			printf("%s是%s的外太奶奶\n",b,c);
		if(strcmp(q,"1")==0)
			printf("%s是%s的母亲\n",b,c);
		if(strcmp(q,"10")==0)
			printf("%s是%s的姥爷\n",b,c);
		if(strcmp(q,"11")==0)
			printf("%s是%s的姥姥\n",b,c);
		if(strcmp(q,"100")==0)
			printf("%s是%s的太姥爷\n",b,c);
		if(strcmp(q,"101")==0)
			printf("%s是%s的太姥姥\n",b,c);
		if(strcmp(q,"110")==0)
			printf("%s是%s的外太姥爷\n",b,c);
		if(strcmp(q,"111")==0)
			printf("%s是%s的外太姥姥\n",b,c);
	}
	else if(x<y)
	{
		code(m,n,0);
		if(strcmp(q,"0")==0)
			printf("%s是%s的父亲\n",c,b);
		if(strcmp(q,"00")==0)
			printf("%s是%s的爷爷\n",c,b);
		if(strcmp(q,"000")==0)
			printf("%s是%s的太爷爷\n",c,b);
		if(strcmp(q,"001")==0)
			printf("%s是%s的太奶奶\n",c,b);
		if(strcmp(q,"01")==0)
			printf("%s是%s的奶奶\n",c,b);
		if(strcmp(q,"010")==0)
			printf("%s是%s的外太爷爷\n",c,b);
		if(strcmp(q,"011")==0)
			printf("%s是%s的外太奶奶\n",c,b);
		if(strcmp(q,"1")==0)
			printf("%s是%s的母亲\n",c,b);
		if(strcmp(q,"10")==0)
			printf("%s是%s的姥爷\n",c,b);
		if(strcmp(q,"11")==0)
			printf("%s是%s的姥姥\n",c,b);
		if(strcmp(q,"100")==0)
			printf("%s是%s的太姥爷\n",c,b);
		if(strcmp(q,"101")==0)
			printf("%s是%s的太姥姥\n",c,b);
		if(strcmp(q,"110")==0)
			printf("%s是%s的外太姥爷\n",c,b);
		if(strcmp(q,"111")==0)
			printf("%s是%s的外太姥姥\n",c,b);
	}
	else
	{
		printf("本程序仅支持计算直系亲属关系,您的输入有误请重新进行输入\n");
	}
}
int main()
{
	printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	printf("++                                                        ++\n");
	printf("++                                                        ++\n");
	printf("++                 家庭成员关系计算器                     ++\n");
	printf("++                                                        ++\n");
	printf("++                                                        ++\n");
	printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	printf("++                   若想退出系统请按0                    ++\n");
	printf("           请输入您想查询的两个人,不分先后顺序             \n");
	tree *T;
	wenjian d[50];
	guodu(d);
	T=creattree(0,d);
	while(1)
	{
	printf("           请输入您想查询的两个人,不分先后顺序             \n");
	char b[20],c[20];
	scanf("%s",b);
	if(b[0]=='0')
		break;
	scanf("%s",c);
	print(T,b,c);
	}
	return 0;
}
Test sample: a king, the king of forty nine bastard * * * * Wang Wang Wu Wang Shi Wang eleven * * * * King of Three Kings 6 Wang Wang thirteen XII * * * * * * king SEVEN fourteen king XV * *
Published 12 original articles · won praise 13 · views 337

Guess you like

Origin blog.csdn.net/weixin_45939019/article/details/104054704
Recommended