c++学习中!🐵
通讯录管理系统的实现,必要的就是添加联系人,修改联系人,删除联系人,查找联系人,清空联系人,显示联系人。
目录
需要注意的是,结构体的设计。先说下我自己的感受,当我开始写结构体,是把联系人信息,和联系人个数size放在了一个结构体中,我写完最后一个排序接口的的时候,bug就出现了。 因为我是用这个结构体创建的数组,所以每个数组的成员中都有一个size,再我添加联系人的时候,每次的size++都是在首元素中的size++,其他数组中数据中size都是乱码。而我用sort排序时,数组中内容数据的顺序会发生改变,这样我就找不到我原来的那个size。
改进:再使用一个结构体,存放联系人数组,和size。这样再添加联系人的时候,只需要把这个结构体创建的变量传过去,在联系人数组中添加,size++。这时候size和联系人信息的存放是互相独立的,所以不管怎么排序,它的size是不会有影响的。
结构体的设计😊
typedef struct peopleinfo
{
string name;
string sex;
int age;
string phone;
string loca;
}peoinfo;
typedef struct contact
{
peopleinfo infor[1000];//联系人数组
int size;//记录联系人个数
}contact;
所有的接口😆
//菜单
void menu();
//添加
void AddContact(contact* pf, int num);
//初始化
void InitContact(contact* pf);
//打印
void PrintContact(contact* pf);
//删除
void DelContact(contact* pf);
//查找
int FindContact(contact* pf, string name);
//打印查找联系人信息
void ContAch(contact* pf, int tmp);
//修改联系人
void ModiContact(contact* pf, string name);
//清空联系人
void EmptyContact(contact* pf);
// 排序
void ContactSort(contact* pf);
接口的实现😯(有详细注释)
void ContAch(contact* pf, int tmp)//打印查找联系人
{
if (tmp != -1)
{
cout << "找到了您查找的联系人" << endl;
cout << "姓名" << "\t" << "性别" << "\t" << "年龄" << "\t" << "电话" << "\t" << "家庭住址" << endl;
cout << pf->infor[tmp].name << "\t" << pf->infor[tmp].sex << "\t" << pf->infor[tmp].age << "\t" << pf->infor[tmp].phone << "\t" << pf->infor[tmp].loca << endl;
}
else
{
cout << "抱歉!没有您要查找的联系人" << endl;
}
}
void menu()
{
cout << "********************************" << endl;
cout << "**** 1.Add 2.Show ****" << endl;
cout << "**** 3.Del 4.Find ****" << endl;
cout << "**** 5Modi 6.Empty ****" << endl;
cout << "**** 7.Sort 0.exit ****" << endl;
cout << "********************************" << endl;
}
void InitContact(contact* pf)
{
pf->size = 0;//初始化联系人个数为0
}
void AddContact(contact* pf, int num)
{
if (pf->size < num)
{
cout << "请输入年龄:" << endl;
cin >> pf->infor[pf->size].age;
cout << "请输入地址:" << endl;
cin >> pf->infor[pf->size].loca;
cout << "请输入名字:" << endl;
cin >> pf->infor[pf->size].name;
cout << "请输入电话:" << endl;
cin >> pf->infor[pf->size].phone;
cout << "请输入性别:" << endl;
cin >> pf->infor[pf->size].sex;
pf->size++;
}
else
{
cout << "通讯录已满,不能添加联系人" << endl;
}
}
void PrintContact(contact* pf)
{
cout << "姓名" << "\t" << "性别" << "\t" << "年龄" << "\t" << "电话" << "\t" << "家庭住址" << endl;
for(int i = 0; i < pf->size; i++)
{
cout << pf->infor[i].name<< "\t" << pf->infor[i].sex << "\t" << pf->infor[i].age << "\t" << pf->infor[i].phone << "\t" << pf->infor[i].loca << endl;
}
}
void DelContact(contact* pf)
{
string name1 = "0";
cout << "请输入你要删除联系人的姓名:" << endl;
cin >> name1;
if (FindContact(pf, name1) != -1)
{
if (pf->infor[FindContact(pf, name1)].name == name1)//判断是否有要删除的联系人
{
//移动后面联系人
for (int i = FindContact(pf, name1); i < pf->size - 1; i++)
{
pf->infor[i] = pf->infor[i + 1];
}
pf->size--;//个数减1
cout << "删除成功" << endl;
}
}
else
{
cout << "没有您要删除的联系人" << endl;
}
}
int FindContact(contact* pf, string name)
{
for (int i = 0; i < pf->size; i++)
{
if (pf->infor[i].name == name)
{
return i;//返回下标
}
}
return -1;。//没有找到返回-1
}
void ModiContact(contact* pf, string name)
{
int tmp = FindContact(pf, name);
if (tmp != -1)
{
cout << "请输入年龄:" << endl;
cin >> pf->infor[tmp].age;
cout << "请输入地址:" << endl;
cin >> pf->infor[tmp].loca;
cout << "请输入名字:" << endl;
cin >> pf->infor[tmp].name;
cout << "请输入电话:" << endl;
cin >> pf->infor[tmp].phone;
cout << "请输入性别:" << endl;
cin >> pf->infor[tmp].sex;
}
else
{
cout << "抱歉!没有你要修改的联系人" << endl;
}
}
void EmptyContact(contact* pf)
{
if (pf->size != 0)
{
pf->size = 0;
cout << "清空联系人列表成功" << endl;
}
else
{
cout << "联系人列表已经是空,不用再次清空" << endl;
}
}
void swap(peoinfo* a, peoinfo* b)
{
peoinfo tmp = *a;
*a = *b;
*b = tmp;
}
void ContactSort(contact* pf)
{
for (int i = 0; i < pf->size - 1; i++)
{
for (int j = 0; j < pf->size - 1 - i; j++)
{
if (pf->infor[j].age > pf->infor[j + 1].age)
{
swap(&pf->infor[j], &pf->infor[j + 1]);
}
}
}
cout << "排序成功" << endl;
}
测试模块🤨
int main()
{
string name = "0";
string name1 = "0";
int tmp = 0;
contact date;//联系人
InitContact(&date);
int input = 0;
do
{
menu();
cout << "请输入你的选择:" << endl;
cin >> input;
switch (input)
{
case 1:
AddContact(&date,1000);
break;
case 2:
PrintContact(&date);
break;
case 3:
DelContact(&date);
break;
case 4:
cout << "请输入你要查找联系人的姓名:" << endl;
cin >> name;
tmp = FindContact(&date, name);
ContAch(&date, tmp);
break;
case 5:
cout << "请输入你要修改联系人的姓名:" << endl;
cin >> name1;
ModiContact(&date, name1);
break;
case 6:
EmptyContact(&date);
break;
case 7:
ContactSort(&date);
break;
case 0:
cout << "退出程序" << endl;
break;
default:
cout << "选择非法,请重新选择:" << endl;
break;
}
} while (input);
system("pause");
return 0;
}
小结😉
通讯录的实现主要我们要把握好,对数组的各种操作,理解底层数组内容的存放原理。这个通讯录也有一个缺点,它把联系人个数写固定了,并且还给存放到栈区了,后续会把他写成动态通讯录,把联系人内容存放到堆区,也可以把它写到文件中去。