C++前期知识实现简单的通讯录系统

2021/9/27 14:11:02

本文主要是介绍C++前期知识实现简单的通讯录系统,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#include<iostream>
using namespace std;
void showMenu();
void end0();
#define max  1000



struct News
{
	string name;
	string sex;
	int age;
	int phone;
	string site;
};
struct books
{
	struct News news[max];
	int size;
};

void showMenu()
{
	cout << " 1.添加联系人" << endl;
	cout << " 2.显示联系人" << endl;
	cout << " 3.删除联系人" << endl;
	cout << " 4.查找联系人 " << endl;
	cout << " 5.修改联系人" << endl;
	cout << " 6.清空联系人" << endl;
	cout << " 0.退出通讯录" << endl;

}

void end0()                    //退出通讯录
{
	cout << "欢迎下次使用" << endl;
	system("pause");
}

void addpeople(books*abc)  //添加联系人用函数
{
	if (abc->size < 1000)
	{
		cout << "输入联系人姓名" << endl;
		cin >> abc->news[abc->size].name;
		cout << "输入联系人性别" << endl;
		cin >> abc->news[abc->size].sex;
		cout << "输入联系人年龄" << endl;
		cin >> abc->news[abc->size].age;
		cout << "输入联系人电话" << endl;
		cin >> abc->news[abc->size].phone;
		cout << "输入联系人地址" << endl;
		cin >> abc->news[abc->size].site;
		abc->size++;
	}
	else
	{
		cout << "通讯录已满,请删除部分信息再试" << endl;
		return;
	}
	cout << "存入成功" << endl;
	system("pause");
	system("cls");
}

void showpeopel(books*abc)     //显示联系人用函数
{
	if (abc->size ==0)
	{
		cout << "当前记录为空" << endl;
	}
	else 
	{
		for (int i = 0; i < abc->size; i++)
		{
			cout << "联系人姓名" <<"  "<< abc->news[i].name << endl;
			cout << "联系人性别" <<"  "<< abc->news[i].sex << endl;
			cout << "联系人年龄" << "  " << abc->news[i].age<< endl;
			cout << "联系人电话" << "  " << abc->news[i].phone << endl;
			cout << "联系人地址" << "  " << abc->news[i].site << endl;
		}
	}
	system("pause");
	system("cls");

}

int exist(books*abc,string name)  //检测联系人是否存在
{
	for (int i = 0; i < abc->size; i++)
	{
		if (name == abc->news[i].name)
		{
			return i;
		}
		else
		{
			return -1;
		}
	}
}

void deletepeople(books* abc)      //删除联系人
{
	string name;
		cout << "输入要删除的联系人姓名" << endl;
		cin>>name;
		if (exist(abc, name) == -1)
		{
			cout << "查无此人,请重新输入" << endl;
		}
		else
		{
			int i = exist(abc, name);
			for (i; i < abc->size; i++)
			{
				abc->news[i] = abc->news[i + 1];
			}
			abc->size--;
		}
		system("pause");
		system("cls");
}


void seekpeople(books*abc)   //查找联系人
{
	string name;
	cout << "请输入要查找的联系人姓名" << endl;
	cin >> name;
	exist(abc, name);
	int i = exist(abc, name);
	if (i == -1)
	{
		cout << "查无此人,请重新输入" << endl;
	}
	else 
	{
		cout << "姓名"<<"  " << abc->news[i].name << endl;
		cout << "性别" << "  " << abc->news[i].sex << endl;
		cout << "年龄" << "  " << abc->news[i].age << endl;
		cout << "电话" << "  " << abc->news[i].phone << endl;
		cout << "地址" << "  " << abc->news[i].site << endl;
	}
	system("pause");
	system("cls");
}

void revisepeople(books* abc)    //修改联系人
{
	string name;
	cout << "输入要修改的联系人姓名" << endl;
	cin >> name;
	int i = exist(abc, name);
	if (i == -1)
	{
		cout << "查无此人,请重新输入" << endl;
	}
	else
	{
		cout << "请输入联系人姓名" << endl; 
		cin >> abc->news[i].name;
		cout << "请输入联系人性别" << endl;  
		cin >> abc->news[i].sex;
		cout << "请输入联系人年龄" << endl;   
		cin >> abc->news[i].age;
		cout << "请输入联系人电话" << endl;  
		cin >> abc->news[i].phone;
		cout << "请输入联系人地址" << endl;   
		cin >> abc->news[i].site;
	}
	system("pause");
	system("cls");
}

void cleanpeople(books* abc) //清空联系人
{
	abc->size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");
	system("cls");
}
int main()
{
	books abc;
	abc.size = 0;
	while (true) {
		//菜单
		showMenu();
		/*
		1.添加联系人
		2.显示联系人
		3.删除联系人
		4.查找联系人
		5.修改联系人
		6.清空联系人
		0.退出通讯录
		*/
		int a;
		cout << "选择所要继续的功能" << endl;
		cin >> a;
		switch (a)
		{
		case 1:
			addpeople(&abc);   //添加联系人
			break;

		case 2:
			showpeopel(&abc);   //显示全部联系人
			break;


		case 3:                 //删除联系人
			deletepeople(&abc);
		break;
		


		case 4:
			seekpeople(&abc);    //查找联系人
			break;


		case 5:
			revisepeople(&abc);  //修改联系人 
			break;


		case 6:
			cleanpeople(&abc);  //清空联系人
			break;


		case 0:
			end0();
			return 0;
			break;
			
		}

	}
}

距离上仙新衣服还有3天  上仙吾辈 吾辈 还可以再长一个肝(昨天早上7点爬起来到现在没有闭过眼的某肝帝)   --一只符华单推人



这篇关于C++前期知识实现简单的通讯录系统的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程