做了一些小修改,可修改空间尚且很多,有空再慢慢修改。
#include <iostream>
using namespace std;
#include <string>
#include <ctime>
#include <sstream>
#define MAX 1000//最大人数
//联系人结构体
struct Person
{
string m_Name;//姓名
string m_Sex;//性别:1男 2女
string m_Age;//年龄
string m_Phone;//电话
string m_Addr;//地址
};
//通讯录结构体
struct Addressbooks
{
struct Person personArray[MAX];//通讯录中保存的联系人数组
int m_Size;//通讯录中人员个数
};
void showMenu()
{
cout << "************************************" << endl;
cout << "********** 1.添加联系人 **********" << endl;
cout << "********** 2.显示联系人 **********" << endl;
cout << "********** 3.删除联系人 **********" << endl;
cout << "********** 4.查找联系人 **********" << endl;
cout << "********** 5.修改联系人 **********" << endl;
cout << "********** 6.清空联系人 **********" << endl;
cout << "********** 7.退出通讯录 **********" << endl;
cout << "************************************" << endl;
cout << "请选择您要进行的操作" << endl;
}
int isnumber(string var, int len)
{
for (int i = 0; i < len; i++)
{
if (isdigit(var[i]) == 0)
{
return -1;
break;
}
}
return 1;
}
void add(Addressbooks* abs,int ret,int type)
{
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
if (type == 1)
{
abs->personArray[ret].m_Name = name;
}
else
{
abs->personArray[abs->m_Size].m_Name = name;
}
//性别
cout << "\n请输入性别:" << endl;
cout << "1 -- 男" << endl;
cout << "2 -- 女" << endl;
string sex = "0";
while (true)
{
cin >> sex;
if (sex == "1" || sex == "2")
{
if (type == 1)
{
abs->personArray[ret].m_Sex = sex;
}
else
{
abs->personArray[abs->m_Size].m_Sex = sex;
}
break;
}
else
{
cout << "输入有误,请重新输入\n";
}
}
//年龄
cout << "\n请输入年龄:" << endl;
string age = "0";
while (true) {
cin >> age;
int len = age.length();
int ret = isnumber(age, len);
if (ret == 1)
{
if (type == 1)
{
abs->personArray[ret].m_Age = age;
}
else
{
abs->personArray[abs->m_Size].m_Age = age;
}
break;
}
else
{
cout << "输入有误,请重新输入\n" << endl;
}
}
//联系电话
cout << "\n请输入联系电话:" << endl;
string phone = "";
cin >> phone;
if (type == 1)
{
abs->personArray[ret].m_Phone = phone;
}
else
{
abs->personArray[abs->m_Size].m_Phone = phone;
}
//家庭住址
cout << "\n请输入家庭住址:" << endl;
string address;
cin >> address;
if (type == 1)
{
abs->personArray[ret].m_Addr = address;
}
else
{
abs->personArray[abs->m_Size].m_Addr = address;
}
}
void addPerson(Addressbooks * abs)
{
if (abs->m_Size == MAX)
{
cout << "通讯录已满,无法添加" << endl;
return;
}
else
{
add(abs, 0, 0);
//更新通讯录人数
abs->m_Size++;
cout << "\n添加成功" << endl;
system("pause");
system("cls");
}
}
void showPerson(Addressbooks * abs)
{
if (abs->m_Size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名:" << abs->personArray[i].m_Name << "\t";
cout << "性别:" << (abs->personArray[i].m_Sex == "1"? "男":"女") << "\t";
cout << "年龄:" << abs->personArray[i].m_Age << "\t";
cout << "电话:" << abs->personArray[i].m_Phone << "\t";
cout << "住址:" << 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 delectPerson(Addressbooks* abs)
{
cout << "请输入您要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "\n您要删除的联系人为:" << abs->personArray[ret].m_Name << endl;
cout << "\n确认删除该联系人-- 1\n取消操作--2" << endl;
int delect = 0;
cin >> delect;
if (delect == 1)
{
for(int i = ret;i<abs->m_Size;i++)
{
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_Size--;
cout << "\n删除成功" << endl;
system("pause");
system("cls");
}
else
{
cout << "取消删除成功" << endl;
system("pause");
system("cls");
}
}
else
{
cout << "查无此人" << endl;
}
}
void findPerson(Addressbooks* abs)
{
cout << "请输入您要查找的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "\n姓名:" << abs->personArray[ret].m_Name << "\t";
cout << "性别:" << abs->personArray[ret].m_Sex << "\t";
cout << "年龄:" << abs->personArray[ret].m_Age << "\t";
cout << "电话:" << abs->personArray[ret].m_Phone << "\t";
cout << "地址:" << abs->personArray[ret].m_Addr << endl;
}
else
{
cout << "\n查无此人" << endl;
}
system("pause");
system("cls");
}
void modifyPerson(Addressbooks* abs)
{
cout << "请输入您要修改的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "\n即将修改的联系人为:" << abs->personArray[ret].m_Name << endl;
cout << "\n请确认是否修改该联系人" << endl;
cout << "确认修改 -- 1" << endl;
cout << "取消修改 -- 2" << endl;
int select_modify = 0;
cin >> select_modify;
if (select_modify == 1)
{
add(abs,ret,1);//type = 1为modify type = 0为add
cout << "\n修改成功" << endl;
}
system("pause");
system("cls");
}
else
{
cout << "查无此人" << endl;
system("pause");
system("cls");
}
}
void cleanPerson(Addressbooks* abs)
{
abs->m_Size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
int main() {
Addressbooks abs;
abs.m_Size = 0;
while (true)
{
showMenu();
string select = "0";
cin >> select;
int len = select.length();
int ret = isnumber(select, len);
if (ret == 1)
{
int choice = 0;
istringstream ss (select);
ss >> choice;
switch (choice) {
case 1://添加联系人
addPerson(&abs);
break;
case 2://显示联系人
showPerson(&abs);
break;
case 3://删除联系人
delectPerson(&abs);
break;
case 4://查找联系人
findPerson(&abs);
break;
case 5://修改联系人
modifyPerson(&abs);
break;
case 6://清空联系人
cleanPerson(&abs);
break;
case 7://退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
cout << "输入有误,请重新输入\n" << endl;
system("pause");
system("cls");
break;
}
}
else
{
cout << "输入有误,请重新输入\n" << endl;
system("pause");
system("cls");
}
}
system("pause");
return 0;
}