#include<iostream>
#include<fstream> //ifstream.open
#include<sstream> //sin
#include<string> //string
#include<cstring> //strcmp
#include<stdio.h> //清屏
#include<conio.h> //getch()
using namespace std;
#define max 50
#define True 1
#define False 0
/*全局变量定义*/
char Name[max][max];
char Birth[max][max];
int Marry[max] = { 0 }; // 1代表结婚 -1代表未婚
char Address[max][max];
int Alive[max] = { 0 }; // 1代表健在 -1代表死亡
char Pass[max][max]; //死亡日期(若其已死亡)
int Gene[max] = { 0 }; //第几
int Num[max] = { 0 }; //孩子个数
int k = 0; //文件读取计数
int m = 0; //判定是否有成员在某天生日
typedef struct Node
{
const char* name; //姓名
const char* birth; //出生日期
int marry; // 1代表结婚 -1代表未婚
const char* address; //地址
int alive; // 1代表健在 -1代表死亡
const char* pass; //死亡日期(若其已死亡)
int gene; //第几代
int num; //孩子个数
Node* child[max]; //指针指向孩子结点
Node* parent; //指针指向父亲结点
Node(); //无参构造函数
}PedNode, * Tree;
Node::Node() //结点构造
{
name = NULL;
birth = NULL;
address = NULL;
marry = 0;
alive = 0;
pass = NULL;
gene = 0;
num = 0;
*child = { NULL };
parent = NULL;
}
/*功能函数声明*/
void MainMenu(); //家谱主菜单
void CreateTree(Tree tree); //调用子树构造函数 递归创建家谱树
void NewChildTree(Tree tree); //子树构造函数
void ShowAll(Tree tree); //递归输出整个家谱信息
bool NameSearch(Tree tree, char Name[], Tree* DrawTree); //在树中按照姓名进行查找
void ShowIn(Tree tree); //输出成员信息(包括本人,父母,孩子的信息) 服务于姓名查询
void All_n(Tree tree, int date); //显示第n代所有人信息
void ShowAll_n(Tree tree); //查找第n代成员功能函数
void BirthSearch(Tree tree, char* date);//按照生日查询成员名单
void ShowBirthSearch(Tree tree); //生日查询输出功能
void Relation(Tree tree, char* name1, char* name2);//确定两人关系
Tree Delete(Tree tree); //删除树
void AddChild(Tree tree, char* name3); //某成员添加孩子
Tree deleteTree(Tree tree, char* aa); //传入要删除成员姓名
void Show(Tree tree); //输出某一个节点的信息
void Change(Tree tree, char* name4); //修改信息
int main()
{
int choice; //菜单选择
char namea[max], nameb[max], namec[max], named[max], namee[max];
ifstream inf;
inf.open("d://ped.txt");
string line;
int i = 0;
while (getline(inf, line))
{
istringstream sin(line);
sin >> Name[i] >> Birth[i] >> Marry[i] >> Address[i] >> Alive[i] >> Pass[i] >> Gene[i] >> Num[i];
i++;
}
Node pednode;
Tree pedtree = &pednode;
CreateTree(pedtree);
MainMenu();
cout << endl;
cout << "请输入相应功能前数字进行实现!" << endl;
cin >> choice;
for (; choice != 0;)
{
switch (choice)
{
case 1:
{
ShowBirthSearch(pedtree);
} break;
case 2:
{
ShowAll_n(pedtree);
}break;
case 3:
{
ShowIn(pedtree);
}break;
case 4:
{
cout << "请输入想要输出关系的两个成员的姓名(中间用空格符间隔):" << endl;
cin >> nameb >> namec;
Relation(pedtree, nameb, namec);
}break;
case 5:
{
cout << "请输入添加孩子的父(母)姓名:" << endl;
cin >> named;
AddChild(pedtree, named);
}break;
case 6:
{
cout << "请输入想要删除的成员姓名:" << endl;
cin >> namea;
pedtree = deleteTree(pedtree, namea);
} break;
case 7:
{
cout << "请输入要修改信息的成员姓名:" << endl;
cin >> namee;
Change(pedtree, namee);
}break;
case 0:
{
}break;
}
cout << "请输入相应功能前数字进行实现!" << endl;
fflush(stdin);//清除键盘缓冲区
cin >> choice;
system("cls");
MainMenu();
}
return 0;
}
void MainMenu()
{
cout << "* * * * * * * * * * * * * * * * * * * * *" << endl;
cout << "* 家谱管理系统主菜单 *" << endl;
cout << "*---------------------------------------*" << endl;
cout << "* 1:查询某天出生的成员名单 *" << endl;
cout << "* 2:显示第n代所有人信息 *" << endl;
cout << "* 3:按照姓名查询成员信息 *" << endl;
cout << "* 4:确定两人关系 *" << endl;
cout << "* 5:添加孩子 *" << endl;
cout << "* 6:删除某成员 *" << endl;
cout << "* 7:修改某成员信息 *" << endl;
cout << "* 0:退出程序 *" << endl;
cout << "* * * * * * * * * * * * * * * * * * * * *" << endl;
}
void NewChildTree(Tree childtree) //创建子树 递归调用
{
for (int e = 0; e < childtree->num; e++)
{
Tree ChildTree = new Node;
childtree->child[e] = ChildTree;
ChildTree->name = Name[k];
ChildTree->birth = Birth[k];
ChildTree->marry = Marry[k];
ChildTree->address = Address[k];
ChildTree->alive = Alive[k];
ChildTree->pass = Pass[k];
ChildTree->gene = Gene[k];
ChildTree->num = Num[k];
k++;
ChildTree->parent = childtree;
NewChildTree(ChildTree);
}
}
void CreateTree(Tree tree) //创建家谱树
{
tree->name = Name[k];
tree->birth = Birth[k];
tree->marry = Marry[k];
tree->address = Address[k];
tree->alive = Alive[k];
tree->pass = Pass[k];
tree->gene = Gene[k];
tree->num = Num[k];
tree->parent = NULL;
int p = tree->num;
k++;
for (int b = 0; b < p; b++)
{
Tree ChildTree = new Node;
tree->child[b] = ChildTree;
ChildTree->name = Name[k];
ChildTree->birth = Birth[k];
ChildTree->marry = Marry[k];
ChildTree->address = Address[k];
ChildTree->alive = Alive[k];
ChildTree->pass = Pass[k];
ChildTree->gene = Gene[k];
ChildTree->num = Num[k];
k++;
ChildTree->parent = tree;
NewChildTree(ChildTree);
}
}
void ShowAll(Tree tree) //输出整个家谱树
{
Show(tree);
for (int f = 0; f < tree->num; f++)
{
cout << tree->name << "的第" << f + 1 << "个子女:" << endl;
ShowAll(tree->child[f]);
}
}
void Show(Tree tree) //输出家谱中某个成员的信息
{
if (tree->name == "0")
{
cout << "该成员已被删除" << endl;
cout << endl;
}
else
{
cout << "姓名:" << tree->name << endl;
cout << "出生日期: " << tree->birth << endl;
if (tree->marry == 1)
cout << "婚否:已婚" << endl;
else
cout << "婚否:未婚" << endl;
cout << "住址:" << tree->address << endl;
if (tree->alive == 1)
cout << "健在状况:健在" << endl;
else
{
cout << "健在状况:死亡" << endl;
cout << "死亡日期:" << tree->pass << endl;
}
cout << "宗族代数:第" << tree->gene << "代" << endl;
cout << endl;
}
}
void ShowIn(Tree tree) //姓名查找的输出函数
{
Tree temptree = NULL;
cout << "请输入要查找人的姓名:" << endl;
char namea[max];
cin >> namea;
bool g = NameSearch(tree, namea, &temptree);
if (g)
{
cout << "查找成功!" << endl;
cout << "-----本人信息为------" << endl;
Show(temptree);
if (temptree->parent != NULL)
{
cout << "父(母)亲姓名为:" << temptree->parent->name << endl;
}
cout << endl;
if (temptree->num == 0)
{
cout << "该成员无孩子" << endl;
}
else
{
for (int h = 0; h < temptree->num; h++)
{
cout << "孩子姓名为:";
cout << temptree->child[h]->name << endl;
}
}
}
else
cout << "家谱中不存在该人信息" << endl;
}
bool NameSearch(Tree tree, char Name[], Tree* DrawTree)//在树中按照姓名查找
{
if (strcmp(tree->name, Name) == 0)
{
*DrawTree = tree;
return true;
}
else
{
for (int g = 0; g < tree->num; g++)
{
if (NameSearch(tree->child[g], Name, DrawTree))
return true;
}
return false;
}
}
void All_n(Tree tree, int dai) //寻找第n代所有人信息
{
if (tree->gene == dai)
{
Show(tree);
cout << endl;
}
else
{
for (int j = 0; j < tree->num; j++)
{
All_n(tree->child[j], dai);
}
}
}
void ShowAll_n(Tree tree) //查找第n代成员功能函数
{
cout << "请输入想要查询的代数" << endl;
int dai;
cin >> dai;
cout << "*****************************" << endl;
All_n(tree, dai);
cout << "第" << dai << "代成员输出完毕!" << endl;
cout << "*****************************" << endl;
}
void BirthSearch(Tree tree, char* date) //按照生日查询成员名单
{
if (strcmp(tree->birth, date) == 0)
{
cout << tree->name << endl;
m += 1;
}
else
{
for (int j = 0; j < tree->num; j++)
{
BirthSearch(tree->child[j], date);
}
}
}
void ShowBirthSearch(Tree tree) //生日查询输出功能
{
char date[max];
cout << "**********************************************************" << endl;
cout << "输入想要查询的出生日期:(输入格式示例:2000-06-27)" << endl;
cin >> date;
cout << "在" << date << "出生的成员名单为:" << endl;
BirthSearch(tree, date);
if (m == 0)
{
cout << "无" << endl;
cout << "没有成员在这一天生日!" << endl;
}
cout << endl;
cout << "生日查询结束!" << endl;
cout << "***********************************************************" << endl;
}
Tree Delete(Tree tree) //删除树
{
if (tree)
{
for (int n = 0; n < tree->num; n++)
tree->child[n] = Delete(tree->child[n]);
tree->alive = 0;
tree->gene = 0;
tree->name = "0";
tree->num = 0;
tree->birth = "0";
tree->pass = "0";
tree->marry = 0;
tree->address = "0";
}
return tree;
}
Tree deleteTree(Tree tree, char* aa) //传入要删除成员姓名
{
Tree tempTree = NULL;
if (NameSearch(tree, aa, &tempTree))
{
Delete(tempTree);
cout << "删除成功!" << endl;
return tree;
}
else
{
cout << "家族中没有该成员!" << endl;
return tree;
}
}
void Relation(Tree tree, char* name1, char* name2) //确定两人关系
{
Tree temptree1 = NULL, temptree2 = NULL;
bool o = NameSearch(tree, name1, &temptree1);
bool p = NameSearch(tree, name2, &temptree2);
if (o && p) //两个人均查找成功
{
if (temptree1->parent == temptree2)
{
cout << name2 << "是" << name1 << "的父(母)亲" << endl;
}
else if (temptree2->parent == temptree1)
{
cout << name1 << "是" << name2 << "的父(母)亲" << endl;
}
else if (temptree1->parent == temptree2->parent)
{
cout << name1 << "和" << name2 << "是兄弟姐妹关系" << endl;
}
else if (temptree1->parent->parent == temptree2 && temptree1->parent->parent != NULL)
{
cout << name2 << "是" << name1 << "的祖辈" << endl;
}
else if (temptree2->parent->parent == temptree1 && temptree1->parent->parent != NULL)
{
cout << name1 << "是" << name2 << "的祖辈" << endl;
}
else if ((temptree1->parent != temptree2->parent) && (temptree1->parent->parent == temptree2->parent->parent) && temptree1->parent->parent != NULL && temptree2->parent->parent != NULL)
{
cout << name1 << "和" << name2 << "互为表兄弟(表姐妹/堂兄弟/堂姐妹)" << endl;
}
else if ((temptree1->parent->parent == temptree2->parent) && temptree1->parent != temptree2 && temptree1->parent->parent != NULL)
{
cout << name1 << "的父母和" << name2 << "是兄弟姐妹" << endl;
}
else if ((temptree2->parent->parent == temptree1->parent) && (temptree2->parent != temptree1))
{
cout << name2 << "的父母和" << name1 << "是兄弟姐妹" << endl;
}
else
{
cout << name1 << "和" << name2 << "无明显近亲关系" << endl;
}
}
else
{
cout << "未在家谱中查找到这两名成员" << endl;
}
}
void AddChild(Tree tree, char* name3) //某成员添加孩子
{
Tree temptree4 = NULL;
bool q = NameSearch(tree, name3, &temptree4);
Tree ChildTree = new Node;
temptree4->child[temptree4->num] = ChildTree;
temptree4->num += 1;
char name5[max];
cout << "请输入孩子姓名:" << endl;
cin >> name5;
ChildTree->name = name5;
char adress[max];
cout << "请输入地址:" << endl;
cin >> adress;
ChildTree->address = adress;
char bir[max];
cout << "请输入孩子的出生日期:(示例:2021-02-12)" << endl;
cin >> bir;
ChildTree->birth = bir;
ChildTree->alive = 1;
ChildTree->num = 0;
ChildTree->marry = -1;
ChildTree->gene = (temptree4->gene) + 1;
cout << "添加孩子成功!" << endl;
}
void Change(Tree tree, char* name4) //修改信息
{
cout << "**********************************************************" << endl;
cout << "若某项信息不需要修改,输入0跳过此项" << endl;
Tree temptree5 = NULL;
bool r = NameSearch(tree, name4, &temptree5);
if (r)
{
char name6[max];
cout << "姓名变更为:" << endl;
cin >> name6;
if (strcmp(name6, "0") == 1)
temptree5->name = name6;
char adr[max];
cout << "住址变更为:" << endl;
cin >> adr;
if (strcmp(adr, "0") == 1)
temptree5->address = adr;
char bi[max];
cout << "出生日期变更为:" << endl;
cin >> bi;
if (strcmp(bi, "0") == 1)
temptree5->birth = bi;
int ma;
cout << "婚姻状态变更为:(1代表已婚 -1代表未婚)" << endl;
cout << "请输入1或-1修改婚姻状态!或输入0跳过此步骤" << endl;
cin >> ma;
if (ma == 1 || ma == -1)
temptree5->marry = ma;
int al;
cout << "健在状态变更为:(1代表健在 -1代表死亡)" << endl;
cin >> al;
if (al == 1 || al == -1)
temptree5->alive = al;
char pa[max];
cout << "死亡日期变更为:" << endl;
cin >> pa;
if (strcmp(pa, "0") == 1)
temptree5->pass = pa;
cout << endl;
cout << "变更结束!" << endl;
cout << "变更后的成员信息为:" << endl;
Show(temptree5);
}
else
cout << "未在家族中发现此人信息!" << endl;
cout << "变更功能结束!" << endl;
cout << "***********************************************" << endl;
}
简易家谱管理系统
最新推荐文章于 2022-03-17 10:58:07 发布