?? operationfamilytree.cpp
字號:
case 10:
case 12:
if(date.day<1||date.day>31)
return false;
else
return true;
break;
case 4:
case 6:
case 9:
case 11:
if(date.day<1||date.day>30)
return false;
else
return true;
break;
case 2:
if(IsLeapYear(date.year)){
if(date.day<1||date.day>29)
return false;
else
return true;
}
else{
if(date.day<1||date.day>28)
return false;
else
return true;
}
break;
default:
return false;
}
}
int COperationFamilytree::CompareDate(Date date1, Date date2)
{
//本函數比較兩日期大小:
//date1比date2早,返回-1;date1比date2晚,返回1;date1與date2相等,返回0
if(date1.year>date2.year)
return 1;
else if(date1.year<date2.year)
return -1;
else if(date1.month>date2.month)
return 1;
else if(date1.month<date2.month)
return -1;
else if(date1.day>date2.day)
return 1;
else if(date1.day<date2.day)
return -1;
else
return 0;
}
bool COperationFamilytree::IsLeapYear(int year)
{
//本函數判斷year是否為閏年
if(year%4!=0)
return false;
else if(year%400==0)
return true;
else if(year%100==0)
return false;
else
return true;
}
void COperationFamilytree::InsertSibling(Person &firstSibling,Person insertSibling)
{
//本函數把insertSibling結點插入到以firstSibling為第一個結點的兄弟結點中
//使兄弟結點以年齡排序
Person pCur,pPre;
pCur=firstSibling; //pCur為第一個孩子
//pCur不是第一個孩子,則pPre為pCur的前一個兄弟,否則pPre為pCur的父親
pPre=firstSibling->parent;
for(;pCur!=0;pCur=pCur->sibling){ //兄弟之間以年齡大小排序(年齡大的在最前)
if(CompareDate(pCur->info.birthday,insertSibling->info.birthday)>=0)
break;
pPre=pCur;
}
if(pCur==firstSibling){ //如果insertSibling的年齡比第一個孩子大
insertSibling->sibling=firstSibling; //則insertSibling應為pPre的第一個孩子
pPre->child=insertSibling;
}
else{ //插入到相應位置
insertSibling->sibling=pCur;
pPre->sibling=insertSibling;
}
}
void COperationFamilytree::Add(Person parent, Person addNode)
{
//本函數把addNode結點加入到其父結點parent下
addNode->child=addNode->sibling=0; //把欲加入的結點所有指針域置空
addNode->parent=parent; //因addNode欲加為parent的孩子,故addNode結點的父指針域應指向parent
if(parent==0){ //若parent為0,則表示欲加addNode為根結點
if(T==0){ //若本為空家譜
T=addNode; //把addNode當成根結點
return;
}
addNode->child=T; //使原來的根結點成為新根結點的孩子
T->parent=addNode;
T=addNode;
return;
}
if(parent->child==0) //parent無孩子,把addNode加入其孩子
parent->child=addNode;
else
InsertSibling(parent->child,addNode); //把addNode加到parent孩子的兄弟域中
}
void COperationFamilytree::Delete(Person &rootNode)
{
//本函數刪除以rootNode為根結點的所有結點
if(rootNode->parent) //如果rootNode有父結點
if(rootNode->parent->child==rootNode) //如果rootNode為其父結點的第一個孩子
rootNode->parent->child=rootNode->sibling; //因要刪掉rootNode,故把其父結點的孩子指針指向rootNode的第一個兄弟
else{ //如果rootNode不是父結點的第一個孩子
Person p=rootNode->parent->child; //找到rootNode應在兄弟中的位置
for(;p->sibling!=rootNode;p=p->sibling)
;
p->sibling=rootNode->sibling; //插入到兄弟中
}
PostOrderTraverse(rootNode->child,DestroyNode);//刪除以rootNode->child為根結點的所有結點
if(rootNode==T) //刪除rootNode結點。如果rootNode為根結點,則刪除根結點T
DestroyNode(T);
else
DestroyNode(rootNode);
}
void COperationFamilytree::Modify(Person &pNode, Person newValue)
{
//本函數用結點newValue中的信息來修改結點pNode中的信息
strcpy(pNode->info.name,newValue->info.name);
strcpy(pNode->info.addr,newValue->info.addr);
pNode->info.birthday=newValue->info.birthday;
pNode->info.marry=newValue->info.marry;
pNode->info.live=newValue->info.live;
if(!pNode->info.live) //如過世,還應有死亡日期
pNode->info.deathday=newValue->info.deathday;
}
Person& COperationFamilytree::GetRoot()
{
//本函數返回家譜的根結點
return T;
}
int COperationFamilytree::ChildNums(Person pNode)
{
//本程序返回pNode的孩子數
Person p;
p=pNode->child;
int childnums=0;
for(;p!=0;p=p->sibling)
childnums++;
return childnums;
}
int COperationFamilytree::InSiblingPos(Person pNode)
{
//本函數返回pNode結點在其兄弟中的排行
int pos=1;
Person p=pNode->parent;
if(p){
p=p->child;
for(;p!=pNode&&p!=0;p=p->sibling)
pos++;
}
return pos;
}
int COperationFamilytree::InGenerationPos(Person pNode)
{
//本函數返回pNode結點在第幾代
int pos=1;
Person p;
p=pNode->parent;
for(;p!=0;p=p->parent)
pos++;
return pos;
}
void COperationFamilytree::SortByBirthday(QuickSortNode *order)
{
//本函數對順序表order以出生日期的大小排序
int totalNums=0;
QuickSortNode* startaddr=order;
startaddr++;
GetPersonNums(T,totalNums);
CopyInfoFromBiTreeToArray(T,startaddr);
QuickSort(order,1,totalNums);
}
int COperationFamilytree::Partition(QuickSortNode *order, int low, int high)
{
//本函數供QuickSort函數調用
//交換順序表order中從low到high的記錄,便樞軸記錄到位,并返回其所在位置,此時
//在它之前(后)的記錄均不大(小)于它
order[0]=order[low]; //用子表的第一個記錄做樞軸記錄
Date pivotkey=order[low].birthday; //樞軸記錄關鍵字
while(low<high){ //從表的兩端交替地向中間掃描
while(low<high&&(CompareDate(order[high].birthday,pivotkey)==1
||CompareDate(order[high].birthday,pivotkey)==0))
--high;
order[low]=order[high]; //將比樞軸記錄小的記錄移到低端
order[low].birthday=order[high].birthday; //樞軸記錄到位
order[low].oneself=order[high].oneself;
while(low<high&&(CompareDate(order[low].birthday,pivotkey)==-1
||CompareDate(order[low].birthday,pivotkey)==0))
++low;
order[high]=order[low]; //將比樞軸記錄大的記錄移到高端
}
order[low]=order[0]; //樞軸記錄到位
return low; //返回樞軸位置
}
void COperationFamilytree::QuickSort(QuickSortNode *order, int low, int high)
{
//本函數對順序表order[low...high]作快速排序
int pivotloc;
if(low<high){ //長度大于1
pivotloc=Partition(order,low,high); //將order[low...high]一分為二
QuickSort(order,low,pivotloc-1); //對低子表遞歸排序,pivotloc是樞軸位置
QuickSort(order,pivotloc+1,high); //對高子表遞歸排序
}
}
void COperationFamilytree::GetPersonNums(Person&T,int& personNums)
{
//本函數返回以T為根結點的所有結點數,并把結果存入personNums中
//初始值personNums必須為0
if(T){
personNums++;
GetPersonNums(T->child,personNums); //遞歸調用
GetPersonNums(T->sibling,personNums);
}
}
void COperationFamilytree::CopyInfoFromBiTreeToArray(Person &T, QuickSortNode *&order)
{
//本函數先序遍歷以T為根結點的所有結點,并把每一個結點的出生日期信息及其指針值
//依次存入順序表order中
if(T){
(*order).birthday=T->info.birthday;
(*order).oneself=T;
order++;
CopyInfoFromBiTreeToArray(T->child,order);
CopyInfoFromBiTreeToArray(T->sibling,order);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -