C++实现简单的通讯录管理系统
1. 功能设计
通讯录是一个可以记录亲人、好友信息的工具。
本代码主要利用C++来实现一个通讯录管理系统
系统中需要实现的功能如下:
●添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
●显示联系人:显示通讯录中所有联系人信息
●删除联系人:按照姓名进行删除指定联系人
●查找联系人:按照姓名查看指定联系人信息
●修改联系人:按照姓名重新修改指定联系人
●清空联系人:清空通讯录中所有信息
●退出通讯录:退出当前使用的通讯录
2.代码设计
1) 通讯录管理系统.cpp
#include<iostream>
#include<string>
#include<stdlib.h>
#define MAX 1000 //宏常量
using namespace std;
//1.设计联系人结构体
struct Person{
string m_name;
int m_sex; //1男;2女
int m_age;
string m_phone;
string m_addr;
};
//2.设计通讯录结构体
struct Addressbooks{
//保存联系人的数组
struct Person personArray[MAX];
//通讯录中当前记录的人的个数
int m_size;
};
bool positive(int n){//判断一个数是不是正数
if(n>0){
return 1;
}
else if(n==0){
return 1;
}
else{
return 0;
}
}
int length(string s){
int i = 0;
while(s[i]){
i++;
}
return i;
}
//添加联系人
void addPerson(Addressbooks * abs)
{
//1.判断通讯录是会否存满了
if(abs->m_size == MAX){
cout << "通讯录已经存满了,不可再存" <<endl;
return; //return 是无返回值,return 0 是跳出程序
}
else{
//输入姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_size].m_name = name; // ->是找地址;.是取值
//输入性别
cout << "请输入性别(输入1:表示性别为男;输入2:表示性别为女):" << endl;
int sex = 0;j
while (true)
{
//如果输入的是1或者2可以退出循环,因为输入的是正确值
//如果输入有误,重新输入
cin >> sex;
if (sex == 1 || sex==2){
abs->personArray[abs->m_size].m_sex = sex;
break;
}
cout << "输入有误,请重新输入" <<endl;
}
//输入年龄
cout << "请输入年龄:" << endl;
int age = 0;
// cin >> age;
while (true){
cin >> age;
if(positive(age)){
abs->personArray[abs->m_size].m_age = age;
break;
}
cout << "输入有误,请输入一个正数" <<endl;
}
//输入电话
cout << "请输入电话号码(请输入11位数字):" << endl;
string phone;
while (true){
cin >> phone;
if(length(phone) == 11){
abs->personArray[abs->m_size].m_phone = phone;
break;
}
cout << "输入有误,请输入一个11位数的电话号码" <<endl;
}
//输入地址
cout << "请输入家庭住址" << endl;
string addr;
cin >> addr;
abs->personArray[abs->m_size].m_addr = addr;
//更新通讯录人数
abs->m_size++;
cout << "现有通讯录中人数" << abs->m_size << endl;
cout << "添加成功!" << endl;
//cout << "\033c"; //清屏操作
//system("cls"); // 只有windows电脑上才能用
}
};
//显示联系人
void showPerson(Addressbooks * abs){
if(abs->m_size == 0){
cout << "当前通讯录里没有记录" << endl;
return;
}
else{
for (int i = 0; i<abs->m_size; i++){
cout << "姓名:" << abs->personArray[i].m_name << endl;
cout << "性别:" << abs->personArray[i].m_sex << endl;
cout << "年龄:" << abs->personArray[i].m_age << endl;
cout << "电话:" << abs->personArray[i].m_phone << endl;
cout << "家庭住址:" << abs->personArray[i].m_addr << endl;
}
cout << "已全部输出" << endl;
}
//cout << "\033c";
};
//删除联系人,存在的话,返回联系人所在数组中的位置,不存在返回-1
void delPerson(Addressbooks *abs){
string name;
cout << "请输入要删除的联系人姓名:" << endl;
cin >> name;
int i=0;
for (i; i< abs->m_size; i++){
if(abs->personArray[i].m_name == name){
for(int j= i; j<abs->m_size; j++){
abs->personArray[j].m_name = abs->personArray[j+1].m_name;
abs->personArray[j].m_sex = abs->personArray[j+1].m_sex;
abs->personArray[j].m_age = abs->personArray[j+1].m_age;
abs->personArray[j].m_phone = abs->personArray[j+1].m_phone;
abs->personArray[j].m_addr = abs->personArray[j+1].m_addr;
}
}
}
abs->m_size--;
cout << "删除成功!" << endl;
};
//查找联系人
void findPerson(Addressbooks *abs){
string name;
cout << "请输入想要查找的联系人的姓名:" << endl;
cin >> name;
for(int i = 0; i< abs->m_size ; i++){
if(name == abs->personArray[i].m_name){
cout << "您所查找的联系人的信息如下:" << endl;
cout << "姓名:" << abs->personArray[i].m_name << endl;
cout << "性别:" << abs->personArray[i].m_sex << endl;
cout << "年龄:" << abs->personArray[i].m_age << endl;
cout << "电话:" << abs->personArray[i].m_phone << endl;
cout << "家庭住址:" << abs->personArray[i].m_addr << endl;
}
}
}
//修改联系人
void redefPerson(Addressbooks *abs){
string name;
cout << "请输入想要修改的联系人的姓名:" << endl;
cin >> name;
int i = 0;
for(i ; i< abs->m_size ; i++){
if(name == abs->personArray[i].m_name){
cout << "请选择您要进行的操作:****" << endl;
cout << "**********************" << endl;
cout << "*****1.修改联系人姓名****" << endl;
cout << "*****2.修改联系人性别****" << endl;
cout << "*****3.修改联系人年龄****" << endl;
cout << "*****4.修改联系人电话****" << endl;
cout << "*****5.修改联系人住址****" << endl;
cout << "***********************" << endl;
}
}
i--;
int select = 0;
cin >> select;
switch(select){
case 1:
{cout << "请输入要修改的内容" << endl;
string name;
cin >> name;
abs->personArray[i].m_name = name;
cout << "修改成功!" << endl;
break;}
case 2:
{cout << "请输入要修改的内容" << endl;
int sex;
cin >> sex;
abs->personArray[i].m_sex = sex;
cout << "修改成功!" << endl;
break;}
case 3:
{cout << "请输入要修改的内容" << endl;
int age;
cin >> age;
abs->personArray[i].m_age = age;
cout << "修改成功!" << endl;
break;}
case 4:
{cout << "请输入要修改的内容" << endl;
string phone;
cin >> phone;
abs->personArray[i].m_phone = phone;
cout << "修改成功!" << endl;
break;}
case 5:
{cout << "请输入要修改的内容" << endl;
string addr;
cin >> addr;
abs->personArray[i].m_addr = addr;
cout << "修改成功!" << endl;
break;}
default:
cout << "请重新选择操作:" << endl;
break;
}
}
//清空联系人
void clsPerson(Addressbooks *abs){
cout << "是否真的要清空联系人?" << " ";
cout << "1.确认清空;2.取消清空。" << endl;
int i = 0;
cin >> i;
if (i == 1){
abs->m_size = 0;
cout << "联系人已清空" << endl;
}
else{
return;
}
}
//菜单建立
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 main(){
//创建一个结构体变量
Addressbooks abs;
abs.m_size = 0;
int select = 0;
while (true){
showMenu(); //菜单调用
cout << "请选择操作:" << endl;
cin >> select;
switch(select){
case 1: //1.添加联系人1
addPerson(&abs); //利用地址传递修饰实参
//return 0; 一开始在这里写了这句话是错误的!程序跳出来了 没有继续执行 跳出程序了
break;
case 2: //2.显示联系人
showPerson(&abs);
break;
case 3: //3.删除联系人
delPerson(&abs);
break;
case 4: //4.查找联系人
findPerson(&abs);
break;
case 5: //5.修改联系人
redefPerson(&abs);
break;
case 6: //6.清空联系人
clsPerson(&abs);
break;
case 0: //0.退出通讯录
cout << "欢迎下次使用!" << endl;
return 0;
break;
default:
break;
}
}
return 0;
}
2. 添加联系人.cpp
#include<iostream>
#include<string>
#define MAX = 1000
using namespace std;
//1.设计联系人结构体
struct Person{
string m_name;
string m_sex;
int m_age;
string m_phone;
string m_adde;
};
//2.设计通讯录结构体
//3.main函数中创建通讯录
//4.封装添加联系人函数
//5.测试添加联系人功能
标签:name,管理系统,联系人,C++,abs,通讯录,personArray,cout
From: https://www.cnblogs.com/Noiimplant/p/17341824.html