C++实现通讯录管理系统(不涉及文件操作)

目录

一、前言

二、项目文件构成

三、项目实现功能

四、项目代码


一、前言

        这个是学习c++的时候顺手写的一个练习小demo,发出来希望有需要的朋友可以用到。

        也可以直接到我的github仓库中拉取源码和可运行程序。

        https://github.com/Mjbetter/AddressBookManagementSystem

二、项目文件构成

        项目的话,有三个头文件

                1、function.h:封装的是功能函数的声明

                2、show_menu.h:封装的是菜单函数的声明

                3、structure.h:封装的是定义的结构体

        有3个cpp文件

                1、function.cpp:实现功能函数

                2、main.cpp:主函数

                3、show_menu.cpp:实现菜单显示

        项目的运行环境是在VS2019上面。

三、项目实现功能

        项目一共实现7个功能

                1、添加联系人

                2、显示联系人

                3、删除联系人

                4、查找联系人

                5、修改联系人

                6、清空联系人        

                7、退出通讯录

四、项目代码

 function.h:

#include <iostream>
#include <string>
#include <Windows.h>
#include <iomanip>
#include "structure.h"

using namespace std;


/*Add new person to the address book*/
void add_person(struct Person *p,int *count);

/*show all person of the address book*/
void show_person(const struct Person* p,int count);

/*delete specified person of the address book*/
void delete_person(struct Person* p,string name,int *count);

/*find specified person of the address book*/
void find_person(const struct Person* p,string name,int count);

/*modify specified person of the address book*/
void modify_person(struct Person* p,string name,int count);

/*clean all person of the address book*/
void clean_person(struct Person* p,int *counts);

 show_menu.h

#include <iostream>
#include <Windows.h>

using namespace std;

int main_menu();

structure.h

#ifndef	 _XXXX_
#define  _XXXX_

#include <string>
#include <iostream>

using namespace std;

struct Person{
	string name;
	int age;
	string sex;
	string phone_number;
	string address;
};

#endif

function.cpp

#include "function.h"

#define MAXSIZE 1000

/*Add new person to the address book*/
void add_person(struct Person* p,int *count) {
	if (*count >= MAXSIZE) {
		cout << "通讯录已满,无法添加" << endl;
		system("pause");
		return;
	}
	cout << "请输入姓名:";
	cin >> p[*count].name;
	cout << endl;
	cout << "请输入性别:(男或女)";
	while (cin >> p[*count].sex) {
		if (p[*count].sex == "男" || p[*count].sex == "女") {
			break;
		}
		else {
			cout << "请输入正确的性别:(男或女)" << endl;
		}
	}
	cout << endl;
	cout << "请输入年龄:(0-110)";
	while (cin >> p[*count].age) {
		if (p[*count].age >= 0 && p[*count].age <= 110) {
			break;
		}
		else {
			cout << "请输入正确的年龄:(0-110)" << endl;
		}
	}
	cout << endl;
	cout << "请输入联系电话:";
	cin >> p[*count].phone_number;
	cout << endl;
	cout << "请输入家庭住址:";
	cin >> p[(*count)++].address;
	cout << endl;
	cout << endl << endl << "添加成功,即将返回主菜单...";
	system("pause");
}

/*show all person of the address book*/
void show_person(const struct Person* p,int count) {
	if (count == 0) {
		cout << "您的通讯录中暂无信息,请添加信息" << endl;
		system("pause");
		return;
	}
	cout << "您的通讯录中有如下亲朋好友:" << endl;
	cout << left << setw(8) << "姓名" << setw(8) << "性别" << setw(8) << "年龄" << setw(26) << "联系电话" << setw(40) << "家庭住址" << endl;
	for (int i = 0; i < count; i++) {
		/*aligned to the left*/
		cout << left << setw(8) << p[i].name << setw(8) << p[i].sex << setw(8) << p[i].age << setw(26) << p[i].phone_number << setw(40) << p[i].address << endl;
	}
	system("pause");
}

/*delete specified person of the address book*/
void delete_person(struct Person* p,string name,int *count) {
	int judge = 0;
	for (int i = 0; i < *count; i++) {
		if (p[i].name == name) {
			judge = 1;
			for (int j = i; j < (*count)-1; j++) {
				p[j] = p[j + 1];
			}
			*count = *count - 1;
		}
	}
	if (judge == 0) cout << "对不起,你的通讯录中不存在此人" << endl;
	else cout << "已经删除所有名字为" << name << "的联系人";
	system("pause");
}

/*find specified person of the address book*/
void find_person(const struct Person* p,string name,int count) {
	for (int i = 0; i < count; i++) {
		if (p[i].name == name) {
			cout << left << setw(8) << "姓名" << setw(8) << "性别" << setw(8) << "年龄" << setw(8) << "联系电话" << setw(8) << "家庭住址" << endl;
			cout << left << setw(8) << p[i].name << setw(8) << p[i].sex << setw(8) << p[i].age << setw(8) << p[i].phone_number << setw(8) << p[i].address << endl;
			break;
		}
	}
	system("pause");
}

/*modify specified person of the address book*/
void modify_person(struct Person* p,string name,int count) {
	for (int i = 0; i < count; i++) {
		if (p[i].name == name) {
			cout << "请输入姓名:";
			cin >> p[i].name;
			cout << endl;
			cout << "请输入性别:";
			cin >> p[i].sex;
			cout << endl;
			cout << "请输入年龄:";
			cin >> p[i].age;
			cout << endl;
			cout << "请输入联系电话:";
			cin >> p[i].phone_number;
			cout << endl;
			cout << "请输入家庭住址:";
			cin >> p[i].address;
			cout << endl;
			cout << endl << endl << "修改成功,即将返回主菜单...";
			Sleep(3000);
			break;
		}
	}
}

/*clean all person of the address book*/
void clean_person(struct Person* p,int *counts) {
	for (int i = 0; i < *counts; i++) {
		memset(&p[i], '\0', sizeof(struct Person));
	}
	cout << "已经清空所有联系人" << endl;
	*counts = 0;
	system("pause");
}

main.cpp

#include <iostream>
#include <string>
#include <Windows.h>
#include "show_menu.h"
#include "function.h"
#include "structure.h"
#include<stdlib.h>

using namespace std;


struct Person persons[1000];
int counts;


int main() {

	while (1) {
		int number = main_menu();
		string name;
		//according to the serial number to jump interface
		switch (number)
		{
		case 1:
			system("cls");
			add_person(persons, &counts);
			break;
		case 2:
			system("cls");
			show_person(persons, counts);
			break;
		case 3:
			cout << "请输入想要删除的联系人的姓名:" << endl;
			cin >> name;
			delete_person(persons, name, &counts);
			break;
		case 4:
			cout << "请输入想要查找的联系人的姓名:" << endl;
			cin >> name;
			find_person(persons, name, counts);
			break;
		case 5:
			cout << "请输入想要修改的联系人的姓名:" << endl;
			cin >> name;
			modify_person(persons, name, counts);
			break;
		case 6:
			clean_person(persons, &counts);
			break;
		case 7:
			cout << "3秒后退出系统";
			Sleep(3000);
			exit(0);
			break;
		}
	}
}

show_menu.cpp

#include "show_menu.h"

int main_menu() {
	system("cls");
	cout << "--------欢迎使用MJ牌通讯录管理系统--------" << endl;
	cout << "----------------------------------------" << endl;
	cout << "----1.添加联系人--------------" << endl;
	cout << "----2.显示联系人--------------" << endl;
	cout << "----3.删除联系人--------------" << endl;
	cout << "----4.查找联系人--------------" << endl;
	cout << "----5.修改联系人--------------" << endl;
	cout << "----6.清空联系人--------------" << endl;
	cout << "----7.退出通讯录--------------" << endl;
	cout << "-----------------------------" << endl;
	cout << "请输入选择的功能序号:";
	int number;
	cin >> number;
	return number;
}

希望对大家有帮助。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值