通讯录管理系统——C++版

通讯录管理

1.菜单面板的初始化
2.功能实现
    从简单功能开始:①退出
    ②添加联系人:设计联系人结构体
                 通讯录存储结构(顺序)
                 封装函数
    ③显示联系人
    ④查找联系人:直接查找
    ⑤删除联系人
    ⑥修改联系人
    ⑦清空联系人
3.运行结果

 

#define MAX  10
#define INCREMENT  10
#include "iostream"
#include "string"
#include "stdlib.h"

using namespace std;

struct person
{
	char name[20];//姓名
	int sex;//性别:1男 2女
	int age;//年龄
	char phone[20]; //电话                 //电话为11位超出了int范围
	char address[20];//住址
};

struct Addressbook
{
	struct person* Ary;//联系人数组指针
	int nember;//联系人人数
	int booksize;//当前容量
};

void createmenu();//菜单界面的实现
bool createbook(struct Addressbook& book);//初始化通讯录
void add(struct Addressbook& book);//添加联系人
void show(struct Addressbook sbook);//显示联系人
int search_person(struct Addressbook sbook,string sname);//在通讯录中按照姓名关键字查找联系人,查找到则返回相应位置否则返回-1
void delete_person(struct Addressbook& book,string sname);//删除通讯录中的联系人
void modify_person(struct Addressbook& book, string sname);//按名字查找联系人,并选择修改联系人的一项信息
void clean(struct Addressbook& book);//清空联系人

int main() {

	createmenu();
	Addressbook book;
	createbook(book);
	string sname;//被删除联系人姓名

	do {

		int opt = 0;
		cout << "请输入功能号:" << endl;
		cin >> opt;

		switch (opt)
		{
		case 1:
			add(book);
			break;
		case 2:
			show(book);
			break;
		case 3:
			cout << "请输入所要删除的联系人姓名:";
			cin >> sname;
			delete_person(book, sname);
			break;
		case 4: {
			cout << "请输入所要查找的联系人姓名:";
			cin >> sname;
			if (search_person(book, sname) == -1)
			{
				cout << "通讯录内无此联系人!" << endl;
			}
			else
			{
				cout << "姓名:" << book.Ary[search_person(book, sname)].name << "\t";
				cout << "性别:" << (book.Ary[search_person(book, sname)].sex == 1 ? "男" : "女") << "\t";
				cout << "年龄:" << book.Ary[search_person(book, sname)].age << "\t";
				cout << "电话:" << book.Ary[search_person(book, sname)].phone << "\t";
				cout << "住址:" << book.Ary[search_person(book, sname)].address << endl;
			}
			break;
		}
		case 5: {
			cout << "请输入所要修改联系人的姓名:";
			cin >> sname;

			if (search_person(book, sname) == -1)
			{
				cout << "通讯录内无此联系人!" << endl;
			}
			else
			{
				modify_person(book, sname);
			}
			createmenu();
			break;
		}
		case 6:
			clean(book);
			createmenu();
			break;
		case 0: cout << "感谢使用,欢迎下次使用!" << endl;
			 system("pause");
			 return 0;
		default:
			cout << "请输入正确的功能号!" << endl;
		}
	} while (true);

	system("pause");

	return 0;
}

void createmenu() {
	cout << "\n" << endl;
	cout << "\n" << endl;
	cout << "\t\t\t\t\t\t1、添加联系人" << endl;
	cout << "\t\t\t\t\t\t2、显示联系人" << endl;
	cout << "\t\t\t\t\t\t3、删除联系人" << endl;
	cout << "\t\t\t\t\t\t4、查找联系人" << endl;
	cout << "\t\t\t\t\t\t5、修改联系人" << endl;
	cout << "\t\t\t\t\t\t6、清空联系人" << endl;
	cout << "\t\t\t\t\t\t0、退出通讯录" << endl;
	cout << "\n" << endl;

}

bool createbook(struct Addressbook& book)
{
	book.Ary = (struct person*)malloc(MAX * sizeof(struct person));
	if (! book.Ary)
	{
		cout << "内存分配失败,请重启程序!" << endl;
		exit(-1);
	}
	book.nember = 0;
	book.booksize = MAX;
	return true;
}

void add(struct Addressbook& book) {
	if (book.nember == MAX)
	{
		book.Ary = (struct person*)realloc(book.Ary, (book.booksize + INCREMENT) * sizeof(struct person));
		book.booksize += INCREMENT;
		if (! book.Ary)
		{
			cout << "内存分配失败,请重启程序!" << endl;
			exit(-1);
		}
	}
	
	struct person* p = &(book.Ary[book.nember]);
	struct person st;

	cout << "请输入联系人姓名:" << endl;
	cin >> st.name;
	strcpy_s(p->name,st.name);
	cout << "请输入联系人性别(输入数字:1男  2女)" << endl;
	cin >> st.sex;
	p->sex = st.sex;
	if (!(st.sex == 1 || st.sex ==2))
	{
		cout << "请输入正确的数字:";
		cin >> st.sex;
		p->sex = st.sex;
	}
	cout << "请输入联系人年龄:" << endl;
	cin >> st.age;
	p->age = st.age;
	cout << "请输入联系人电话" << endl;
	cin >> st.phone;
	strcpy_s(p->phone, st.phone);
	cout << "请输入联系人住址" << endl;
	cin >> st.address;
	strcpy_s(p->address, st.address);

	book.nember++;
	cout << "添加成功!" << endl;
	cout << "\n" << endl;

}

void show(struct Addressbook sbook) {
	int i ;
	if (sbook.nember == 0)
	{
		cout << "通讯录内无联系人!" << endl;
		return;
	}
	for ( i = 0; i < sbook.nember; i++)
	{
		cout << "姓名:" << sbook.Ary[i].name << "\t";
		cout << "性别:" << (sbook.Ary[i].sex == 1 ? "男":"女") << "\t";
		cout << "年龄:" << sbook.Ary[i].age << "\t";
		cout << "电话:" << sbook.Ary[i].phone << "\t";
		cout << "住址:" << sbook.Ary[i].address << endl;
	}

	cout << "已显示所有联系人信息!" << endl;

	system("pause");
	system("cls");

	createmenu();
	
}

int search_person(struct Addressbook sbook,string sname) {
	int i;

	for ( i = 0; i < sbook.nember; i++)
	{
		if (sbook.Ary[i].name == sname) {
			return i;
		}
	}
	return -1;
}

void delete_person(struct Addressbook& book, string sname) {
	if (search_person(book,sname) == -1)
	{
		cout << "通讯录内无此联系人" << endl;
	}
	else
	{
		int val = search_person(book, sname);//被删除联系人位置
		
		struct person* p = &(book.Ary[val]);
		for (int i = 1; i < (book.nember - val); i++)
		{
			++p;
			*(p - 1) = *p;
		}

		--book.nember;
		cout << "删除成功!" << endl;
	}
}

void modify_person(struct Addressbook& book, string sname) {
	int sex = 0;
	int age = 0;
	char phone[20] ;
	char address[20];
	int i = 0;

	int val = search_person(book, sname);
	struct person* p = &(book.Ary[val]);

	cout << "\n" << endl;
	cout << "\n" << endl;
	cout << "\t\t\t\t\t\t1、修改联系人性别" << endl;
	cout << "\t\t\t\t\t\t2、修改联系人年龄" << endl;
	cout << "\t\t\t\t\t\t3、修改联系人电话" << endl;
	cout << "\t\t\t\t\t\t4、修改联系人住址" << endl;
	cout << "\n" << endl;

	cout << "请输入所要修改联系人信息项前的功能号:" << endl;
	cin >> i;
	
	switch (i)
	{
	case 1:
		cout << "请输入联系人修改后性别:"  ;
		cin >> sex;
		p->sex = sex;
		cout << "修改成功!" << endl;
		break;
	case 2:
		cout << "请输入联系人修改后年龄:";
		cin >> age;
		p->age = age;
		cout << "修改成功!" << endl;
		break;
	case 3:
		cout << "请输入联系人修改后电话:";
		cin >> phone;
		strcpy_s(p->phone, phone);
		cout << "修改成功!" << endl;
		break;
	case 4:
		cout << "请输入联系人修改后住址:";
		cin >> address;
		strcpy_s(p->address, address);
		cout << "修改成功!" << endl;
	default:
		cout << "请输入正确的功能号:" << endl;
		modify_person(book,sname);
		break;
	}

	system("pause");
	system("cls");
}

void clean(struct Addressbook& book) {
	int i = 0;
	cout << "是否确定清空通讯录(1——是   2——否)" << endl;
	cin >> i;
	if (i == 1)
	{
		book.nember = 0;//逻辑清空
	}

	cout << "通讯录已清空成功!" << endl;

	system("pause");
	system("cls");
}

猜你喜欢

转载自blog.csdn.net/wild_idea/article/details/128445957
今日推荐