#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <fstream>
#define MAX 1000
using namespace std;
struct Preson
{
string m_Name;
string m_Postion;
string m_Relation;
string m_Phone;
string m_QQ;
string m_Email;
};
struct Addressbooks
{
struct Preson presonArray[MAX]; //保存的数组
int m_Size; //个数
};
void ShowMenu()
{
cout << "\t\t\t\t****************************" << endl;
cout << "\t\t\t\t****** 1:添加联系人 ******" << endl;
cout << "\t\t\t\t****** 2:显示联系人 ******" << endl;
cout << "\t\t\t\t****** 3:删除联系人 ******" << endl;
cout << "\t\t\t\t****** 4:查找联系人 ******" << endl;
cout << "\t\t\t\t****** 5:修改联系人 ******" << endl;
cout << "\t\t\t\t****** 6:清空联系人 ******" << endl;
cout << "\t\t\t\t****** 7:写入文件 ******" << endl;
cout << "\t\t\t\t****** 0:退出该系统 ******" << endl;
cout << "\t\t\t\t****************************" << endl;
}
void addPreson(Addressbooks* abs) //添加的函数
{
if (abs->m_Size == MAX) //看看是不是满了
{
cout << "通讯录满了,无法再输入了!" << endl;
return;
}
else
{
//开始添加了
string name;
cout << "请输入姓名 : " << endl;
cin >> name;
abs->presonArray[abs->m_Size].m_Name = name;
cout << "请输入电话 : " << endl;
string phone;
cin >> phone;
abs->presonArray[abs->m_Size].m_Phone = phone;
cout << "请输入单位 : " << endl;
string postion;
cin >> postion;
abs->presonArray[abs->m_Size].m_Postion = postion;
cout << "请输入关系 : " << endl;
string relation;
cin >> relation;
abs->presonArray[abs->m_Size].m_Relation = relation;
cout << "请输入Email : " << endl;
string email;
cin >> email;
abs->presonArray[abs->m_Size].m_Email = email;
cout << "请输入QQ : " << endl;
string qq;
cin >> qq;
abs->presonArray[abs->m_Size].m_QQ = qq;
abs->m_Size++; //更新一下
cout << "添加成功!" << 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->presonArray[i].m_Name << "\t";
cout << " 电话: " << abs->presonArray[i].m_Phone << "\t";
cout << " 单位: " << abs->presonArray[i].m_Postion << "\t";
cout << " 关系: " << abs->presonArray[i].m_Relation << "\t";
cout << " Email: " << abs->presonArray[i].m_Email << "\t";
cout << " QQ: " << abs->presonArray[i].m_QQ << endl;
}
}
system("pause");
system("cls");
}
int isExist(Addressbooks* abs, string name) //检索联系人是否存在
{
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->presonArray[i].m_Name == name)
{
return i;
}
}
return -1;
}
void deletePerson(Addressbooks* abs) //删除
{
cout << "请输入要删除的联系人姓名:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->m_Size; i++) //删除的操作
{
abs->presonArray[i] = abs->presonArray[i + 1]; //数据前移
}
abs->m_Size--; //更新一下
cout << "删除成功!" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void findPerson(Addressbooks* abs) //查找
{
cout << "请输入要查找的联系人姓名:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{ //打印
cout << "姓名: " << abs->presonArray[ret].m_Name << "\t";
cout << " 电话: " << abs->presonArray[ret].m_Phone << "\t";
cout << " 单位: " << abs->presonArray[ret].m_Postion << "\t";
cout << " 关系: " << abs->presonArray[ret].m_Relation << "\t";
cout << " Email: " << abs->presonArray[ret].m_Email << "\t";
cout << " QQ: " << abs->presonArray[ret].m_QQ << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void out(Addressbooks* abs)
{
ofstream fp;
fp.open("通讯录.txt", ios::out);
fp << "姓名" << "\t" << "电话" << "\t" << "单位" << "\t" << "关系" << "\t" << "Email" << "\t" << "QQ" << endl;
for (int i = 0; i <= abs->m_Size; i++)
{
fp << abs->presonArray[i].m_Name << "\t" << abs->presonArray[i].m_Phone <<
"\t" << abs->presonArray[i].m_Postion << "\t" << abs->presonArray[i].m_Relation
<< "\t" << abs->presonArray[i].m_Email << "\t" << abs->presonArray[i].m_QQ << endl;
}
fp.close();
cout << "写入成功" << endl;
system("pause");
system("cls");
}
void modifyPerson(Addressbooks* abs) //修改
{
cout << "请输入要修改的联系人姓名" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
string name;
cout << "请输入姓名 :" << endl;
cin >> name;
abs->presonArray[ret].m_Name = name;
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
abs->presonArray[ret].m_Phone = phone;
cout << "请输入单位:" << endl;
string postion;
cin >> postion;
abs->presonArray[ret].m_Postion = postion;
cout << "请输入关系:" << endl;
string relation;
cin >> relation;
abs->presonArray[ret].m_Relation = relation;
cout << "请输入Email :" << endl;
string email;
cin >> email;
abs->presonArray[ret].m_Email = email;
cout << "请输入QQ:" << endl;
string qq;
cin >> qq;
abs->presonArray[ret].m_QQ = qq;
cout << "修改成功!" << endl;
}
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; //初始化一下
int select = 0; //供用户选择菜单
while (true)
{
ShowMenu();
cin >> select;
switch (select)
{
case 1: //添加
addPreson(&abs); //记得带 &
break;
case 2: //显示
showPerson(&abs);
break;
case 3: //删除
deletePerson(&abs);
break;
case 4: //查找
findPerson(&abs);
break;
case 5: //修改
modifyPerson(&abs);
break;
case 6: //清空
cleanPerson(&abs);
break;
case 7:
out(&abs);
break;
case 0: //退出
cout << "退出成功,欢迎下次使用!" << endl;
system("pasue");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
把结构体变成类 最好有详细的注释(体谅一下小白)