?? people.cpp
字號(hào):
//文件people.cpp
#include"iostream.h"
#include"people.h"
#include"fstream.h"
#include<assert.h>
int people::psum=0;//靜態(tài)成員在類體外初始化
char &people::operator[](int k) //重載下標(biāo)運(yùn)算符
{
char m[10]={""};
assert(k>=0&&k<people::psum);
return m[k];
}
people::people()//無參構(gòu)造函數(shù),為指針分配內(nèi)存
{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)
//有參構(gòu)造函數(shù):初始化數(shù)據(jù)
{name=new char[strlen(na)+1];
strcpy(name,na);
id=new char[strlen(pid)+1];
strcpy(id,pid);
psum++; //統(tǒng)計(jì)人數(shù)
}
people::people(const people &init)//拷貝構(gòu)造函數(shù)
{delete[]name; //釋放內(nèi)存,便于后面負(fù)新值
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()//顯示數(shù)據(jù)函數(shù)
{cout<<"姓名:"<<name<<" "<<"編號(hào):"<<number<<" "<<"性別:"<<sex<<" "<<"身份證號(hào):"<<id<<endl;
birthday.print();
}
void people::input()//數(shù)據(jù)輸入函數(shù)
{cout<<"請(qǐng)輸入姓名:";cin>>name;
cout<<"請(qǐng)輸入編號(hào):";cin>>number;
cout<<"請(qǐng)輸入性別(男M,女W):";cin>>sex;
cout<<"請(qǐng)輸入身份證號(hào):";cin>>id;
birthday.input();
}
people &people::operator =(const people &peo) //賦值運(yùn)算符重載
{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; //實(shí)現(xiàn)可連續(xù)賦值
}
void search(people p[],int num,int n) //按編號(hào)查找函數(shù)
{int i,z;
z=num;
for(i=0;i<n;i++)
if(z==p[i].number)
{cout<<"您要找的對(duì)象有如下信息:"<<endl;
p[i].print();
cout<<"查找完畢!請(qǐng)繼續(xù)操作!"<<endl;
break;
}
if(i==n) cout<<"對(duì)不起,沒有找到合適的對(duì)象。"<<endl;
}
void sort(people p[],int n) //按編號(hào)從大到小排序,并將結(jié)果輸出到文件peopl.dat中及顯示在屏幕上
{int i,j;
people temp;//定義臨時(shí)對(duì)象
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<<"對(duì)people的排序完畢!!請(qǐng)繼續(xù)操作"<<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<<"編號(hào):"<<p[i].number<<endl;
ostrm<<"身份證號(hào)碼:"<<p[i].id<<endl;
}
}
int operator==(people a,people b) //==運(yùn)算符重載,判斷兩個(gè)people對(duì)象的id好是否相等
{if(!strcmp(a.getID(),b.getID())) return 1;//身份證號(hào)碼一樣,返回“真”
else return 0;
}
void people::set(char*na,int num,char*pid,char s,int y,int m,int d)//增加人員函數(shù),使得增加的信息在文件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<<"對(duì)不起!您輸入的編號(hào)出現(xiàn)重復(fù),請(qǐng)重輸入:";
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<<"編號(hào):"<<number<<endl;
ostrm<<"身份證號(hào)碼:"<<id<<endl;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -