个人笔记,这个是自己写的,未观看黑马教程,所以差异会比较大。
#include "iostream"
#include
using namespace std;
/**
* 通讯录管理系统
* 功能
* 1.添加联系人
* 2.显示联系人
* 3.删除联系人
* 4.查找联系人
* 5.修改联系人
* 6.清空联系人
* @return
*/
struct Phone {
string name;
int sex;
string phone;
string address;
};
void add(Phone * listPhone,int);
void show(Phone * listPhone,int);
void deletePhone(Phone * listPhone,int);
void searchPhone(Phone * listPhone,int);
void editPhone(Phone * listPhone,int);
void deleteAll(Phone * listPhone,int);
int main() {
struct Phone phoneList[100]={{"张工",1,"13519107598","咸阳秦都区"},{"马工",0,"13500000000","西安高新区"}};
while (true) {
system("cls");
cout << "******************" << endl;
cout << "***通讯录管理系统***" << endl;
cout << "***1.添加联系人***" << endl;
cout << "***2.显示联系人***" << endl;
cout << "***3.删除联系人***" << endl;
cout << "***4.查找联系人***" << endl;
cout << "***5.修改联系人***" << endl;
cout << "***6.清空联系人***" << endl;
cout << "******************" << endl;
cout << "请输入要执行的操作" << endl;
int cinData;
cin >> cinData;
//获取数组元素个数
int len = sizeof(phoneList) / sizeof(phoneList[0]);
if (cinData == 1) {
add(phoneList,len);
}
if (cinData == 2) {
show(phoneList,len);
}
if (cinData == 3) {
deletePhone(phoneList,len);
}
if (cinData == 4) {
searchPhone(phoneList,len);
}
if (cinData == 5) {
editPhone(phoneList,len);
}
if (cinData == 6) {
deleteAll(phoneList,len);
}
}
return 0;
}
// 1.添加联系人
void add(Phone * pPhone,int len) {
Phone newPhone;
cout << "请输入要添加联系人的姓名" << endl;
cin >> newPhone.name;
cout << "请输入要添加联系人的性别" << endl;
cout << "男=0" << endl;
cout << "女=1" << endl;
cin >> newPhone.sex;
cout << "请输入要添加联系人的电话" << endl;
cin >> newPhone.phone;
cout << "请输入要添加联系人的地址" << endl;
cin >> newPhone.address;
for(int i=0;i<len;i++,pPhone++){
if(pPhone->name.empty()){
*pPhone=newPhone;
cout<<"新用户添加成功"<<endl;
break;
}
}
cout << "按任意键继续" << endl;
getchar();
}
// 2.显示联系人
void show(Phone * pPhone,int len) {
cout<<"数组大小为:"<<len<<endl;
for(int i=0;i< len;i++,pPhone++){
if(pPhone->name.empty()){
cout<<"打印完成"<<endl;
break;
}
cout<<"第"<<i+1<<"个联系人为: name="<<pPhone->name<<", sex="<<pPhone->sex<<", phone="<<pPhone->phone<<", address="<<pPhone->address<<";"<<endl;
}
cout << "按任意键继续" << endl;
getch();
}
// 3.删除联系人
void deletePhone(Phone *pPhone,int len) {
string inName;
Phone newPhone;
cout<<"请输入要删除联系人的姓名:";
cin>>inName;
for(int i=0;i<len;i++,pPhone++){
if(pPhone->name.empty()){
cout<<"未查找到联系人"<<endl;
break;
}
if(pPhone->name==inName){
cout<<"第"<<i+1<<"个联系人为: name="<<pPhone->name<<", sex="<<pPhone->sex<<", phone="<<pPhone->phone<<", address="<<pPhone->address<<";"<<endl;
*pPhone=newPhone;
cout<<"第"<<i+1<<"个联系人为: name="<<pPhone->name<<", sex="<<pPhone->sex<<", phone="<<pPhone->phone<<", address="<<pPhone->address<<";"<<endl;
cout<<"删除成功"<<endl;
}
}
cout << "按任意键继续" << endl;
getch();
}
// 4.查找联系人
void searchPhone(Phone *pPhone,int len) {
string inName;
cout<<"请输入要查找联系人的姓名:";
cin>>inName;
for(int i=0;i<len;i++,pPhone++){
if(pPhone->name.empty()){
cout<<"未查找到联系人"<<endl;
break;
}
if(pPhone->name==inName){
cout<<"第"<<i+1<<"个联系人为: name="<<pPhone->name<<", sex="<<pPhone->sex<<", phone="<<pPhone->phone<<", address="<<pPhone->address<<";"<<endl;
break;
}
}
cout << "按任意键继续" << endl;
getch();
}
// 5.修改联系人
void editPhone(Phone *pPhone,int len) {
cout<<"输入要修改联系人的名字"<<endl;
string inName;
cin>>inName;
for(int i=0;i<len;i++,pPhone++){
if(pPhone->name.empty()){
cout<<"为找到联系人"<<endl;
}
if(pPhone->name==inName){
Phone newPhone;
cout << "请输入要添加联系人的姓名" << endl;
cin >> newPhone.name;
cout << "请输入要添加联系人的性别" << endl;
cout << "男=0" << endl;
cout << "女=1" << endl;
cin >> newPhone.sex;
cout << "请输入要添加联系人的电话" << endl;
cin >> newPhone.phone;
cout << "请输入要添加联系人的地址" << endl;
cin >> newPhone.address;
*pPhone=newPhone;
break;
}
}
cout << "按任意键继续" << endl;
getch();
}
// 6.清空联系人
void deleteAll(Phone *pPhone,int len) {
cout<<"确认要清空联系人吗?"<<endl;
cout<<"y=yes(确认清除);n=no(取消操作)"<<endl;
char inChar;
cin>>inChar;
if(inChar=='n'){
return;
}
if(inChar!='y'){
cout<<"未确定是否删除,退出"<<endl;
return;
}
if(inChar=='y'){
struct Phone newPhoneList[100];
for(int i=0;i<len;i++,pPhone++){
Phone newPhone;
*pPhone=newPhone;
}
cout<<"联系人列表清空成功!!!"<<endl;
}
cout << "按任意键继续" << endl;
getch();
}