#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;
#define MAX 1000
struct Person
{
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Address;
};
struct Contacts
{
struct Person personArray[MAX];
int m_Size;
};
int isExist(Contacts* cts, string name);
void addPerson(Contacts* cts)
{
//判断是否满了
if (cts->m_Size >= MAX)
{
cout << "通讯录满了" << endl;
return;
}
else {
string name;
cout << "请输入姓名:" << endl;
cin >> name;
cts->personArray[cts->m_Size].m_Name = name;
int sex;
cout << "请输入性别:" << endl;
cin >> sex;
cts->personArray[cts->m_Size].m_Sex = sex;
string phone;
cout << "请输入手机号:" << endl;
cin >> phone;
cts->personArray[cts->m_Size].m_Phone = phone;
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
cts->personArray[cts->m_Size].m_Address = addr;
cts->m_Size++;
system("pause");
system("cls");
}
}
void updatePerson(Contacts* cts, string name)
{
int index = isExist(cts, name);
if(index != -1){
string name;
cout << "请输入姓名:" << endl;
cin >> name;
cts->personArray[index].m_Name = name;
int sex;
cout << "请输入性别:" << endl;
cin >> sex;
cts->personArray[index].m_Sex = sex;
string phone;
cout << "请输入手机号:" << endl;
cin >> phone;
cts->personArray[index].m_Phone = phone;
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
cts->personArray[index].m_Address = addr;
}
else {
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void showPerson(Contacts * cts)
{
//判断人数
if (cts->m_Size == 0)
{
cout << "人数为0" << endl;
}
else {
for (int i = 0; i < cts->m_Size; i++)
{
cout << "姓名:" << cts->personArray[i].m_Name << "\t"
<< "电话:" << cts->personArray[i].m_Phone << "\t"
<< "性别:" << (cts->personArray[i].m_Sex == 1 ? "男" : "女") << "\t"
<< "地址:" << cts->personArray[i].m_Address << endl;
}
}
system("pause");
system("cls");
}
int isExist(Contacts* cts, string name)
{
for (int i = 0; i < cts->m_Size; i++)
{
if (cts->personArray[i].m_Name == name)
{
return i;
}
}
return -1;
}
void delPerson(Contacts* cts,string name)
{
int index = isExist(cts, name);
if (index != -1)
{
//删除index索引数据
for (int i = 0; i < cts->m_Size; i++)
{
cts->personArray[i] = cts->personArray[i + 1];
}
cts->m_Size--;
cout << "删除成功" << endl;
}
else {
cout << "查无此人" << endl;
}
}
void findPersonByName(Contacts* cts, string name)
{
int index = isExist(cts, name);
if (index != -1)
{
cout << "姓名:" << cts->personArray[index].m_Name << "\t"
<< "电话:" << cts->personArray[index].m_Phone << "\t"
<< "性别:" << (cts->personArray[index].m_Sex == 1 ? "男" : "女") << "\t"
<< "地址:" << cts->personArray[index].m_Address << endl;
}
system("pasue");
system("cls");
}
void clearPerson(Contacts* cts)
{
cts->m_Size = 0;//逻辑清空即可 访问不到就是清空
cout << "清空成功" << endl;
system("pasue");
system("cls");
}
void showMenu()
{
cout << "*************************" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、显示联系人 *****" << endl;
cout << "***** 3、删除联系人 *****" << endl;
cout << "***** 4、查找联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "***** 0、退出 *****" << endl;
cout << "*************************" << endl;
}
int main()
{
//创建通讯录
struct Contacts cts;
cts.m_Size = 0;
int select = 0;//用户选择变量
while (true) {
showMenu();
cin >> select;
switch (select)
{
case 1:
addPerson(&cts);
break;
case 2:
showPerson(&cts);
break;
case 3: {
cout << "请输入删除人姓名:" << endl;
string name;
cin >> name;
delPerson(&cts, name);
break;
}
case 4: {
cout << "请输入查找的姓名:" << endl;
string name;
cin >> name;
findPersonByName(&cts, name);
break;
}
case 5:
{
cout << "请输入修改的姓名:" << endl;
string name;
cin >> name;
updatePerson(&cts, name);
break;
}
case 6:
clearPerson(&cts);
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
cout << "无命令" << endl;
break;
}
}
}
c++做通讯录的demo
最新推荐文章于 2024-04-17 14:10:05 发布