//************************************************* //PersonnelManage.cpp //创建人:雷神 //日期:2002-8-30 //版本: //描述: //*************************************************
#include #include //基类,是此对象模型的最上层父类 class Personnel { friend class point_list; //用来实现输出链表,以及插入或删除人员的功能. protected: char serial_number[15];//编号 char name[10];//名称 char password[15]//口令 Personnel *pointer; Personnel *next_link; public: Personnel(char *sn,char *nm,char *pwd) { strcpy(serial_number,sn); strcpy(name,sm); strcpy(password,pwd); next_link=0; } Personnel() { serial_number[0]=NULL; name[0]=NULL; password[0]=NULL; next_link=0; } void fill_serial_number(char *p_n) { strcpy(serial_number,p_n); } void fill_name(char *p_nm) { strcpy(name,p_nm); } void fill_password(char *p_pwd) { strcpy(password,p_pwd); }
virtual void addnew(){} virtual void display() { cout<<"\n编号:"< cout<<"名字:"< cout<<"口令:"< } }; //下面是派生的子类,为了简单些我在把子类进行了成员简化。 //思路:由父类派生出成员子类,正式成员要求更详细的个人资料,这里省略了大部份. //并且正式成员可以有一些系统的操作权限,这里省略了大部份。 //正式成员子类 class Member:public Personnel { friend class point_list; private: char member_email[50]; char member_gender[10]; double member_age; public: Member(char *sn,char *nm,char *pwd,char *em,char *gd,double ag):Personnel(sn,nm,pwd) { strcpy(member_email,em); strcpy(member_gender,gd); member_age=age; } Member():Personnel() { member_email[0]=NULL; member_gender=NULL; member_age=0.0; } void fill_email(char *p_em) { strcpy(member_email,p_em); } void fill_gender(char *p_gd) { strcpy(member_gender,p_gd); } void fill_age(double ages) { member_age=ages; }
void addnew() { pointer=this; } void display() { Personnel::display() cout<<"电子邮件:"< cout<<"性别:"< cout<<"年龄"< } };
//好了,我们还需要实现一个超级成员子类和一个项目经理的子类. //这是超级成员类 class Supermember:public Member { friend class point_list; private: int sm_documentcount;//提交的文档数 int sm_codecount;//提交的代码段数 public: Supermember(char *sn,char *nm,char *pwd,char *em,char *gd,double ag,int dc,int cc):Member(sn,nm,pwd,gd,ag) { sm_documnetcount=0; sm_codecount=0; } Spupermember():Member() { sm_documentcount=0; sm_codecount=0; } void fill_documentcount(int smdc) { sm_documentcount=smdc; } void fill_codecount(int smcc) { sm_codecount=smcc; }
void addnew() { pointer=this; } void display() { Member::display() cout<<"提交文章数:"< cout<<"提交代码段数"< } };
//实现友元类 class point_list { private: Personnel *location; public: point_list() { location=0; } void print(); void insert(Personnel *node); void delete(char *serial_number); } //显示链表 void point_list::print() { Personnel *ps=location; while(ps!=0) { ps->display(); ps=ps->next_link; } } //插入链表 void point_list::insert(Personnel *node) { Personnel *current_node=location; Personnel *previous_node=0; while(current_node!=0 && (strcmp(current_node->name,node->name<0) { previous_node=current_node; current_node=current_node->next_link; } node->addnew() node->pointer->next_link=current_node; if(previous_node==0) location=node->pointer; else previous_node->next_link=node->pointer; }
//从链表中删除 void point_list::delete(char *serial_number) { Personnel *current_node=location; Personnel *previous_node=0; while(current_node!=0 && strcmp(current_node->serial_number,serial_number)!=0) { previous_node=current_node; current_node=current_node->next_link; } if(current_node !=0 && previous_node==0) { location=current_node->next_link; } else if(current_node !=0 && previous_node!=0) { previous_node->next_link=current_node->next_link; } }
//这是主函数,我们显式的增加3个Supermember信息,然后在通过编号删除一个 //我们没有从成员再派生出管理成员,所以没有办法演示它,但我们可以看出要实现它并不难 //注意:此程序没有经过验证,也许会有BUG. main() { point_list pl; Supermember sm1("000000000000001","雷神","123456","lsmodel@ai361.com","男",29.9,10,10); Supermember sm1("000000000000002","木一","234567","MY@ai361.com","男",26.5,20,5); Supermember sm1("000000000000003","落叶夏日","345678","LYXR@ai361.com","男",24.8,5,15); //如果我们还派生了管理人员,可能的方式如下: //Managemember mm1("000000000000004","ADMIN","888888","webmaster@ai361.com","男",30,5,15,......);
//下面是将上面的3个人员信息加到链表中 pl.insert(&sm1); pl.insert(&sm2); pl.insert(&sm3); //对应管理人员的 pl.insert(&mm1);
//下面是显示他们 //下面是显示人员列表 pl.print();
//下面是删除一个人员信息 pl.delete("000000000000001"); //我们再显示一次看看. cout<<"\n删除后的列表:\n"; pl.print(); } |