chapter–1 通讯录项目
// 建议这里的key设置为学生ID 因为学生ID是唯一 学生姓名会有重名出现
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
// 联系人结构体
struct Person
{
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
// 通讯录的结构体
struct Addressbooks
{
// 通讯录保存的联系人数组
struct Person personArray[1000];
// 通讯录当前记录的人数
int m_Size;
};
// 1. Add LinkMan
void addPerson(Addressbooks *abs)
{
// 判断人数是否已经到达1000人满员了 ps:这里是用指针来引用对象属性的
if (abs->m_Size == 1000)
{
cout << "The LinkMan Person are full, you can't add preson!";
return;
}
else
{
string name;
cout << "please input the person name:" << endl;
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
cout << "please input the person sex:" << endl;
cout << "1 --- man " << endl;
cout << "2 --- women" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
else
{
cout << "please input the correct sex code: " << endl;
}
}
cout << "please input the person age:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0 && age < 100)
{
abs->personArray[abs->m_Size].m_Age = age;
break;
}
else
{
cout << "please input the correct age: " << endl;
}
}
cout << "please input the person phone:" << endl;
string phone;
while (true)
{
cin >> phone;
int len = phone.length();
if (len == 11)
{
abs->personArray[abs->m_Size].m_Phone = phone;
break;
}
else
{
cout << "please input the correct phone's length: " << endl;
}
}
cout << "please input the person address:" << endl;
string address;
cin >> address;
// 这里就不进行地址的判断了
abs->personArray[abs->m_Size].m_Addr = address;
// 因为添加了一个人 那么在通讯录里面就会增加一个人
abs->m_Size++;
cout << "You add preson successfully!!!" << endl;
// 请按任意键继续
system("pause");
// 清屏操作
system("cls");
}
}
// 显示联系人
void showPerson(Addressbooks *abs)
{
// 判空操作
if (!abs->m_Size)
{
cout << "There is no person!" << endl;
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "Name :" << abs->personArray[i].m_Name << "\t";
cout << "Sex :" << (abs->personArray[i].m_Sex == 1 ? "man" : "women") << "\t";
cout << "Age :" << abs->personArray[i].m_Age << "\t";
cout << "Phone :" << abs->personArray[i].m_Phone << "\t";
cout << "Address:" << abs->personArray[i].m_Addr << endl;
}
}
system("pause");
system("cls");
}
// 判断联系人是否在数组中
int isExist(Addressbooks *abs, string name)
{
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->personArray[i].m_Name == name)
{
return i;
}
}
// 未搜索到
return -1;
}
void deletePerson(Addressbooks *abs)
{
cout << "please the deleter Person" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->m_Size; i++)
{
abs->personArray[i] = abs->personArray[i + 1]; // 这里不会出现数组越界
}
// 删除了一个人 通讯录的人数要实现更新
abs->m_Size--;
cout << "数据更新成功!" << endl;
}
else
{
cout << "There is no person!" << endl;
}
system("pause");
system("cls");
}
void findPerson(Addressbooks *abs)
{
cout << "please input the name:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "Name :" << abs->personArray[ret].m_Name << "\t";
cout << "Sex :" << (abs->personArray[ret].m_Sex == 1 ? "man" : "women") << "\t";
cout << "Age :" << abs->personArray[ret].m_Age << "\t";
cout << "Phone :" << abs->personArray[ret].m_Phone << "\t";
cout << "Address:" << abs->personArray[ret].m_Addr << endl;
}
else
{
cout << "No finding the person!" << endl;
}
system("pause");
system("cls");
}
void modifyPerson(Addressbooks *abs)
{
cout << "please input the modify Person:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
// 修改内容项
string name;
cout << "please input the new name" < < < endl;
cin >> name;
abs->personArray[ret].m_Name = name;
int sex;
cin >> sex;
cout << "please input the new sex:" << endl;
cout << "1 -- man 2 -- women" << endl;
// 没有做sex正确的输入判断
abs->personArray[ret].m_Sex = sex;
int age;
cout << "please input the new age" << endl;
cin >> age;
abs->personArray[ret].m_Age = age;
string phone;
cin >> phone;
abs->personArray[ret].m_Phone = phone;
string addr;
cin >> addr;
abs->personArray[ret].m_Addr = addr;
}
system("pause");
system("cls");
}
void cleanPerson(Addressbooks *abs)
{
cout << "Whether to clear all contacts: 1-Yes 2-No" << endl;
int password;
cin >> password;
if (password == 1)
{
abs->m_Size = 0;
cout << "the Linkman is over" << end;
}
else if (password == 2)
{
cout << "Do not clean the all contacts!" << endl;
}
system("pause");
system("cls");
}
// 菜单界面
void showMenu()
{
cout << "*************************" << endl;
cout << "*****1 Add LinkMan******" << endl;
cout << "*****2 Show LinkMan******" << endl;
cout << "*****3 Del LinkMan******" << endl;
cout << "*****4 Find LinkMan******" << endl;
cout << "*****5 Upda LinkMan******" << endl;
cout << "*****6 Clea LinkMan******" << endl;
cout << "*****0 Logout System*****" << endl;
cout << "*************************" << endl;
}
int main()
{
// 创建通讯录结构体变量
Addressbooks abs;
// 初始化通讯录当前人员的个数
abs.m_Size = 0;
// 创建用户选择输入的变量
int select = 0;
while (true)
{
// 菜单调用
showMenu();
cin >> select;
switch (select)
{
case 1: // Add LinkMan
addPerson(&abs);
break;
case 2: // Show LinkMan
showPerson(&abs);
break;
case 3: // Del LinkMan
{
cout << "please input the delete name:" << endl;
string name;
cin >> name;
if (isExist(&abs, name) == -1)
{
cout << "There is no the person!" << endl;
}
else
{
cout << "There is the person!" << endl;
deletePerson(abs);
}
break;
}
case 4: // Find LinkMan
findPerson(&abs);
break;
case 5: // Upda LinkMan
modifyPerson(&abs);
break;
case 6: // Clea LinkMan
cleanPerson(&abs);
break;
case 0: // Logout System
cout << "Welcome to use next time!";
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}