C++通讯录管理系统

新建空项目   ;  添加源文件.cpp(可中文)
#include <iostream>
using namespace std;
#include <string >
#define MAX 1000
//菜单界面
void showmenu()
{
	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;
	cout << "****************************" << endl;
	cout << "****************************" << endl;
	cout << "******************请选择功能" << endl;
}
//退出功能------0

struct information// 生成信息的结构体
{
	string name;
	string sex;
	int age;
	string phone;
	string address;
};
struct TXL//生成通讯录结构体
{
	struct information people[MAX];
	int num;//人员编号
};


// 添加联系人
void add(TXL *abs)
{
	if (abs->num > 1000)
	{
		cout << "内存已满" << endl;
		return;
	}
	else// 添加信息
	{
		//输入姓名
		string name;
		cout << "输入姓名" << endl;
		cin >> name;
		abs->people[abs->num].name = name;
		//性别
		cout << "输入性别" << endl;
		cout << "1-----男" << endl;
		cout << "2-----女" << endl;
		int sex = 0;
		while (true)
		{
			
			cin >> sex;
			if (sex == 1)
			{
				abs->people[abs->num].sex = "男";
				break;
			}
			else if (sex == 2)
			{
				abs->people[abs->num].sex = "女";
				break;
			}
			else
			{
				cout << "输入错误,重新输入" << endl;
			}
		}
		//年龄
		int age = 0;
		cout << "输入年龄" << endl;
		cin >> age;
		abs->people[abs->num].age = age;
		//电话电话
		string phone;
		cout << "输入电话号码" << endl;
		cin >> phone;
		abs->people[abs->num].phone = phone;
		//住址
		string address;
		cout << "输入住址" << endl;
		cin >> address;
		abs->people[abs->num].address = address;

		cout << "输入成功" << endl;	
		abs->num++;
		system("pause");//任意键继续
		system("cls");//清屏
		showmenu();
	}
}

//显示所有联系人
void show(TXL *abs)
{
	if (abs->num == 0)
	{
		cout << "还没有添加数据" << endl;
	}
	else 
	{
		for (int i = 0; i < abs->num; i++)
		{
			cout << "编号:" << abs->num << endl;
			cout<<"姓名:" << abs->people[i].name << "\t";
			cout << "年龄:" << abs->people[i].age << "\t";
			cout << "性别:" << abs->people[i].sex << "\t";
			cout<< "电话:" << abs->people[i].phone << "\t";
			cout<< "住址:" << abs->people[i].address<< endl;
		}
	}
	system("pause");
	system("cls");
	showmenu();
}

//检测联系人是否存在 有返回编号,没有返回-1
int isExist(TXL *abs , string name)
{
	for (int i = 0; i < abs->num; i++)
	{
		if (abs->people[i].name == name)
		{
			cout << "编号" << i << endl;
			return i;
		}
	}
	return -1;


}

//删除联系人
void delet(TXL* abs)
{
	cout << "输入要删除联系人姓名" << endl;
	string name;
	cin >> name;
	int ret =isExist(abs, name);
	if (ret == -1)
	{
		cout << "查无此人" << endl;
	}
	else
	{
		//删除功能
		for (int i = ret; i < abs->num; i++)
		{
			//数据前移,覆盖要删除的数据
			abs->people[i] = abs->people[i + 1];
		}
		abs->num--;//更新通讯录人员编号
		cout << "删除成功" << endl;
	}
	system("pause");
	system("cls");
	showmenu();
}

//查找联系人
void find(TXL* abs)
{
	cout << "输入要查找联系人姓名" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret == -1)
	{
		cout << "查无此人" << endl;
	}
	else
	{
		//查找显示功能
		cout << "编号:" << abs->num << endl;
		cout << "姓名:" << abs->people[ret].name << "\t";
		cout << "年龄:" << abs->people[ret].age << "\t";
		cout << "性别:" << abs->people[ret].sex << "\t";
		cout << "电话:" << abs->people[ret].phone << "\t";
		cout << "住址:" << abs->people[ret].address << endl;
	}
	system("pause");
	system("cls");
	showmenu();
}

//修改联系人
void mod(TXL* abs)
{
	cout << "输入要修改联系人姓名" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret == -1)
	{
		cout << "查无此人" << endl;
	}
	else
	{
		//修改功能
		string name;
		cout << "输入姓名" << endl;
		cin >> name;
		abs->people[ret].name = name;
		//性别
		cout << "输入性别" << endl;
		cout << "1-----男" << endl;
		cout << "2-----女" << endl;
		int sex = 0;
		while (true)
		{

			cin >> sex;
			if (sex == 1)
			{
				abs->people[ret].sex = "男";
				break;
			}
			else if (sex == 2)
			{
				abs->people[ret].sex = "女";
				break;
			}
			else
			{
				cout << "输入错误,重新输入" << endl;
			}
		}
		//年龄
		int age = 0;
		cout << "输入年龄" << endl;
		cin >> age;
		abs->people[ret].age = age;
		//电话电话
		string phone;
		cout << "输入电话号码" << endl;
		cin >> phone;
		abs->people[ret].phone = phone;
		//住址
		string address;
		cout << "输入住址" << endl;
		cin >> address;
		abs->people[ret].address = address;

		cout << "输入成功" << endl;
		system("pause");//任意键继续
		system("cls");//清屏
		showmenu();
	}
	system("pause");
	system("cls");
	showmenu();
}

//清空联系人
void clean(TXL* abs)
{
	cout << "是否确定要清空" << endl;
	cout << "1----确定要清空" << endl;
	cout << "2----点错了" << endl;
	int a = 0;
	cin >> a;
	if (a == 1)
	{
		abs->num = 0;
		cout << "已清空" << endl;		
	}
	else if(a==2)
	{
		system("pause");
		system("cls");
		showmenu();
	}
	else
	{
		cout << "无此选项" << endl;
		system("pause");
		system("cls");
		showmenu();
	}
	system("pause");
	system("cls");
	showmenu();
}
//按错键
void aaa(TXL* abs)
{
	cout << "没有所选择的功能" << endl;
	system("pause");
	system("cls");
	showmenu();
}


int main()
{
	TXL abs;// abs包括 信息和编号 33-34
	abs.num = 0;
	
	//菜单调用
	showmenu();
	//分支结构   按键不同效果
	
	while (true)//  按下非0 键就一直在界面中
	{
		int i = 0;
		cin >> i;//用户的按键
		switch (i)
		{
		case 0://退出
			cout << "欢迎下次再来" << endl;
			system("pause");
			return 0;
			break;//退出
		case 1://1、添加联系人
			add(&abs);//添加人员到通讯录
			break;
		case 2://显示联系人
			show(&abs);
			break;
		case 3://删除联系人			
		
		//if (isExist(&abs, name) == -1)//检查有没有这个人
		//{
		//	cout << "查无此人" << endl;
		//}
		//else
		//{
		//	cout << "找到了" << endl;
		//}} ///在case中,如果break前面代码太多会报错,需要把break前的代码加上{  }
			delet(&abs);
			break;
		case 4://查找
			find(&abs);
			break;
		case 5://修改
			mod(&abs);
			break;
		case 6://清空
			clean(&abs);
			break;
		default://退出
			aaa(&abs);
			break;

		}
	}
	


	system("pause");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值