设计并实现简单通讯录系统
1.定义描述联系人信息的类(数据类)
数据成员:身份证号、姓名、手机号(用字符串数组,可能不止一个)、QQ号、微信号、住址;成员函数:构造函数若干、get/set函数、显示函数。
2.定义功能实现类(操作类)
数据成员:联系人数组、联系人数量、数组容量;成员函数:
构造函数(完成对本类数据成员的初始化);增加联系人(在尾部增加);
按身份证号查询联系人并输出找到的联系人信息。找不到,不做任何操作;按身份证号修改联系人信息(找到,修改全部其他数据。否则,不做任何操作);按身份证号删除联系人信息;
按“姓”(如姓李)查找所有满足条件的联系人并逐一输出;析构函数(可不要)
注意:
1.不要菜单,不要任何提示信息。只输出要求的结果;
2.对每个成员函数均进行测试(调用)。包括数据类的成员函数。
//vector数组
#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class people//人员类
{
string name;
string id;
string phone;
string address;
friend ostream& operator<<(ostream& out, people& p);
public:
people(string name,string id,string phone,string address);
people();
void setname(string name);
string getname();
void setid(string id);
string getid();
void setphone(string phone);
string getphone();
void setaddress(string address);
string getaddress();
};
people::people(string name,string id,string phone,string address)
{
this->name=name;
this->id=id;
this->phone=phone;
this->address=address;
}
people::people()
{
this->name="";
this->id="";
this->phone="";
this->address="";
}
ostream& operator<<(ostream& out, people& p)//全局函数实现左移重载,成员函数实现不了 p << cout 不是我们想要的效果。
{
out<<p.getname()<<" "<<p.getid()<<" "<<p.getphone()<<" "<<p.getaddress();
return out;//可在后面追加 如:endl 。若不加return main里面输出不能追加cout
}
void people::setname(string name)
{
this->name=name;
}
string people::getname()
{
return name;
}
void people::setid(string id)
{
this->id=id;
}
string people::getid()
{
return id;
}
void people::setphone(string phone)
{
this->phone=phone;
}
string people::getphone()
{
return phone;
}
void people::setaddress(string address)
{
this->address=address;
}
string people::getaddress()
{
return address;
}
class information//操作对象类
{
vector<people> v;//vector数组里面存放people数组
public:
information(vector<people>& v);
void printAll();//打印全部
void query(string id);//通过d查询此人是否存在,存在则打印
void change(string id1,string name,string id2,string phone,string address);//通过id1查询人员,后面为修改的信息内容
};
information::information(vector<people>& v)
{
this->v=v;
}
void information::printAll()
{
for(vector<people>::iterator it=this->v.begin();it!=this->v.end();it++)
{
cout<<(*it)<<endl;
}
}
void information::query(string id)
{
for(vector<people>::iterator it=this->v.begin();it!=this->v.end();it++)
{
if((*it).getid()==id)
{
cout<<(*it);
}
}
}
void information::change(string id1,string name,string id,string phone,string address)
{
for(vector<people>::iterator it=this->v.begin();it!=this->v.end();it++)
{
if((*it).getid()==id1)
{
(*it).setname(name);
(*it).setid(id);
(*it).setphone(phone);
(*it).setaddress(address);
}
}
}
int main()
{
vector<people> v;
// string name,id,phone,address;
// cout<<"请问几个联系人:"<<endl;
// int n;cin>>n;
// for(int i=0;i<n;i++)
// {
// cout<<"请输入姓名:"<<endl;cin>>name;
// cout<<"请输入ID:"<<endl;cin>>id;
// cout<<"请输入手机号:"<<endl;cin>>phone;
// cout<<"请输入地址:"<<endl;cin>>address;
// people a(name,id,phone,address);
// v.push_back(a);
// }
people p1("ZhangSan","1","138000","BeiJing");
people p2("LiSi","2","159000","ShangHai");
people p3("WangWu","3","176000","ChengDu");
people p4("ZhaoLiu","4","178000","TianJin");
people p5("ChenQi","5","188000","KaiFeng");
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
information m(v);
cout<<"修改前为:"<<endl;
m.printAll();
cout<<endl;
cout<<"是否有ID为3的人:"<<endl;
m.query("3");
cout<<endl;
cout<<"修改2号人员!"<<endl;
m.change("2","HuBa","10","1980000","Harbin");
cout<<endl;
cout<<"修改后为:"<<endl;
m.printAll();
}
//普通数组
#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
class information
{
int QQ;
string name;
string WeChat;
string address;
string phone;
public:
int ID;
int PhoneNum = 0;
information(int ID, string name, int QQ, string WeChat, string address, string phone);
information();
void setID(int ID);
int getID();
void setQQ(int QQ);
int getQQ();
void setname(string name);
string getname();
void setWeChat(string WeChat);
string getWeChat();
void setaddress(string address);
string getaddress();
void setphone(string phone);
string getphone();
};
information::information(int ID, string name, int QQ, string WeChat, string address, string phone)
{
this->ID = ID;
this->name = name;
this->QQ = QQ;
this->WeChat = WeChat;
this->address = address;
this->phone = phone;
}
information::information()
{
this->ID = 0;
this->name = "";
this->QQ = 0;
this->WeChat = "";
this->address = "";
this->phone = "";
}
void information::setID(int ID)
{
this->ID = ID;
}
int information::getID()
{
return this->ID;
}
void information::setQQ(int QQ)
{
this->QQ = QQ;
}
int information::getQQ()
{
return this->QQ;
}
void information::setname(string name)
{
this->name = name;
}
string information::getname()
{
return name;
}
void information::setWeChat(string WeChat)
{
this->WeChat = WeChat;
}
string information::getWeChat()
{
return WeChat;
}
void information::setaddress(string address)
{
this->address = address;
}
string information::getaddress()
{
return address;
}
void information::setphone(string x)
{
this->phone = x;
}
string information::getphone()
{
return phone;
}
class contacts
{
int Size = 0;
int Capacity = 0;
public:
information p[1000];
contacts(int Capacity);
contacts();
int getSize();
void add_last(information x);
void query_ID(int ID);
void change_ID(int ID1,int ID2, string name, int QQ, string WeChat, string address, string phone);
void delete_ID(int ID);
void query_SurnameID(string surname);
void PrintAll();
};
contacts::contacts(int Capacity)
{
this->Capacity = Capacity;
this->Size = 0;
}
contacts::contacts()
{
this->Capacity = 10;
this->Size = 0;
}
int contacts::getSize()
{
return this->Size;
}
void contacts::add_last(information x)
{
if (this->Size == this->Capacity)
{
return;
}
p[this->Size] = x;
this->Size++;
}
void contacts::query_ID(int ID)
{
for (int i = 0; i < this->Size; i++)
{
if (this->p[i].getID() == ID && p[i].getID() != 0)
{
cout << p[i].getID() << " " << p[i].getname() << " " << p[i].getQQ() << " " << p[i].getWeChat() << " " << p[i].getaddress() << " " << p[i].getphone() << endl;
}
}
}
void contacts::change_ID(int ID1,int ID2, string name, int QQ, string WeChat, string address, string phone)
{
for (int i = 0; i < this->Size; i++)
{
if (this->p[i].getID() == ID1)
{
this->p[i].setID(ID2);
this->p[i].setname(name);
this->p[i].setQQ(QQ);
this->p[i].setWeChat(WeChat);
this->p[i].setaddress(address);
this->p[i].setphone(phone);
}
}
}
void contacts::delete_ID(int ID)
{
for (int i = 0; i < this->Size; i++)
{
if (this->p[i].getID() == ID)
{
this->p[i].ID = 0;
}
}
}
void contacts::query_SurnameID(string surname)
{
for (int i = 0; i < this->Size; i++)
{
int flage = 0;
for (int j = 0; j < surname.length(); j++)
{
if (surname[j] != p[i].getname()[j])
{
flage = 1;
}
}
if (flage == 0)
{
cout << p[i].getID() << " " << p[i].getname() << " " << p[i].getQQ() << " " << p[i].getWeChat() << " " << p[i].getaddress() << " " << p[i].getphone() << endl;
}
}
}
void contacts::PrintAll()
{
for (int i = 0; i < this->Size; i++)
{
if (p[i].getID() != 0)
{
cout << p[i].getID() << " " << p[i].getname() << " " << p[i].getQQ() << " " << p[i].getWeChat() << " " << p[i].getaddress() << " " << p[i].getphone() << endl;
}
}
}
int main()
{
contacts a(100);
a.add_last(a.p[0]); a.add_last(a.p[1]); a.add_last(a.p[2]); a.add_last(a.p[3]); a.add_last(a.p[4]); a.add_last(a.p[5]);
a.p[0].setID(1); a.p[0].setname("ZhangSan"); a.p[0].setQQ(1234567); a.p[0].setWeChat("zhangsan123"); a.p[0].setaddress("BeiJing"); a.p[0].setphone("1380000 1590000");
a.p[1].setID(2); a.p[1].setname("LiSi"); a.p[1].setQQ(2345678); a.p[1].setWeChat("lisi123"); a.p[1].setaddress("TianJin"); a.p[1].setphone("1590000");
a.p[2].setID(3); a.p[2].setname("WangWu"); a.p[2].setQQ(3456789); a.p[2].setWeChat("wangwu123"); a.p[2].setaddress("ShangHai"); a.p[2].setphone("1860000");
a.p[3].setID(4); a.p[3].setname("ZhaoLiu"); a.p[3].setQQ(88488); a.p[3].setWeChat("zhaoliu123"); a.p[3].setaddress("ChongQing"); a.p[3].setphone("1760000");
a.p[4].setID(5); a.p[4].setname("LiQi"); a.p[4].setQQ(66466); a.p[4].setWeChat("liqi123"); a.p[4].setaddress("ShanDong"); a.p[4].setphone("1580000 13800000");
a.p[5].setID(6); a.p[5].setname("HuBa"); a.p[5].setQQ(111111); a.p[5].setWeChat("huba123"); a.p[5].setaddress("HeNan"); a.p[5].setphone("1720000 1220000 1550000");
cout << a.p[0].getID() << endl;
cout << a.p[0].getname() << endl;
cout << a.p[0].getQQ() << endl;
cout << a.p[0].getWeChat() << endl;
cout << a.p[0].getaddress() << endl;
cout << a.p[0].getphone() << endl;
cout << endl;
a.PrintAll();
cout << endl;
a.query_ID(3);
cout << endl;
a.change_ID(3, 9,"ChenJiu",999999999,"ChenJiu123","JiangSu","15600000");
a.delete_ID(6);
a.PrintAll();
cout << endl;
a.query_SurnameID("Li");
}