⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 people.cpp

📁 人事管理系统是课程设计
💻 CPP
字号:
//文件people.cpp

#include"iostream.h"
#include"people.h"
#include"fstream.h"
#include<assert.h>
int people::psum=0;//静态成员在类体外初始化

char &people::operator[](int k)  //重载下标运算符
{
    char m[10]={""};
	assert(k>=0&&k<people::psum);
	return m[k];
}

people::people()//无参构造函数,为指针分配内存
{name=new char[9];
 id =new char[10];
}
people::people(char*na,int num,char*pid,char s,int y,int m,int d):birthday(y,m,d),number(num),sex(s)
//有参构造函数:初始化数据
{name=new char[strlen(na)+1];
 strcpy(name,na);
 id=new char[strlen(pid)+1];
 strcpy(id,pid);
 psum++; //统计人数
}
people::people(const people &init)//拷贝构造函数
{delete[]name; //释放内存,便于后面负新值
 delete[]id;
 name=new char[strlen(init.name)+1];
 strcpy(name,init.name);
 id=new char[strlen(init.id)+1];
 strcpy(id,init.id);
 number=init.number;
 birthday=init.birthday;
}
void people::print()//显示数据函数
{cout<<"姓名:"<<name<<"   "<<"编号:"<<number<<"   "<<"性别:"<<sex<<"  "<<"身份证号:"<<id<<endl;
birthday.print();
}
void people::input()//数据输入函数
{cout<<"请输入姓名:";cin>>name;
 cout<<"请输入编号:";cin>>number;
 cout<<"请输入性别(男M,女W):";cin>>sex;
 cout<<"请输入身份证号:";cin>>id;
 birthday.input();
} 
people &people::operator =(const people &peo) //赋值运算符重载
{if(&peo!=this)     //防止自身赋值
 delete[]name;
 delete[]id;
 name=new char[strlen(peo.name)+1];
 strcpy(name,peo.name);
 id=new char[strlen(peo.id)+1];
 strcpy(id,peo.id);
 number=peo.number;
 birthday=peo.birthday;
 return *this;   //实现可连续赋值
}


void search(people p[],int num,int n)  //按编号查找函数
{int i,z;
  z=num;
	 for(i=0;i<n;i++)
		 if(z==p[i].number)
		 {cout<<"您要找的对象有如下信息:"<<endl;
		   p[i].print();
		cout<<"查找完毕!请继续操作!"<<endl;
		   break;
		 }
 if(i==n) cout<<"对不起,没有找到合适的对象。"<<endl;
 }


void sort(people p[],int n)  //按编号从大到小排序,并将结果输出到文件peopl.dat中及显示在屏幕上
{int i,j;
 people temp;//定义临时对象
 for(j=1;j<=n-1;j++)
	 for(i=0;i<=n-1-j;i++)
		 if(p[i].number>p[i+1].number)
		 {temp=p[i];p[i]=p[i+1];p[i+1]=temp;}
  for(i=0;i<n;i++)
  {p[i].print();}
  cout<<"对people的排序完毕!!请继续操作"<<endl;
 ofstream ostrm;
 ostrm.open("people.dat");
 if(!ostrm)
 {cout<<"people.dat can't open.\n";
 }
 for( i=0;i<n;i++)
{ostrm<<"人员信息有如下:"<<endl;
 ostrm<<"\n";
ostrm<<"姓名:"<<p[i].name<<endl;
ostrm<<"编号:"<<p[i].number<<endl;
ostrm<<"身份证号码:"<<p[i].id<<endl;
}
}
 int operator==(people a,people b) //==运算符重载,判断两个people对象的id好是否相等
{if(!strcmp(a.getID(),b.getID())) return 1;//身份证号码一样,返回“真”
 else return 0;
}

 void people::set(char*na,int num,char*pid,char s,int y,int m,int d)//增加人员函数,使得增加的信息在文件people.dat尾部输出
{
  psum++;
 name=new char[strlen(na)+1];
 strcpy(name,na);
id=new char[strlen(pid)+1];
/*for(int i=0;i<4;i++)
if(p[i].id==pid)
{cout<<"对不起!您输入的编号出现重复,请重输入:";
 cin>>pid;
 delete[]id;
 id=new char[strlen(pid)+1];break;
}*/
 strcpy(id,pid);
 number=num;
 sex=s;
 birthday.set(y,m,d);
 
ofstream ostrm;
ostrm.open("people.dat",ios::app);//将添加的人员信息加在文件尾部
ostrm<<endl;
ostrm<<"姓名:"<<name<<endl;
ostrm<<"编号:"<<number<<endl;
ostrm<<"身份证号码:"<<id<<endl;
}
         

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -