收藏 分享(赏)

数据结构大作业之家谱管理系统.doc

上传人:精品资料 文档编号:11066443 上传时间:2020-02-06 格式:DOC 页数:37 大小:97.05KB
下载 相关 举报
数据结构大作业之家谱管理系统.doc_第1页
第1页 / 共37页
数据结构大作业之家谱管理系统.doc_第2页
第2页 / 共37页
数据结构大作业之家谱管理系统.doc_第3页
第3页 / 共37页
数据结构大作业之家谱管理系统.doc_第4页
第4页 / 共37页
数据结构大作业之家谱管理系统.doc_第5页
第5页 / 共37页
点击查看更多>>
资源描述

1、C+实现的数据结构大作业之家谱管理系统欢迎界面:主菜单:其他一些运行结果不在一一列举,一面篇幅过长。这个家谱管理系统,支持键盘操作,所有的操作都可以用键盘完成,如选择功能、退出等。代码的实现使用的是c+11标准,与旧标准或有不同。类的头文件:#include#includeusing std:string;using std:ostream;using std:istream;struct BirthDay /生日结构变量int year=0; int month=0; int day=0; ;class Memberfriend class FamilySystem;friend ostre

2、am /从文件中读入成员信息public:Member()=default;/使用默认合成构造函数void setE();/通过控制台输入给成员赋值Member() = default;/使用默认合成析构函数Member* pson=nullptr;/指向儿子的指针Member* pbro = nullptr;/指向兄弟的指针private:/成员的各种数据string name=“未知“;string birthPlace = “未知“;BirthDay birthDay;string sex = “未知“;float height=0;int age=0;string education

3、= “未知“;string job = “未知“;string father = “未知“;class FamilySystempublic:FamilySystem() = default;/使用默认合成构造函数FamilySystem()delete root;bool fileInit();/从文件中读取数据int menu();/显示菜单(功能)bool addMem();/增加成员bool deleteMem();/删除成员bool demandMem();/查询bool demandAve();/查询家庭整体情况bool modifyMem();/修改成员bool showFami

4、ly();/展示整个家谱bool saveFile();/保存信息到文件中,将使用两个文件,一个保存成员信息,一个保存家谱整体信息Member* root = nullptr;/家谱数结构的根节点指针?private:int total = 0;/总人数float aveAge=0;/平均年龄float aveHeight=0;/平均身高float aveMember=0;/家庭平均人数float ratio=0;/男女比例void calculate();/计算平均值等Member *seek(string);/查找是否存在成员void display(Member*, int);/先序遍历

5、树,用于显示所有成员;extern string gets();/用于接收键盘的输入类的函数及相关操作函数的实现#include#include#include#include#include#include“SystemClass.h“using std:cin;using std:cout;using std:endl;using std:setw;using std:vector;using std:ios;using std:ifstream;using std:ofstream;using std:istringstream;string gets()/从控制台获取字符等信息stri

6、ng s;while (int n = _getch()if (n = 27)throw 27;break;else if (n = 13)cout (istream return in;ostream cout birthPlace; cout birthDay.year birthDay.month birthDay.day;if ( !sin|birthDay.year 2014 | birthDay.month 12 | birthDay.day 31)cout sex)if (sex = “male“ | sex = “female“ | sex = “男“ | sex = “女“)

7、break;elsecout height)cout age) | age 300)cout education;cout job;cout father;catch (int ex)throw ex;return;ostream return in;/FamilySystem的成员函数int FamilySystem:menu()cout = 49 /保存这一代成员的指针vector next;/保存下一代成员的指针Member *temp = new Member;/临时保存输入的成员信息root = temp;/第一个赋给rootgenely.push_back(root);next.p

8、ush_back(root);while (infile *temp)/将成员放入家族树中,成员的父亲必须是这一代的成员for (auto i = genely.begin(); i != genely.end(); +i)if (*i)-name = temp-father)if (*i)-pson=nullptr)(*i)-pson = temp;elseMember *p = (*i)-pson;while (p-pbro!=nullptr)p = p-pbro;p-pbro = temp;next.push_back(temp);temp = nullptr;break;/若成员的父亲

9、不是这一代的成员,则将next赋予genely,迭进下一代if (temp != nullptr)genely = next;/迭进下一代next.clear();/清空nextfor (auto i : genely)/将成员放入家族树中if (i-name = temp-father)i-pson = temp;next.push_back(temp);temp = nullptr;break;/动态创建临时成员变量,用于存储输入的下一个成员的信息temp = new Member;infile.close();/关闭存储成员信息的文件/若文件中没有成员信息,将root置为空if (roo

10、t-name = “未知“)root = nullptr;/打开存储家庭总体信息的文件,读入信息infile.open(“Wholes.txt“, ios:in | ios:_Nocreate);if (!infile)return false;infile total aveAge aveHeight aveMember ratio;infile.close();/关闭文件return true;Member *FamilySystem:seek(string nam)/若家谱树为空,返回空指针if (root = nullptr) return nullptr;/若root不为空,继续查找

11、Member *store = nullptr;/存储返回的指针vector genely;/存储这一代的指针genely.push_back(root);vector next;/存储下一代的指针for (;)/循环查找/在这一代中查找,若找到,跳到最后for (auto p : genely)if (p-name = nam)store = p;goto End;/判断下一代是否为空,若为空,跳到最后int jubge = 0;for (auto p : genely)if (p-pson != nullptr)+jubge;if (jubge = 0)goto End;/找到下一代for

12、 (auto p : genely)Member *temp = p-pson;if (temp=nullptr) continue;elsenext.push_back(temp);while (temp-pbro != nullptr)next.push_back(temp-pbro);temp = temp-pbro;genely = next;next.clear();End:return store;bool FamilySystem:addMem()cout setE();/输入成员信息catch (int)return false;/检查其父亲是否存在Member *jubge

13、= seek(temp-father);if (root = nullptr) root = temp;else if (jubge = nullptr)cout name) != nullptr)cout pson = nullptr) jubge-pson = temp;elseMember *p = jubge-pson;while (p-pbro != nullptr)p = p-pbro;p-pbro = temp;cout na;catch (int)goto end;Member *temp = seek(na);if (temp = nullptr)cout pson != n

14、ullptr)cout father);if (p = nullptr) root = nullptr;else if (temp = p-pson) p-pson = nullptr;elsewhile (p-pbro!=temp)p = p-pbro;p-pbro = nullptr;cout na;catch (int)goto end;Member *m=seek(na);if(m!=nullptr) cout genely;/存储这一代的指针genely.push_back(root);vector next;/存储下一代的指针int girl = 0;/女性数int family

15、= 0;/家庭数total = 0; aveAge = 0; aveHeight = 0;/initializefor (;)/循环叠加各所需总体数据/在这一代中for (auto p : genely)+total;aveAge += p-age;aveHeight += p-height;if (p-sex = “female“ | p-sex = “女“)+girl;/判断下一代是否为空,若为空,跳到最后int jubge = 0;for (auto p : genely)if (p-pson != nullptr)+jubge;+family;if (jubge = 0)goto ca

16、l;/找到下一代for (auto p : genely)Member *temp = p-pson;if (temp = nullptr) continue;elsenext.push_back(temp);while (temp-pbro != nullptr)next.push_back(temp-pbro);temp = temp-pbro;genely = next;/迭进下一代next.clear();/清空next/计算最后结果cal:total = total;aveAge /= total;aveHeight /= total;if (family = 0) family =

17、 1;aveMember = static_cast(total) / static_cast(family);if (girl!=0)ratio = static_cast(total - girl) / static_cast(girl);else ratio = 0;/退出return;bool FamilySystem:demandAve()calculate();cout na;catch (int)goto rend;Member *temp = seek(na); if (temp = nullptr)cout name;cout temp-name;if (temp-pson

18、= nullptr) break;elsestring na = temp-name;temp=temp-pson;temp-father = na;while (temp-pbro != nullptr)temp = temp-pbro;temp-father = na;break;case 50:cout birthPlace;cout temp-birthDay;break;case 51:cout birthDay;cout temp-birthDay;break;case 52:cout sex;cout temp-sex;while (temp-sex != “male“break

19、;case 53:cout height;cout temp-height;break;case 54:cout age;cout temp-age;break;case 55:cout education;cout temp-education;break;case 56:cout job;cout temp-job;break;default:;cout namepson!=nullptr)display(r-pson, n + 6);if (r-pbro != nullptr)display(r-pbro, n);return;bool FamilySystem:showFamily()cout genely;/保存一代成员的指针genely.push_back(root);while (1)/输出这一代成员的信息for (auto p : genely)if (p != nullptr)outfile name birthPlace birthDay sex height age job education father pson != nullptr)+jubge;if (jubge = 0) break;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报