通讯录管理系统
系统需求
通讯录是一个可以记录亲人、好友信息的工具
本案例主要利用C++来实现基础的通讯录管理系统
系统中需要实现的主要功能如下:
- 添加联系人:向通讯录中添加新人,信息包括姓名、性别、年龄、联系电话、家庭住址,最多记录1000人
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名删除指定联系人信息
- 查找联系人:按照姓名查看指定联系人信息
- 修改联系人:按照姓名重新修改指定联系人
- 清空联系人:清空通讯录中所有信息
- 退出通讯录:退出当前使用的通讯录
- 将对应功能通过分文件编写函数,使得项目结构清晰,可读性、复用性更强
联系人结构体
将联系人结构体放入到MyConstructs.h
文件中,这样后续涉及到联系人结构体的内容直接引入即可:
1 2 3 4 5 6 7 8 9 10 11 12
| #pragma once #include <string>
using namespace std;
struct Person { string name; string gender; int age = -1; string phone; string address; };
|
展示功能菜单
BeautyMenu.h
:
1 2 3 4 5 6 7 8 9 10
| #include <iostream> #include <iomanip> #include <windows.h> #include <string>
using namespace std;
void setColor(int color); void printMenu();
|
BeautyMenu.cpp
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include "BeautyMenu.h"
void setColor(int color) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); }
void printMenu() { const int colWidth = 30; const int numWidth = 8; const string line = "+" + string(numWidth + 2, '-') + "+" + string(colWidth + 2, '-') + "+";
setColor(14);
cout << "\n\n" << setw((numWidth + colWidth + 6) / 2 + 4) << "" << "通讯录管理系统" << endl;
setColor(11); cout << line << endl;
setColor(10); cout << "| " << left << setw(numWidth) << "选项数字" << " | " << setw(colWidth) << "功能描述" << " |" << endl;
setColor(11); cout << line << endl;
setColor(7); cout << "| " << left << setw(numWidth) << "数字1" << " | " << setw(colWidth) << "添加联系人" << " |" << endl;
cout << "| " << setw(numWidth) << "数字2" << " | " << setw(colWidth) << "显示联系人" << " |" << endl;
cout << "| " << setw(numWidth) << "数字3" << " | " << setw(colWidth) << "删除联系人" << " |" << endl;
cout << "| " << setw(numWidth) << "数字4" << " | " << setw(colWidth) << "查找联系人" << " |" << endl;
cout << "| " << setw(numWidth) << "数字5" << " | " << setw(colWidth) << "修改联系人" << " |" << endl;
cout << "| " << setw(numWidth) << "数字6" << " | " << setw(colWidth) << "清空联系人" << " |" << endl;
cout << "| " << setw(numWidth) << "数字7" << " | " << setw(colWidth) << "退出通讯录" << " |" << endl;
setColor(11); cout << line << endl; setColor(7); }
|
添加联系人
AddPerson.h
:
1 2 3 4 5 6 7 8
| #include <iostream> #include "MyConstructs.h"
using namespace std;
int createPerson(Person* p);
int addPerson(Person p[]);
|
AddPerson.cpp
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include "AddPerson.h"
int createPerson(Person* p) { Person tmp; bool controller = true; int confirm;
while (controller) { cout << "请输入用户姓名:"; cin >> tmp.name; cout << endl;
cout << "请输入用户性别:"; cin >> tmp.gender; cout << endl;
BACK: cout << "请输入用户年龄:"; cin >> tmp.age; if (tmp.age <= 0) { cout << "请输入正确年龄" << endl; goto BACK; } cout << endl;
cout << "请输入用户联系电话:"; cin >> tmp.phone; cout << endl;
cout << "请输入用户家庭住址:"; cin >> tmp.address; cout << endl;
cout << "输入完毕,您输入的信息为:" << "姓名:" << tmp.name << ";性别:" << tmp.gender << ";年龄:" << tmp.age << ";联系电话:" << tmp.phone << ";家庭住址:" << tmp.address << endl;
cout << "确认请输入1,取消并重新录入请输入2,退出请输入3:"; cin >> confirm;
while (true) { if (confirm == 1) { p->name = tmp.name; p->age = tmp.age; p->address = tmp.address; p->gender = tmp.gender; p->phone = tmp.phone; return 1; } else if (confirm == 2) { tmp = {}; break; } else if (confirm == 3) { return 2; } } } }
int addPerson(Person parr[]) { Person p; Person* personPointer = &p;
int check = createPerson(personPointer);
if (check == 1) { for (int i = 0; i < 1000; i++) { if (parr[i].name == "") { parr[i] = p; break; } } return 1; } else { return 2; } }
|
显示联系人
PrintContacts.h
:
1 2 3 4 5 6 7 8 9 10
| #include <iostream> #include <iomanip> #include <windows.h> #include <string> #include "MyConstructs.h"
using namespace std;
void showPersonList(Person p[]);
|
PrintContacts.cpp
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include "PrintContacts.h"
void showPersonList(Person p[]) { const int id_w = 6; const int name_w = 16; const int age_w = 8; const int gender_w = 8; const int phone_w = 16; const int addr_w = 30;
const string separator = "+------+------------------+--------+--------+------------------+-------------------------------+";
cout << separator << "\n" << "| " << left << setw(id_w) << "ID" << " | " << setw(name_w) << "姓名" << " | " << setw(age_w) << "年龄" << " | " << setw(gender_w) << "性别" << " | " << setw(phone_w) << "联系电话" << " | " << setw(addr_w) << "家庭住址" << " |\n" << separator << endl;
for (int i = 0; i < 1000; ++i) { if (p[i].age == -1) continue;
cout << "| " << left << setw(id_w) << i + 1 << " | " << setw(name_w) << p[i].name.substr(0, name_w) << " | " << right << setw(age_w - 1) << p[i].age << " " << " | " << left << setw(gender_w) << (p[i].gender.empty() ? "-" : p[i].gender) << " | " << setw(phone_w) << p[i].phone << " | " << setw(addr_w) << p[i].address.substr(0, addr_w) << " |\n"; }
cout << separator << endl; }
|
删除联系人
DeletePerson.h
:
1 2 3 4
| #include <iostream> #include "MyConstructs.h"
int deletePerson(Person p[]);
|
DeletePerson.cpp
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include "DeletePerson.h"
int deletePerson(Person p[]) { string name; Person empty;
cout << "请输入删除用户的名称:"; cin >> name;
for (size_t i = 0; i < 1000; i++) { if (p[i].name == name) { p[i] = empty; cout << "删除成功!" << endl; return 1; } }
cout << "没有" << name << "这个用户" << endl; return 2; }
|
查找联系人
FindPerson.h
:
1 2 3 4 5 6
| #include <iostream> #include "MyConstructs.h"
using namespace std;
int findPerson(Person p[]);
|
FindPerson.cpp
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include "FindPerson.h"
int findPerson(Person p[]) { string name; int result = -1; cout << "请输入要查找的姓名:" << endl; cin >> name; for (int i = 0; i < 1000; i++) { if (p[i].name == name) { cout << "姓名:" << p[i].name << " | "; cout << "性别:" << p[i].gender << " | "; cout << "年龄:" << p[i].age << " | "; cout << "电话:" << p[i].phone << " | "; cout << "地址:" << p[i].address << endl; result = 1; } }
if (result == 1) { return 1; } else { cout << "没有找到该联系人!" << endl; return 2; } }
|
修改联系人
UpdatePerson.h
:
1 2 3 4 5 6
| #include <iostream> #include "MyConstructs.h"
using namespace std;
int updatePerson(Person p[]);
|
UpdatePerson.cpp
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include "UpdatePerson.h"
int updatePerson(Person p[]) { cout << "请输入要修改的联系人姓名:" << endl; string name; cin >> name;
for (int i = 0; i < 1000; i++) { if (p[i].name == name) { cout << "请输入修改后的姓名:" << endl; cin >> p[i].name; cout << "请输入修改后的性别:" << endl; cin >> p[i].gender; cout << "请输入修改后的年龄:" << endl; cin >> p[i].age; cout << "请输入修改后的手机号:" << endl; cin >> p[i].phone; cout << "请输入修改后的地址:" << endl; cin >> p[i].address;
return 1; } }
cout << "未找到该联系人" << endl;
return 2; }
|
清空联系人
CleanContacts.h
:
1 2 3 4 5 6
| #include <iostream> #include "MyConstructs.h"
using namespace std;
int cleanContacts(Person p[]);
|
CleanContacts.cpp
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include "CleanContacts.h"
int cleanContacts(Person p[]) { cout << "确认清空通讯录请输入1,或输入其他数字退出" << endl; int confirm = 0; cin >> confirm; if (confirm == 1) { for (int i = 0; i < 1000; i++) { if (p[i].age != -1) { p[i].name = ""; p[i].phone = ""; p[i].gender = ""; p[i].age = -1; p[i].address = ""; } } cout << "通讯录已清空" << endl; return 1; }
cout << "已退出" << endl; return 0; }
|
主函数入口
Main.cpp
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <iostream> #include <string> #include "BeautyMenu.h" #include "AddPerson.h" #include "PrintContacts.h" #include "DeletePerson.h" #include "FindPerson.h" #include "UpdatePerson.h" #include "CleanContacts.h"
using namespace std;
int main() { Person contacts[1000];
bool controller = true;
while (controller) { int funcNumber = 0; printMenu(); cout << "欢迎来到通讯录管理系统,请输入相关数字进入对应功能:" << endl; cin >> funcNumber; switch (funcNumber) { case 1: addPerson(contacts); break; case 2: showPersonList(contacts); break; case 3: deletePerson(contacts); break; case 4: findPerson(contacts); break; case 5: updatePerson(contacts); break; case 6: cleanContacts(contacts); break; case 7: controller = false; cout << "系统退出中,欢迎下次使用!" << endl; break; default: cout << "请输入正确数字!" << endl; break; } }
return 0; }
|