C++ 实现一个简单的通讯录管理系统

需求

通讯录是一个可以记录亲人、好友信息的工具。本教程主要利用C++来实现一个通讯录管理系统系统中需要实现的功能如下:

  • 添加联系人∶向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人;
  • 显示联系人:显示通讯录中所有联系人信息;
  • 删除联系人:按照姓名进行删除指定联系人;
  • 查找联系人︰按照姓名查看指定联系人信息;
  • 修改联系人:按照姓名重新修改指定联系人;
  • 清空联系人:清空通讯录中所有信息;
  • 退出通讯录:退出当前使用的通讯录;

代码

#include <iostream>
#include <string>
#define MAX 1000
using namespace std;

// 联系人结构体
struct Contacts
{
	string name;
	string sex;
	int age;
	string phone_number;
	string address;
};

// 通讯录结构体
struct Address_Books
{
	struct Contacts contactsArray[MAX];
	int AB_size;
};

// 显示菜单界面
void showMene()
{
	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;
}

// 添加联系人
void addContacts(Address_Books * ABs)
{
	// 先判断通讯录是否已满,如果满了不再添加
	if (ABs->AB_size == MAX)
	{
		cout << "通讯录已满。" << endl;
		return;
	}
	else
	{
		cout << "姓名:" << endl;
		cin >> ABs->contactsArray[ABs->AB_size].name;
		cout << "性别:" << endl;
		cin >> ABs->contactsArray[ABs->AB_size].sex;
		cout << "年龄:" << endl;
		cin >> ABs->contactsArray[ABs->AB_size].age;
		cout << "联系方式:" << endl;
		cin >> ABs->contactsArray[ABs->AB_size].phone_number;
		cout << "家庭地址:" << endl;
		cin >> ABs->contactsArray[ABs->AB_size].address;
		ABs->AB_size += 1;
		cout << "添加成功!" << endl;
		system("pause");
		system("cls");
	}
}

// 显示联系人
void printContacts(Address_Books *ABs)
{
	if (ABs->AB_size == 0)
	{
		cout << "通讯录列表为空,请先添加联系人吧!" << endl;
	}
	else
	{
		for (int i = 0; i < ABs->AB_size; i++)
		{
			cout << "姓名:" << ABs->contactsArray[i].name << "\t性别:" << ABs->contactsArray[i].sex
				<< "\t年龄:" << ABs->contactsArray[i].age << "\t联系方式:" << ABs->contactsArray[i].phone_number
				<< "\t家庭地址:" << ABs->contactsArray[i].address << endl;
		}
	}
	system("pause");
	system("cls");
}

// 删除联系人
void delContacts(Address_Books *ABs, string del_name)
{
	int flag = 0;
	for (int i = 0; i < ABs->AB_size; i++)
	{
		if (ABs->contactsArray[i].name == del_name)
		{
			for (int j = i; j < ABs->AB_size; j++)
			{
				ABs->contactsArray[j] = ABs->contactsArray[j + 1];
			}
			flag = 1;
			ABs->AB_size -= 1;
			cout << "删除成功!" << endl;
			break;
		}
	}
	if (flag == 0)
	{
		cout << "查无此人!" << endl;
	}
	system("pause");
	system("cls");
}

// 查找联系人
void serContacts(Address_Books *ABs, string ser_name)
{
	int flag = 0;
	for (int i = 0; i < ABs->AB_size; i++)
	{
		if (ABs->contactsArray[i].name == ser_name)
		{
			flag = 1;
			cout << "您查找的联系人信息如下:" << endl;
			cout << "姓名:" << ABs->contactsArray[i].name << "\t性别:" << ABs->contactsArray[i].sex
				<< "\t年龄:" << ABs->contactsArray[i].age << "\t联系方式:" << ABs->contactsArray[i].phone_number
				<< "\t家庭地址:" << ABs->contactsArray[i].address << endl;
		}
	}
	if (flag == 0)
	{
		cout << "查无此人!" << endl;
	}
	system("pause");
	system("cls");
}

// 修改联系人
void modifyContacts(Address_Books *ABs, string modify_name)
{
	int flag = 0;
	for (int i = 0; i < ABs->AB_size; i++)
	{
		if (ABs->contactsArray[i].name == modify_name)
		{
			flag = 1;
			cout << "请输入修改后的联系人信息:" << endl;
			cout << "姓名:" << endl;
			cin >> ABs->contactsArray[i].name;
			cout << "性别:" << endl;
			cin >> ABs->contactsArray[i].sex;
			cout << "年龄:" << endl;
			cin >> ABs->contactsArray[i].age;
			cout << "联系方式:" << endl;
			cin >> ABs->contactsArray[i].phone_number;
			cout << "家庭地址:" << endl;
			cin >> ABs->contactsArray[i].address;
			cout << "修改成功!" << endl;
			break;
		}
	}
	if (flag == 0)
	{
		cout << "查无此人!" << endl;
	}
	system("pause");
	system("cls");
}

// 清空联系人
void delallContacts(Address_Books *ABs, int del_select)
{
	if (del_select == 1)
	{
		ABs->AB_size = 0;
		cout << "已清空所有联系人!" << endl;
	}
	else
	{
		cout << "已取消!" << endl;
	}
	system("pause");
	system("cls");
}

int main()
{
	Address_Books ABs;
	ABs.AB_size = 0;
	int select = 0;
	int del_select = 0;
	string del_name;
	string ser_name;
	string modify_name;
	while (true)
	{
		showMene();
		cin >> select;
		switch (select)
		{

		case 1: // 1、添加联系人
			addContacts(&ABs); // 利用地址传递
			break;

		case 2: // 2、显示联系人
			printContacts(&ABs);
			break;

		case 3: // 3、删除联系人
			cout << "请输入要删除的联系人姓名:" << endl;
			cin >> del_name;
			delContacts(&ABs,del_name);
			break;

		case 4: //  4、查找联系人
			cout << "请输入要查找的联系人姓名:" << endl;
			cin >> ser_name;
			serContacts(&ABs, ser_name);
			break;

		case 5: // 5、修改联系人
			cout << "请输入要修改的联系人姓名:" << endl;
			cin >> modify_name;
			modifyContacts(&ABs, modify_name);
			break;

		case 6: // 6、清空联系人
			cout << "您确定要清空联系人吗?(1:仍要清空;其他数字:取消。)" << endl;
			cin >> del_select;
			delallContacts(&ABs, del_select);
			break;

		case 0: // 0、退出通讯录
			cout << "欢迎下次使用!" << endl;
			system("pause");
			return 0;
		}
	}
	system("pause");
	return 0;
}

总结

新手刚开始学C++做的第一个案例,也是第一次写CSDN博客,代码有什么改进的地方欢迎留言!

#define _CRT_SECURE_NO_WARNINGS 1 #ifndef __CONTACT_H_ #define __CONTACT_H_ #include<stdio.h> #include<stdlib.h> #include<string.h> #define PEO_MAX 30 #define STUID_MAX 10 #define NAME_MAX 20 #define SEX_MAX 10 #define TEL_MAX 15 typedef struct People { char id[STUID_MAX]; char name[NAME_MAX]; char sex[SEX_MAX]; char tel[TEL_MAX]; }*peo; typedef struct Contact { int count; struct People people[PEO_MAX]; }*pCon; void add_peo(pCon pcon); //添加联系人信息 void show_peo(pCon pcon); //显示指定联系人信息 void find_peo(pCon pcon); //查找联系人信息 void modify_peo(pCon pcon); //修改指定联系人信息 void clear_peo(pCon pcon); //清空联系人信息 void show_menu(); //菜单显示 #endif int search(pCon pcon, char *name) { int i = 0; for (i=0; i < pcon->count; i++) { if (strcmp(name, pcon->people[i].name) == 0) return i; } return -1; } void add_peo(pCon pcon) //添加联系人 { if (pcon->count == PEO_MAX) { printf("The contact has fullen."); return; } printf("please input studentID: "); scanf("%s",(pcon->people[pcon->count]).id); printf("please input name: "); scanf("%s", (pcon->people[pcon->count]).name); printf("please input sex : "); scanf("%s", (pcon->people[pcon->count]).sex); printf("please input tel: "); scanf("%s", (pcon->people[pcon->count]).tel); pcon->count++; } void show_peo(pCon pcon)//显示联系人 { int i = 0; for (; i < pcon->count; i++) { printf("studentID name sex tel \n"); printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } } void find_peo(pCon pcon) //查找联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to find:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } void modify_peo(pCon pcon) //修改指定联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to modify:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("please input studentID: "); scanf("%s", (pcon->people[i]).id); printf("please input name: "); scanf("%s", (pcon->people[i]).name); printf("please input sex : "); scanf("%s", (pcon->people[i]).sex); printf("please input tel: "); scanf("%s", (pcon->people[i]).tel); } void clear_peo(pCon pcon) //清空联系人 { pcon->count = 0; } void show_menu() //菜单显示 { printf("==========通讯录==========\n"); printf(" \n"); printf("*******[1]显示通讯录******\n"); printf("*******[2]查 询******\n"); printf("*******[3]修 改******\n"); printf("*******[4]结 束******\n"); } int main() { struct Contact my_contact; int input = 1; my_contact.count = 0; while (input) { show_menu(); printf("please input:"); scanf("%d", &input;); switch (input) { case 1: add_peo(&my;_contact); break; case 2: show_peo(&my;_contact); break; case 3: find_peo(&my;_contact); break; case 4: modify_peo(&my;_contact); break; case 5: clear_peo(&my;_contact); break; default: break; } } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值