基于结构体的控制台通讯录管理系统
通讯录是一个可以记录亲人、好友信息的工具,系统中需要实现的功能如下:
添加联系人
:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人显示联系人
:显示通讯录中所有的联系人信息删除联系人
:按照姓名进行删除指定联系人信息查找联系人
:按照姓名查看指定联系人信息修改联系人
:按照姓名重新修改指定联系人清空联系人
:清空通讯录中所有的信息退出通信录
:退出当前使用的通讯录
使用结构体记录联系人信息,在控制台中展示所有的操作流程
系统代码结构
:
ad_book.h
#pragma once
#include <iostream>
#include "string"
using namespace std;
struct Address_Book;
//添加联系人方法
void Add();
//打印共有多少联系人方法
void Print_Info();
//显示通讯录所有联系人信息
void Show();
//显示目标Address_Book结构体中的联系人信息
void Show(Address_Book* address_book);
//查找联系人信息
void Find();
//根据联系人姓名查找联系人信息
int Find(string name);
//删除联系人方法
void Delete();
//清空联系人
void Clear();
//修改联系人信息
void Update();
//菜单展示
void Show_Menu();
ad_book.cpp
#include <iostream>
#include "string"
#include "ad_book.h"
using namespace std;
//联系人最大支持的数量定义
#define MAX_LEN 1000
//通讯录记录信息的结构体
struct Address_Book
{
//姓名
string name;
//性别
string gender;
//年龄
int age = 0;
//联系电话
string phone;
//家庭住址
string address;
};
//联系人记录结构
static Address_Book address_book_array[MAX_LEN];
//当前共有联系人数量
static int Now_Size = 0;
//添加联系人方法
void Add()
{
std::cout << "-------------------------------------------------------------------------------" << std::endl;
Address_Book address_book;
//打印输出添加各项信息的提示
std::cout << "请输入您要添加的联系人姓名:" << std::endl;
std::cin >> address_book.name;
std::cout << "请输入您要添加的联系人性别:" << std::endl;
std::cin >> address_book.gender;
std::cout << "请输入您要添加的联系人年龄:" << std::endl;
std::cin >> address_book.age;
std::cout << "请输入您要添加的联系人联系电话:" << std::endl;
std::cin >> address_book.phone;
std::cout << "请输入您要添加的联系人家庭住址:" << std::endl;
std::cin >> address_book.address;
address_book_array[Now_Size++] = address_book;
std::cout << "-------------------------------------------------------------------------------" << std::endl;
Print_Info();
}
//打印输出当前共有多少的联系人信息
void Print_Info()
{
std::cout << "-------------------------------------------------------------------------------" << std::endl;
std::cout << "当前共有:" << Now_Size << " 个联系人 " << std::endl;
std::cout << "-------------------------------------------------------------------------------" << std::endl;
}
//显示通讯录所有联系人信息
void Show()
{
std::cout << "-------------------------------------------------------------------------------" << std::endl;
for (int i = 0; i < Now_Size; i++)
{
Show(&(address_book_array[i]));
}
std::cout << "-------------------------------------------------------------------------------" << std::endl;
}
//显示目标Address_Book结构体中的联系人信息
void Show(Address_Book* address_book)
{
std::cout << "姓名: " << address_book->name << " 性别: " << address_book->gender << " 年龄: "
<< address_book->age << " 联系电话: " << address_book->phone << "家庭住址: " << address_book->address << std::endl;
}
//查找联系人信息
void Find()
{
std::cout << "-------------------------------------------------------------------------------" << std::endl;
std::cout << "请输入您要查找的目标联系人姓名:" << std::endl;
string wait_find_name;
std::cin >> wait_find_name;
Find(wait_find_name);
std::cout << "-------------------------------------------------------------------------------" << std::endl;
}
//根据联系人姓名查找联系人信息
int Find(string name)
{
for (int i = 0; i < Now_Size; i++)
{
if (address_book_array[i].name == name)
{
Show(&address_book_array[i]);
return i;
}
}
std::cout << "用户姓名:" << name << " 对应的联系人信息未查找到!" << std::endl;
return -1;
}
//删除联系人方法
void Delete()
{
std::cout << "-------------------------------------------------------------------------------" << std::endl;
std::cout << "请输入您要删除的目标联系人姓名:" << std::endl;
string wait_delete_name;
std::cin >> wait_delete_name;
int wait_delete_index = Find(wait_delete_name);
if (wait_delete_index >= 0)
{
//把后面的内容交换到前面
for (int i = wait_delete_index; i < Now_Size; i++)
{
address_book_array[i] = address_book_array[i + 1];
}
}
Now_Size--;
std::cout << "-------------------------------------------------------------------------------" << std::endl;
}
//清空联系人
void Clear()
{
*address_book_array = {};
Now_Size = 0;
std::cout << "联系人清空完毕!" << std::endl;
}
//修改联系人信息
void Update()
{
std::cout << "-------------------------------------------------------------------------------" << std::endl;
std::cout << "请输入您要修改的目标联系人姓名:" << std::endl;
string wait_update_name;
std::cin >> wait_update_name;
int wait_update_index = Find(wait_update_name);
if (wait_update_index >= 0)
{
Address_Book address_book;
std::cout << "请输入您要修改的联系人姓名:" << std::endl;
std::cin >> address_book.name;
std::cout << "请输入您要修改的联系人性别:" << std::endl;
std::cin >> address_book.gender;
std::cout << "请输入您要修改的联系人年龄:" << std::endl;
std::cin >> address_book.age;
std::cout << "请输入您要修改加的联系人联系电话:" << std::endl;
std::cin >> address_book.phone;
std::cout << "请输入您要修改的联系人家庭住址:" << std::endl;
std::cin >> address_book.address;
address_book_array[wait_update_index] = address_book;
}
std::cout << "-------------------------------------------------------------------------------" << std::endl;
}
//菜单展示
void Show_Menu()
{
while (true)
{
std::cout << "-------------------------------------------------------------------------------" << std::endl;
std::cout << "欢迎来到通讯录管理系统,当前通讯录共有 " << Now_Size << " 个联系人!" << std::endl;
std::cout << "请选择您当前要进行的操作:" << std::endl;
std::cout << "A:添加联系人" << std::endl;
std::cout << "S:显示所有联系人" << std::endl;
std::cout << "D:删除联系人" << std::endl;
std::cout << "F:查找联系人" << std::endl;
std::cout << "U:修改联系人" << std::endl;
std::cout << "C:清空联系人" << std::endl;
std::cout << "E:退出当前系统" << std::endl;
char input;
std::cin >> input;
switch (input)
{
case 'A': Add(); break;
case 'S': Show(); break;
case 'D': Delete(); break;
case 'F': Find(); break;
case 'U': Update(); break;
case 'C': Clear(); break;
case 'E': exit(0);
}
std::cout << "-------------------------------------------------------------------------------" << std::endl;
}
}
Main.cpp
#include <iostream>
#include <ctime>
#include "ad_book.h"
int main()
{
Show_Menu();
}