栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实战 —— 通讯录管理系统

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实战 —— 通讯录管理系统

代码编写
#include 
#include 
#include 
using namespace std;

const int MAX = 100;//通讯录容量

//联系人结构体
struct Persion
{
	//姓名
	string m_Name;
	//性别 1:男 2:女
	int m_Sex;
	//年龄
	int m_Age;
	//电话
	string m_Phone;
	//住址
	string m_Address;
};

//通讯录结构体
struct AddressBooks
{
	//联系人数组
	struct Persion personArray[MAX];
	//联系人数量
	int m_Size;
};

//展示菜单
void showMenu()
{
	cout << "***************************" << endl;
	cout << "*****  1、添加联系人  *****" << endl;
	cout << "*****  2、显示联系人  *****" << endl;
	cout << "*****  3、删除联系人  *****" << endl;
	cout << "*****  4、查找联系人  *****" << endl;
	cout << "*****  5、修改联系人  *****" << endl;
	cout << "*****  6、清空联系人  *****" << endl;
	cout << "*****  7、保存到本地  *****" << endl;
	cout << "*****  0、退出通讯录  *****" << endl;
	cout << "***************************" << endl;
}

//1、添加联系人
void addPerson(AddressBooks& abs)
{
	//判断通讯录是否已满
	if (abs.m_Size == MAX)
	{
		cout << "通讯录已满,无法添加!" << endl;
		return;
	}
	else
	{
		//开始添加联系人
		//姓名
		string name;
		cout << "请输入姓名: " << endl;
		cin >> name;
		abs.personArray[abs.m_Size].m_Name = name;
		//性别
		int sex;
		cout << "请输入性别(1:男 2:女): " << endl;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs.personArray[abs.m_Size].m_Sex = sex;
				break;
			}
			else
			{
				cout << "输入有误,请重新输入(1:男 2:女): " << endl;
			}

		}
		//年龄
		int age;
		cout << "请输入年龄: " << endl;
		cin >> age;
		abs.personArray[abs.m_Size].m_Age = age;
		//电话
		string phone;
		cout << "请输入电话: " << endl;
		cin >> phone;
		abs.personArray[abs.m_Size].m_Phone = phone;
		//地址
		string address;
		cout << "请输入地址: " << endl;
		cin >> address;
		abs.personArray[abs.m_Size].m_Address = address;
		//更新人数
		abs.m_Size++;

		cout << "添加成功!" << endl;
		system("pause");//请按任意键继续
		system("cls");	//清屏

	}
}

//2、显示联系人
void showPerson(AddressBooks abs)
{
	if (abs.m_Size==0)
	{
		cout << "联系人为空!" << endl;
		system("pause");//请按任意键继续
		system("cls");	//清屏
		return;
	}
	for (int i = 0; i < abs.m_Size; i++)
	{
		cout << "姓名:" << abs.personArray[i].m_Name << "t";
		cout << "性别:" << (abs.personArray[i].m_Sex== 1 ? "男" : "女" )<< "t";
		cout << "年龄:" << abs.personArray[i].m_Age << "t";
		cout << "电话:" << abs.personArray[i].m_Phone << "t";
		cout << "住址:" << abs.personArray[i].m_Address << endl;
	}
	cout << "所有联系人已加载完毕" << endl;
	system("pause");//请按任意键继续
	system("cls");	//清屏
}

//检索联系人,返回位置,不存在则返回-1
int isExist(AddressBooks abs, string name)
{
	if (abs.m_Size==0)
	{
		return -1;
	}
	for (int i = 0; i < abs.m_Size; i++)
	{
		if (abs.personArray[i].m_Name == name)
			return i;
	}
	return -1;
}

//3、删除联系人
void deletePerson(AddressBooks& abs)
{
	string name;
	cout << "请输入联系人姓名:" << endl;
	cin >> name;
	int is_exist = isExist(abs, name);
	if (is_exist==-1)
	{
		cout << "联系人不存在。" << endl;
	}
	else
	{
		for (int i = is_exist; i < abs.m_Size; i++)
		{
			abs.personArray[i] = abs.personArray[i + 1];
		}
		cout << "删除成功" << endl;
		abs.m_Size--;
	}
	system("pause");//请按任意键继续
	system("cls");	//清屏
}

//4、查找联系人
void findPerson(AddressBooks abs)
{
	cout << "请输入联系人姓名:" << endl;
	string name;
	cin >> name;
	int is_exist = isExist(abs, name);
	if (is_exist == -1)
	{
		cout << "联系人不存在!" << endl;
	}
	else
	{
		cout << "姓名:" << abs.personArray[is_exist].m_Name << "t";
		cout << "性别:" << (abs.personArray[is_exist].m_Sex == 1 ? "男" : "女") << "t";
		cout << "年龄:" << abs.personArray[is_exist].m_Age << "t";
		cout << "电话:" << abs.personArray[is_exist].m_Phone << "t";
		cout << "住址:" << abs.personArray[is_exist].m_Address << endl;
	}
	system("pause");//请按任意键继续
	system("cls");	//清屏
}

//5、修改联系人
void modifyPerson(AddressBooks& abs)
{
	cout << "请输入联系人姓名:" << endl;
	string name;
	cin >> name;
	int is_exist = isExist(abs, name);
	if (is_exist == -1)
	{
		cout << "联系人不存在!" << endl;
	}
	else
	{
		//姓名
		string name;
		cout << "请输入姓名: " << endl;
		cin >> name;
		abs.personArray[is_exist].m_Name = name;
		//性别
		int sex;
		cout << "请输入性别(1:男 2:女): " << endl;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs.personArray[is_exist].m_Sex = sex;
				break;
			}
			else
			{
				cout << "输入有误,请重新输入(1:男 2:女): " << endl;
			}

		}
		//年龄
		int age;
		cout << "请输入年龄: " << endl;
		cin >> age;
		abs.personArray[is_exist].m_Age = age;
		//电话
		string phone;
		cout << "请输入电话: " << endl;
		cin >> phone;
		abs.personArray[is_exist].m_Phone = phone;
		//地址
		string address;
		cout << "请输入地址: " << endl;
		cin >> address;
		abs.personArray[is_exist].m_Address = address;
	}
	system("pause");//请按任意键继续
	system("cls");	//清屏
}

//6、清空联系人
void clearPerson(AddressBooks& abs)
{
	cout << "您确定要清空联系人吗?(是 y 否 n)" << endl;
	string confirm;
	cin >> confirm;
	if (confirm == "y")
	{
		abs.m_Size = 0;
		cout << "成功!" << endl;
	}
	system("pause");//请按任意键继续
	system("cls");	//清屏
}

//7、保存通讯录到文件
void saveAddressBooks(AddressBooks abs)
{
	ofstream fout;	//创建输出文件流对象fout,用法与cout相同
	fout.open("通讯录.txt");	//与文件"通讯录.txt"建立连接,默认指针指向文件首,即每次写文件都会覆盖原文件
	for (int i = 0; i < abs.m_Size; i++)
	{
		fout << "姓名: " << abs.personArray[i].m_Name << "t";
		fout << "性别: " << (abs.personArray[i].m_Sex == 1 ? "男" : "女") << "t";
		fout << "年龄: " << abs.personArray[i].m_Age << "t";
		fout << "电话: " << abs.personArray[i].m_Phone << "t";
		fout << "住址: " << abs.personArray[i].m_Address << endl;
	}
	fout.close();	//断开文件连接,其实是断开程序与文件之间的“流”
	cout << "成功!" << endl;
	system("pause");//请按任意键继续
	system("cls");	//清屏
}

//读取本地存档,程序启动时自动完成
void getAddressBook(AddressBooks& abs)
{
	ifstream fin;	//创建输入文件流对象fin,用法与cin相同
	fin.open("通讯录.txt");
	if (!fin.is_open())
	{
		cout << "文件打开失败" << endl;
		return;
	}
	string name, sex, phone, address;	//文件中sex以字符串方式表示
	int age;
	string str;		//吸收文件中多余字符串
	while (fin>>str)	//fin读到EOF时停止,str吸收多余字符串"姓名:"
	{
		fin >> name; abs.personArray[abs.m_Size].m_Name = name;	//以空格为区分读取字符串
		fin >> str >> sex; abs.personArray[abs.m_Size].m_Sex = (int)(sex == "男" ? 1 : 2);
		fin >> str >> age; abs.personArray[abs.m_Size].m_Age = age;
		fin >> str >> phone; abs.personArray[abs.m_Size].m_Phone = phone;
		fin >> str >> address; abs.personArray[abs.m_Size].m_Address = address;
		abs.m_Size++;
	}

}

int main()
{
	AddressBooks abs;
	abs.m_Size = 0;

	//读取本地文件
	getAddressBook(abs);

	while (true)
	{

		showMenu(); 
		
		int select;
		cin >> select;

		switch (select)
		{
		case 0:		//0、退出通讯录
			cout << "感谢使用!" << endl;
			system("pause");
			return 0;
			break;
		case 1:		//1、添加联系人
			addPerson(abs);
			break;
		case 2:		//2、显示联系人
			showPerson(abs);
			break;
		case 3:		//3、删除联系人 
			deletePerson(abs);
			break;
		case 4:		//4、查找联系人 
			findPerson(abs);
			break;
		case 5:		//5、修改联系人
			modifyPerson(abs);
			break;
		case 6:		//6、清空联系人
			clearPerson(abs);
			break;
		case 7:		//7、保存通讯录到文件
			saveAddressBooks(abs);
			break;
		default:
			break;
		}
	}

	return 0;
}
运行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004737.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号