通讯录管理系统——C++版-创新互联

通讯录管理

创新互联建站专业提供成都温江机房服务,为用户提供五星数据中心、电信、双线接入解决方案,用户可自行在线购买成都温江机房服务,并享受7*24小时金牌售后服务。

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");
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网页题目:通讯录管理系统——C++版-创新互联
转载源于:http://azwzsj.com/article/gejoh.html