一個簡單的學生成績管理程式(二叉樹實現) (轉)

themoney發表於2007-10-03
一個簡單的學生成績管理程式(二叉樹實現) (轉)[@more@]

#include
#include
struct feng  //記錄分數
{
 int pshuxue;
 int penglish;
 int pchinese;
 int count;
}total;
class stunode  //子節點
{
 private:
 int  id;
 string name;
 int  shuxue;
 int  english;
 int  chinese; 
 stunode  *left;  //左節點
 stunode  *right;  //右節點
 public:
 stunode(void); //構造
 stunode(int sid,string sname,int sshuxue,int senglish,int schinese); 
 ~stunode(void);
 void insertleft(stunode* newnode); 
 void insertright(stunode* newnode);
 stunode* returnleft(void);
 stunode* returnright(void);
 int  getid(void);
 void setname(string nwname)
 {name=nwname;}
 void setshuxue(int ssx){shuxue=ssx;}
 void setenglish(int se){english=se;}
 void setchinese(int sc){chinese=sc;}
 friend void printnode(stunode* head);  //列印學生分數
 friend void chengji(stunode* head); //計算平均成績
};
stunode::stunode(void)
{
  left=NULL;
 right=NULL;
}
stunode::stunode(int sid,string sname,int sshuxue,int senglish,int schinese)
{
 id=sid;
 name=sname;
 shuxue=sshuxue;
 english=senglish;
 chinese=schinese;
 left=NULL;
 right=NULL;
}
stunode::~stunode(void)
{
  left=NULL;
 right=NULL;
}
void stunode::insertleft(stunode* newnode)
{
  left=newnode; 
}
void stunode::insertright(stunode* newnode)
{
  right=newnode;
}
stunode* stunode::returnleft(void)
{
 return left;
}
stunode* stunode::returnright(void)
{
 return right;
}
int stunode::getid(void)
{
 return id;
}
void printnode(stunode *head)
{
 cout<id<name<shuxue<english<chinese<}
void pingchengji(stunode *head)
{
 total.pshuxue=total.pshuxue+head->shuxue;
 total.penglish=total.penglish+head->english;
 total.pchinese=total.pchinese+head->chinese;
 total.count=total.count+1;
}
class stutree   //二叉樹類
{
 private:
 stunode *,*newnode,*currnode,*parent;
 void print(stunode* temp);
 void zonghe(stunode* temp);
 int  ischild(stunode* pnode,stunode* childnode);  
 stunode* findnode(const int rid,stunode*& parentnode);
 public:
 stutree(void);
 void creattree(stunode *node);
 void prints(void);
 stunode* findnode(const int rid);
 void insertnode(stunode* temp);
 void pingjun(void);
 stunode* deletenode(int did);
 void deletetree(stunode* temp);
 ~stutree(void);
 
};
stutree::stutree(void)
{
  root=NULL;
  newnode=NULL;
  currnode=NULL;
  parent=NULL;
}
void stutree::deletetree(stunode* temp)
{
  if(temp!=NULL)
  {
  deletetree(temp->returnleft()); 
  deletetree(temp->returnright());
  delete temp; 
  }
}
stutree::~stutree(void)
{
  deletetree(root);
}
void stutree::creattree(stunode *node)   //建立二叉樹
{
  if(root==NULL)
 {
  root=node;
 }
 else
 {
 if(node->getid()getid())
 {
  parent=root;
    currnode=root->returnleft(); 
 }
 if(node->getid()>root->getid())
 {
  parent=root;
    currnode=root->returnright(); 
 }
 if(node->getid()==root->getid())
  {
  cout<   return; 
  }
 while(currnode!=NULL)
  {
 
 if(node->getid()==currnode->getid())
  {
  cout<   return; 
  }
  if(node->getid()getid())
 {
  parent=currnode;
  currnode=parent->returnleft();
 } 
  else
  {
  parent=currnode;
  currnode=parent->returnright();
  } 
  }
 if(node->getid()getid())
 {
 parent->insertleft(node);
 currnode=node;
 }
 if(node->getid()>parent->getid())
 {
 parent->insertright(node);
 currnode=node;
 }
  }
}
void stutree::print(stunode* temp)
{
  if(temp!=NULL)
  { 
  print(temp->returnleft());
  printnode(temp);
  print(temp->returnright());
  }
}
void stutree::zonghe(stunode* temp)
{
  if(temp!=NULL)
  {
  zonghe(temp->returnleft()); 
  pingchengji(temp);
  zonghe(temp->returnright());
  } 
}
void stutree::prints(void)
{
  print(root);
}
stunode* stutree::findnode(const int rid)
{
 if(root->getid()==rid)
 {
  return root;
 }
 if(ridgetid())
 {
  parent=root;
  currnode=root->returnleft();
 }
 if(rid>root->getid())
 {
  parent=root;
  currnode=root->returnright();
 }
 while(currnode!=NULL)
 {
  if(currnode->getid()==rid)
   {
   return currnode;
  } 
  if(ridgetid())
  {
  parent=currnode;
 currnode=currnode->returnleft();
  }
  else if(rid>currnode->getid())
  {
  parent=currnode;
 currnode=currnode->returnright();
  } 
 }
 return NULL;
}

stunode* stutree::findnode(const int rid,stunode*& parentnode)
{
 if(root->getid()==rid)
 {
  parentnode=NULL; 
  return root;
 }
 if(ridgetid())
 {
  parent=root;
  currnode=root->returnleft();
 }
 if(rid>root->getid())
 {
  parent=root;
  currnode=root->returnright();
 }
 while(currnode!=NULL)
 {
  if(currnode->getid()==rid)
   {
 parentnode=parent;
   return currnode;
  } 
  if(ridgetid())
  {
  parent=currnode;
 currnode=currnode->returnleft();
  }
  else if(rid>currnode->getid())
  {
  parent=currnode;
 currnode=currnode->returnright();
  } 
 }
}
void stutree::insertnode(stunode* temp)
{
 creattree(temp);
}
void stutree::pingjun(void)
{
  zonghe(root);
  cout<  cout<  cout<}
int  stutree::ischild(stunode* pnode,stunode* childnode)
{  //判斷父節點和子節點的位子關係
 if(pnode==NULL)
  {
  return -1; 
  }
 if(pnode->returnleft()==childnode)
 {
 return 1;
  }
 if(pnode->returnright()==childnode)
 {
 return 0;
 }
}
stunode* stutree::deletenode(int did)  //刪除節點
{
  stunode *parentnode,*temp;
  stunode *leftnode,*rightnode;
  stunode *minxnode,*minright,*parenode=NULL,*renode;
  int postion;
  temp=findnode(did,parentnode);  //temp是要刪除的節點
    if(temp==NULL)
  {
  cout< }
  postion=ischild(parentnode,temp);
  leftnode=temp->returnleft();
  rightnode=temp->returnright();
  if(leftnode==NULL && rightnode==NULL)  //temp左右節點為空
  {
 switch(postion)
 {
  case 1:
  parentnode->insertleft(NULL);
    break;
  case 0:
  parentnode->insertright(NULL);
  break;
  case -1:
  break; 
  } 
  delete temp;
  return root;
  }
  if(leftnode!=NULL && rightnode==NULL) //有左節點沒有右節點
  {
  switch(postion)
  {
  case 1:
  parentnode->insertleft(leftnode);
    break;
  case 0:
  parentnode->insertright(leftnode);
  break;
  case -1:
  root=root->returnleft();
 break;
  }
  delete temp;
  return root;
  } 
  if(leftnode==NULL && rightnode!=NULL)  有右節點沒有左節點
  {
  switch(postion)
  {
  case 1:
  parentnode->insertleft(rightnode);
 break;
  case 0: 
  parentnode->insertright(rightnode);
 break;
  case -1:
  root=root->returnright();
  break; 
  } 
   delete temp;
 return root;
 }
  if(leftnode!=NULL && rightnode!=NULL) 左右節點都有
  {
  parenode=rightnode;
  renode=rightnode;  //renode是替換的節點
  while(renode->returnleft()!=NULL) //查詢替換的節點
  { //被刪除節點右子樹中最小的節點就是我們要找的替換節點
 parenode=renode;//也可以查詢被刪除節點左子樹中最大的節點
 renode=renode->returnleft();
  } 
  if(renode==rightnode)  //一種情況是:替換節點就是temp的右兒子
  {
  rightnode->insertleft(leftnode); 
  }
  else{  //如果不是右兒子就如下操作 
  parenode->insertleft(renode->returnright());
  renode->insertleft(leftnode);
  renode->insertright(rightnode);
  } 
  switch(postion)
  {
  case -1:   
  root=renode;
  break;
  case 1:
  parentnode->insertleft(renode);
  break;
  case 0:
  parentnode->insertright(renode); 
    break;
  }
   delete temp;
  return root; 
  } 
 
  }
void main(void)
{
 int ,nid,nshuxue,nenglish,nchinese;
 string nname,space;
 stunode* newstudent,*findrezult;
 stutree  stulist;
 int wid,deid;
 for(;;)
 {
 cout<creat a chengji list"< cout<print a list"< cout<find student"< cout<insert a student"< cout<ping jun cheng ji"< cout<delete a student"< cout<chang student information"< cout<exit"< cin>>select;
 switch(select)
 {
  case 1:
  cout<    cin>>nid;
  getline(cin,space,'n');
  cout<  getline(cin,nname,'n');
  cout<  cin>>nshuxue;
  cout<  cin>>nenglish;
  cout<  cin>>nchinese;
  newstudent=new stunode(nid,nname,nshuxue,nenglish,nchinese);  
  if(newstudent==NULL)
  {
 cout< exit(1);
  }
  stulist.creattree(newstudent);
  break;
  case 2:
  stulist.prints();
  break;
  case 3:
  cout<  cin>>wid;
  findrezult=stulist.findnode(wid);
  if(findrezult!=NULL)
  {
  printnode(findrezult);
  }
  else
  {
  cout<  }
  break;
  case 4: 
  cout<    cin>>nid;
  getline(cin,space,'n');
  cout<  getline(cin,nname,'n');
  cout<  cin>>nshuxue;
  cout<  cin>>nenglish;
  cout<  cin>>nchinese;
  newstudent=new stunode(nid,nname,nshuxue,nenglish,nchinese);  
  if(newstudent==NULL)
  {
 cout< exit(1);
  }
  stulist.insertnode(newstudent);
  break;
  case 5:
  stulist.pingjun();
  break;
  case 6:
  cout<  cin>>deid;
  stulist.deletenode(deid);
  break;
  case 7:
  cout<  cin>>nid;
  findrezult=stulist.findnode(nid);
  cout<  cout<  cout<  cout<  cin>>select;
  switch(select)
  {
  case 20:
  getline(cin,space,'n');
  cout<  getline(cin,nname,'n');
  findrezult->setname(nname);
  break;
  case 21:
  cout<  cin>>nshuxue;
  findrezult->setshuxue(nshuxue);
  break;
  case 22: 
  cout<  cin>>nenglish;
  findrezult->setenglish(nenglish);
  break;
  case 23:
  cout<  cin>>nchinese;
  findrezult->setchinese(nchinese);
  break;
  }
  break;
  case 0:
  exit(0);
  }
  }
}
本在環境下透過.程式遵守GPL.
參考書目:
 <> 作者:William Ford  William Topp著 清華大學初版社
 <> 作者:黃國瑜 葉乃菁 清華大學出版社

請大家斧正

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10794571/viewspace-974633/,如需轉載,請註明出處,否則將追究法律責任。

相關文章