经个位高手指点,终于可以实现部分功能了,谢谢各位,可是还是会出现错误报告的对话框,在输出男女职工人数时会出现一长串数字,不知道怎么回事.而且不会输出被删除的职工链表,即函数del12的功能实现不了,原本是想让此函数实现删除大于60岁的男性职工和大于55岁的女性职工的,可是却实现不了,怎么回事?能帮我改改吗?谢谢各位了!
#include <iostream.h>
#include<string.h>
class worker
{public:
worker(int x,int y, char * z)
{number=x;age=y;sex=new char[10];strcpy(sex,z);};
~worker(){};
int Getnum(){return number;}
int Getage(){return age;}
char *Getsex(){return sex;}
void friend showlist(worker*&f);
void friend del1(worker*&f,int key);
void friend del2(worker*&f);
void friend f2(worker*&f);
void friend f1(worker*&f,worker*&r,int x,int y,char z);
worker*next;
private:
int number;
int age;
char* sex;
};
void f1(worker*&f,worker*&r,int x,int y,char* z)
{worker*p=new worker(x,y,z);
p->next=NULL;
if(f==NULL) {f=r=p;}
else if(r==NULL)
r=p;
else
{
r->next=p;
r=r->next;
}
}
void showlist(worker*&f)
{worker*p1;
for(p1=f;p1!=NULL;p1=p1->next)
{cout<<p1->number<<" "<<p1->age<<" "<<p1->sex<<endl;}
}
void del1(worker*&f,int key)
{worker*p2;
worker*p3;
if(!f)
{cout<<"no worker!\n";return;}
if(f->number==key)
{p2=f;
f=f->next;
delete p2;
p2=NULL;
cout<<key<<"have been deleted\n";
return;
}
for(p3=f;p3!=NULL;p3=p3->next)
{if(p3->next->number==key)
{p2=p3->next;
p3->next=p2->next;
delete p2;
p2=NULL;
cout<<key<<"have been deleted\n";
return;
}
}
cout<<"there is not key:"<<key<<endl;
return;
}
void del2(worker*&f)
{worker*p4;
worker *p5;
worker *head;worker *r1;
head=new worker(0,0,NULL);
if(!f)
{cout<<"no worker!\n";
return;
}
if(f->sex=="man"&&f->age>=60||f->sex=="woman"&&f->age>=55)
{p4=f;
f=f->next;
delete p4;
f1(head,r1,p4->number,p4->age,p4->sex);
p4=NULL;}
for(p5=f;p5->next!=NULL;p5=p5->next)
{if (p5->next->sex=="man"&&p5->next->age>=60||p5->next->sex=="woman"&&p5->next->age>=55)
p4=p5->next;
p5->next=p4->next;
delete p4;
f1(head,r1,p4->number,p4->age,p4->sex);
p4=NULL;
return;
}
}
void f2(worker*&f)
{worker*p6;
int a,b;
for(p6=f;p6!=NULL;p6=p6->next)
{if(!strcmp(p6->sex,"man"))
a++;
if(!strcmp(p6->sex,"woman"))
b++;
}
cout<<"共有男性职工人数为:"<<a<<endl;
cout<<"共有女性职工人数为:"<<b<<endl;
}
void main()
{worker*front=NULL;worker *rear=NULL;worker*head=NULL;worker*r1=NULL;
int x,y,choice,key;
char *z=new char[10];
do
{cout<<"请选择:\n";
cout<<"输入1继续\n输入0结束\n";
cin>>choice;
switch(choice)
{case 1:{cout<<"输入学号:";
cin>>x;
cout<<"输入年龄:";
cin>>y;
cout<<"输入性别:";
cin>>z;
f1(front,rear,x,y,z);
break;}
case 0:break;
}
}while(choice);
f2(front);
cout<<"请输入所要删除职工的编号:";
cin>>key;
del1(front,key);
f2(front);
del2(front);
f2(front);
showlist(head);
}