为什么运行的时候老是运行不了,出现要不要发送错误报告的对话框,编译的时候又没有错误?怎么解决?以下是原代码:
#include <iostream.h>
#include<string.h>
class worker
{public:
worker(int x,int y, char * z)
{number=x;age=y;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{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;
f1(head,r1,p4->number,p4->age,p4->sex);
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;
f1(head,r1,p4->number,p4->age,p4->sex);
delete p4;
p4=NULL;
return;
}
}
}
void f2(worker*&f)
{worker*p6;
int a,b;
for(p6=f;p6!=NULL;p6=p6->next)
{if(p6->sex=="man")
a++;
if(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);
}