CPP基础案例
通讯录管理系统
系统需求
通讯录是一个可以记录亲人、好友信息的工具
本案例主要利用C++来实现基础的通讯录管理系统
系统中需要实现的主要功能如下:
- 添加联系人:向通讯录中添加新人,信息包括姓名、性别、年龄、联系电话、家庭住址,最多记录1000人
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名删除指定联系人信息
- 查找联系人:按照姓名查看指定联系人信息
- 修改联系人:按照姓名重新修改指定联系人
- 清空联系人:清空通讯录中所有信息
- 退出通讯录:退出当前使用的通讯录
- 将对应功能通过分文件编写函数,使得项目结构清晰,可读性、复用性更强
联系人结构体
将联系人结构体放入到MyConstructs.h文件中,这样后续涉及到联系人结构体的内容直接引入即可:
#pragma once
#include <string>
using namespace std;
struct Person {
string name;
string gender;
int age = -1;// 初始值
string phone;
string address;
};
展示功能菜单
BeautyMenu.h:
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <string>
using namespace std;
void setColor(int color);// 设置打印文本颜色
void printMenu();// 打印功能菜单
BeautyMenu.cpp:
#include "BeautyMenu.h"
// 设置控制台颜色(Windows专属,Linux/macOS需改用ANSI escape codes)
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:
#include <iostream>
#include "MyConstructs.h"
using namespace std;
int createPerson(Person* p);// 在添加联系人之前,需要先创建联系人信息
int addPerson(Person p[]);
AddPerson.cpp:
#include "AddPerson.h"
// 创建用户函数,返回值为int,(1表示成功,2表示取消)
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;
// 当年龄输入错误,跳转到BACK位置
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:
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <string>
#include "MyConstructs.h"
using namespace std;
void showPersonList(Person p[]);
PrintContacts.cpp:
#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:
#include <iostream>
#include "MyConstructs.h"
int deletePerson(Person p[]);
DeletePerson.cpp:
#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:
#include <iostream>
#include "MyConstructs.h"
using namespace std;
int findPerson(Person p[]);
FindPerson.cpp:
#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;// 不能直接return,因为可能存在多个同名的
}
}
if (result == 1) {
return 1;
} else {
cout << "没有找到该联系人!" << endl;
return 2;
}
}
修改联系人
UpdatePerson.h:
#include <iostream>
#include "MyConstructs.h"
using namespace std;
int updatePerson(Person p[]);
UpdatePerson.cpp:
#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:
#include <iostream>
#include "MyConstructs.h"
using namespace std;
int cleanContacts(Person p[]);
CleanContacts.cpp:
#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:
#include <iostream>
#include <string>// 引入string库,用于处理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;
}
许可协议:
CC BY 4.0