C++通讯录管理系统
石先森很疯狂 人气:0案例描述:
通讯录是一个可以记录亲人、好友信息的工具。
本教程主要利用C++来实现一个通讯录管理系统
系统中需要实现的功能如下:
- 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名进行删除指定联系人
- 查找联系人:按照姓名查看指定联系人信息
- 修改联系人:按照姓名重新修改指定联系人
- 清空联系人:清空通讯录中所有信息
- 退出通讯录:退出当前使用的通讯录
#include<iostream> #include<string> using namespace std; #define MAX 1000 //通讯录总人数,可以修改 //联系人信息结构体 struct Person { string name; string sex; int age; int number; string address; }; //通讯录结构体 struct addressbook { 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 << "***** 0、退出通讯录 *****" << endl; cout << "***************************" << endl; } //查找联系人 int findPerson(addressbook* abs, string name) { //-1代表不存在该联系人 int j = -1; for (int i = 0; i < abs->m_Size; i++) { if (abs->personArray[i].name == name) { cout << "姓名:" << abs->personArray[i].name << " " << "性别:" << abs->personArray[i].sex << " " << "年龄:" << abs->personArray[i].age << " " << "联系电话:" << abs->personArray[i].number << " " << "家庭住址:" << abs->personArray[i].address << endl; //获得已存在联系人的下标 j = i; } } return j; } //添加联系人 void addPerson(addressbook* abs, string name) { int sex = 0; int age; int number; string address; abs->personArray[abs->m_Size].name = name; cout << "请输入性别:" << endl; cout << "1 -- 男" << endl; cout << "2 -- 女" << endl; //性别 while (true) { cin >> sex; if (sex == 1 || sex == 2) { if (sex == 1) { abs->personArray[abs->m_Size].sex = "男"; break; } else { abs->personArray[abs->m_Size].sex = "女"; break; } } cout << "输入有误,请重新输入"; } //年龄 cout << "请输入年龄:" << endl; cin >> age; abs->personArray[abs->m_Size].age = age; //联系电话 cout << "请输入联系电话:" << endl; cin >> number; abs->personArray[abs->m_Size].number = number; //家庭住址 cout << "请输入家庭住址:" << endl; cin >> address; abs->personArray[abs->m_Size].address = address; //更新通讯录人数 abs->m_Size++; cout << "添加成功" << endl; system("pause"); //执行清屏操作 system("cls"); } //判断联系人受否存在 void existsPerson(addressbook* abs, string name) { //判断通讯录是否已满 if (abs->m_Size == 1000) { cout << "通讯录已满,不能添加联系人!" << endl; } //未满,则判断联系人是否已存在 else { //判断用户输入的联系人是否存在 int p = findPerson(abs, name); //不存在执行添加联系人操作 if (p == -1) { addPerson(abs, name); } else { //若存在,则输出联系人信息,并询问用户是否继续添加 int i = p; cout << "姓名:" << abs->personArray[i].name << " " << "性别:" << abs->personArray[i].sex << " " << "年龄:" << abs->personArray[i].age << " " << "联系电话:" << abs->personArray[i].address << " " << "家庭住址:" << abs->personArray[i].address << endl; cout << "联系人已存在! " << endl; system("pause"); //执行清屏操作 system("cls"); } } } //删除联系人 void deletePerson(addressbook* abs) { string name; cout << "请输入联系人姓名" << endl; cin >> name; //判断用户输入的联系人是否存在 int p = findPerson(abs, name); if (p != -1) { //进行删除联系人操作 for (int i = p; i < abs->m_Size; i++) { abs->personArray[i] = abs->personArray[i + 1]; } //更新通讯录 abs->m_Size--; cout << "删除成功!" << endl; } else { cout << "查无此人!" << endl; } system("pause"); //执行清屏操作 system("cls"); } //显示联系人 void showPerson(addressbook* abs) { if (abs->m_Size == 0) { cout << "当前记录为空" << endl; } else { for (int i = 0; i < abs->m_Size; i++) { cout << "姓名:" << abs->personArray[i].name << "\t"; cout << "性别:" << abs->personArray[i].sex << "\t"; cout << "年龄:" << abs->personArray[i].age << "\t"; cout << "电话:" << abs->personArray[i].number << "\t"; cout << "住址:" << abs->personArray[i].address << endl; } } system("pause"); system("cls"); } //修改联系人 void changePerson(addressbook* abs) { string name; int sex = 0; int age; int number; string address; cout << "请输入联系人姓名:" << endl; cin >> name; //判断用户输入的联系人是否存在 int p = findPerson(abs, name); if (p != -1) { cout << "请输入联系人姓名:" << endl; cin >> name; abs->personArray[p].name = name; cout << "请输入性别:" << endl; cout << "1 -- 男" << endl; cout << "2 -- 女" << endl; //性别 while (true) { cin >> sex; if (sex == 1 || sex == 2) { if (sex == 1) { abs->personArray[p].sex = "男"; break; } else { abs->personArray[p].sex = "女"; break; } } cout << "输入有误,请重新输入"; } //年龄 cout << "请输入年龄:" << endl; cin >> age; abs->personArray[p].age = age; //联系电话 cout << "请输入联系电话:" << endl; cin >> number; abs->personArray[p].number = number; //家庭住址 cout << "请输入家庭住址:" << endl; cin >> address; abs->personArray[p].address = address; cout << "修改成功" << endl; system("pause"); //执行清屏操作 system("cls"); } else { cout << "该联系人不存在!" << endl; system("pause"); //执行清屏操作 system("cls"); } } //清空联系人 void empty(addressbook* abs) { abs->m_Size = 0; cout << "通讯录已清空" << endl; system("pause"); system("cls"); } int main() { //初始化通讯录 addressbook abs; //初始化通讯录人数为0 abs.m_Size = 0; int flag = true; int key = 0; string name; while (true) { showMenu(); cout << "请输入要操作的功能" << endl; cin >> key; switch (key) { case 1: //先判断判断通讯录是否已满,在判断联系人是否已存在 //最后进行联系人添加操作addPerson(addressbook *abs) cout << "请输入联系人姓名" << endl; cin >> name; existsPerson(&abs, name); break; case 2: showPerson(&abs); break; case 3: deletePerson(&abs); break; case 4: cout << "请输入联系人姓名" << endl; cin >> name; findPerson(&abs, name); system("pause"); //执行清屏操作 system("cls"); break; case 5: changePerson(&abs); break; case 6: empty(&abs); break; case 0: cout << "欢迎下次使用!" << endl; system("pause"); return 0; break; default: cout << "请输入正确的选项序号:" << endl; break; } } system("pause"); return 0; }
加载全部内容