成员 |\\n\; cout << \|\\n\; cout << \修改成员信息 |\\n\; cout << \|\\n\; cout << \显示整个家谱 |\\n\; cout << \|\\n\; cout << \按ESC退出系统 |\\n\; cout << \|\\n\; cout << \---------------------------\\n\; cout << \; int n = 0; while (1){ n = _getch(); if (n == 27) break; if (n >= 49 &&n<=54) break; cout << \请按下1、2、3、4、5、6选择或按ESC键退出!\ << endl; } return n; } bool FamilySystem::fileInit(){ ifstream infile; //打开存储成员信息的文件,读入成员信息 infile.open(\, ios::in | ios::_Nocreate); if (!infile){ return false; } vector genely;//保存这一代成员的指针 vector next;//保存下一代成员的指针 Member *temp = new Member;//临时保存输入的成员信息 root = temp;//第一个赋给root genely.push_back(root); next.push_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; else{ Member *p = (*i)->pson; while (p->pbro!=nullptr) p = p->pbro; p->pbro = temp; } next.push_back(temp); temp = nullptr; break; } } //若成员的父亲不是这一代的成员,则将next赋予genely,迭进下一代 if (temp != nullptr){ genely = next;//迭进下一代 next.clear();//清空next for (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 (root->name == \未知\) root = nullptr; //打开存储家庭总体信息的文件,读入信息 infile.open(\, 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不为空,继续查找 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 (auto p : genely){ Member *temp = p->pson; if (temp==nullptr) continue; else{ next.push_back(temp); while (temp->pbro != nullptr){ next.push_back(temp->pbro); temp = temp->pbro;