通讯录管理系统

通讯录管理系统

系统需求
1.添加联系人
2.显示联系人
3.删除联系人
4.查找联系人
5.修改联系人
6.清空联系人
7.退出通讯录
此外还做了一些改进,比如在输入年龄时输入了字符,比如输入姓名和电话时输入的和之前相同的姓名或电话,比如在修改时也出现了姓名和电话与之前输入的相同的情况,都对这些可能出现的情况进行了解决,同时也欢迎大家发现更多可能出现的问题,大家一起来交流学习

#include<iostream>
using namespace std;
/*系统需求
1.添加联系人//当姓名或电话相同时提示用户进行修改
2.显示联系人
3.删除联系人
4.查找联系人
5.修改联系人
6.清空联系人
7.退出通讯录
*/
#define Max 100
struct person
{
	string name;
	int sex{};//性别:1男,2女
	int age{};
	string telephonenumber;
	string address;
};
struct addressbook
{
	struct person personarr[Max];
	int renshu{};
};
void showmemenu()
{
	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 addperson(addressbook*p)
{
	//判断通讯录满了没有,如果满了就不要添加了
	if(p->renshu==Max)
	{
		cout << "通讯录已满,无法添加!" << endl;
		return;
	}
	else
	{
		//添加具体联系人
		//姓名
		string nameinput;
		cout << "please input name" << endl;
		FLAG3:
		cin >> nameinput;
		int a = p->renshu;
		while(a>=0)
		{
			if (nameinput == p->personarr[a].name)
			{
				cout << "The name is repeated, please re-enter" << endl;
				goto FLAG3;
			}
			else
			{
				a--;
			}
		}
		p->personarr[p->renshu].name = nameinput;
	    
		
		//性别
		cout<< "please input sex" << endl;
		cout << "1--男" << endl;
		cout << "2--女" << endl;
		int inputsex = 0;
		while (true)
		{
			cin >> inputsex;
			while (cin.fail())//如果输入字符时
			{
				cin.clear();
				cin.ignore(1024, '\n');
				cout << "You can only enter numbers." << endl;
				cout << "Enter a number:" << endl;
				cin >> inputsex;
			}
		    if (inputsex == 1 || inputsex == 2)
				{
					p->personarr[p->renshu].sex = inputsex;
					break;//如果输入1或2可以退出循环,否则要重新输入
				}
				else
				{
					cout << "wrong,please input one or two" << endl;
				}

			}
		
		//年龄
		cout << "please input age" << endl;
		int inputage = 0;
		cin >> inputage;
		while (cin.fail())//如果输入字符时
		{
			cin.clear();
			cin.ignore(1024, '\n');
			cout << "You can only enter numbers." << endl;
			cout << "Enter a number:" << endl;
			cin >> inputage;
		}
		p->personarr[p->renshu].age = inputage;

		//电话
		cout << "please input telephone number" << endl;
		string inputnum;
		FLAG4:
		cin >> inputnum;
		int b = p->renshu;
		while (b >= 0)
		{
			if (inputnum == p->personarr[b].telephonenumber)
			{
				cout << "the num is repeated,please re-enter" << endl;
				goto FLAG4;
			}
			else
			{
				b--;
			}
		}
		p->personarr[p->renshu].telephonenumber = inputnum;

		//地址
		cout << "please input address" << endl;
		string inputaddress;
		cin >> inputaddress;
		p->personarr[p->renshu].address = inputaddress;

		//更新通讯录人数
		p->renshu++;
		cout << "Successfully added" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏

	}
}
void showperson(addressbook* p)
{
	//判断通讯录中人数是否为0,如果为0,提示记录为空
	//如果不为0,显示记录的联系人信息
	if(p->renshu==0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < p->renshu; i++)
		{
			cout << "姓名:" << p->personarr[i].name << "\t";
			cout << "性别:" << (p->personarr[i].sex ==1?"男":"女") << "\t";
			cout << "年龄:" << p->personarr[i].age << "\t";
			cout << "电话:" << p->personarr[i].telephonenumber << "\t";
			cout << "住址:" << p->personarr[i].address << endl;
		}
	}
	system("pause");//请按任意键继续
	system("cls");//清屏
}
//检测联系人是否存在,如果存在,返回下标编号,不存在的话,返回-1
int isexist(addressbook* p, string inputname)//参数1通讯录,参数2对比姓名
{
	for (int i = 0; i < p->renshu; i++)
	{
		if (p->personarr[i].name == inputname)
		{
			return i;
		}
	}
	return -1;
}

void deleteperson(addressbook* p, string inputname)
{
	//cout << "please input the person's name that you want to delete" << endl;
	//string inputname;
	//cin >> inputname;
	int a = isexist(p, inputname);
	//if (a != -1)//查到了,要删除这个人,需要把他后面的数据前移一位,并且让通讯录里面人数-1
	//{
		while( a <= p->renshu)
		{ 
			p->personarr[a] = p->personarr[a + 1];
			a++;
		}
		p->renshu = p->renshu - 1;
		cout << "Deletion succeeded" << endl;
	//}
	//else
	//{
		//cout << "No person found" << endl;
	//}

}

void modifyperson(addressbook* p)//修改指定的联系人
{
	cout << "Please enter the person's name you want to modify" << endl;
	string inputname;
	cin >> inputname;
	int a=isexist(p,inputname);
	if (a != -1)//找到了指定的联系人
	{
		cout << "please choose the part you want to modify" << endl;
		cout<<"1.姓名;2.性别;3.年龄;4.电话;5.住址"<<endl;
		int b;
		FLAG2:
	 	cin >> b;
		switch (b)
		{
		case 1:
		{
			string name;
		    cout << "please input new name" << endl;
			FLAG5:
		    cin >> name;
			int q = p->renshu;
			while (q >= 0)
			{
				if (name == p->personarr[q].name)
				{
					cout << "the name is repeated,please re-enter" << endl;
						goto FLAG5;
				}
				else
				{
					q--;
				}
			}
		    p->personarr[a].name = name;
			cout << "Modification succeeded" << endl;
			system("pause");//请按任意键继续
			system("cls");//清屏
		}
			break;
		case 2://性别
		{
			int sex;
			cout << "please input new sex" << endl;
			while (true)
			{
				cin >> sex;
				while (cin.fail())//如果输入字符时
				{
					cin.clear();
					cin.ignore(1024, '\n');
					cout << "You can only enter numbers." << endl;
					cout << "Enter a number:" << endl;
					cin >> sex;
				}
				if (sex == 1 || sex == 2)
				{
					p->personarr[a].sex = sex;
					break;//如果输入1或2可以退出循环,否则要重新输入
				}
				cout << "wrong,please input one or two" << endl;
			}
			
		}
		cout << "Modification succeeded" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏
			break;
		case 3://年龄
		{
			int age;
			cout << "please input new age" << endl;
			cin >> age;
			while (cin.fail())//如果输入字符时
			{
				cin.clear();
				cin.ignore(1024, '\n');
				cout << "You can only enter numbers." << endl;
				cout << "Enter a number:" << endl;
				cin >> age;
			}
			p->personarr[a].age = age;
		}
		cout << "Modification succeeded" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏
			break;
		case 4://电话
		{
			string telephonenum;
			cout << "please input new number" << endl;
			FLAG6:
			cin >> telephonenum;
			int w = p->renshu;
			while (w >= 0)
			{
				if (telephonenum == p->personarr[w].telephonenumber)
				{
					cout << "the num is repeated,please re-enter" << endl;
					goto FLAG6;
				}
				else
				{
					w--;
				}
			}
			p->personarr[a].telephonenumber= telephonenum;
		}
		cout << "Modification succeeded" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏
			break;
		case 5://地址
		{
			string address;
			cout << "please input new address" << endl;
			cin >> address;
			p->personarr[a].address = address;
		}
		cout << "Modification succeeded" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏
			break;
		default:
			cout << "please input the right number!" << endl;
			system("pause");//请按任意键继续
			goto FLAG2;
			break;
		}
	}
	else
	{
		cout << "No person found" << endl;
	}

}
void clearperson(addressbook* p)//只要将通讯录记录的联系人数置为0,这样就实现了逻辑上的清空
{
	p->renshu = 0;
	cout << "Clear completed" << endl;
	system("pause");
	system("cls");
}
int main()
{
	int shurushuzi;
	addressbook abs;
	//初始化通讯录中当前人员个数
	abs.renshu = 0;
FLAG:
	showmemenu();
	cout << "当前联系人个数" << abs.renshu << endl;
	cin >> shurushuzi;
 switch (shurushuzi)
	{
	case 1:
		addperson(&abs);//利用地址传递修改实参
		goto FLAG;
		break;
	case 2:
		showperson(&abs);
		goto FLAG;
		break; 
	case 3:
	{
		cout << "please input the person's name that you want to delete" << endl;
		string inputname;
		cin >> inputname;
		isexist(&abs, inputname);
		if (isexist(&abs, inputname) == -1)
		{
			cout << "No person found" << endl;
		}
		else
		{
			deleteperson(&abs, inputname);
		}
		system("pause");//请按任意键继续
		system("cls");//清屏
	}
		goto FLAG;
		break;
	case 4:
	{
		cout << "please input the person's name that you want to find" << endl;
		string inputname;
		cin >> inputname;
		isexist(&abs, inputname);
		if (isexist(&abs, inputname) == -1)
		{
			cout << "No person found" << endl;
		}
		else
		{
			cout << "姓名:" << abs.personarr[isexist(&abs, inputname)].name << "\t";
			cout << "性别:" << (abs.personarr[isexist(&abs, inputname)].sex == 1 ? "男" : "女") << "\t";
			cout << "年龄:" << abs.personarr[isexist(&abs, inputname)].age << "\t";
			cout << "电话:" << abs.personarr[isexist(&abs, inputname)].telephonenumber << "\t";
			cout << "住址:" << abs.personarr[isexist(&abs, inputname)].address << endl;
		}
		system("pause");//请按任意键继续
		system("cls");//清屏
	}
		goto FLAG;
		break;
	case 5:
		modifyperson(&abs);
		goto FLAG;
		break;
	case 6:
		clearperson(&abs);
		goto FLAG;
		break; 
	case 0:
		cout << "欢迎下次使用" << endl;
		system("pause");
		return 0;
		break;
	default:
		cout << "please input the right number!" << endl;
		system("pause");//请按任意键继续
		system("cls");//清屏
		goto FLAG;
	
	}

}

注:这是本人初学C++时完成的代码,只学习了最基础的C++知识,所以可能代码看起来十分的呆板…

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值