(C++)基础练习项目——通讯录管理系统

通讯录管理系统

需求:

1.添加联系人
2.显示联系人
3.删除联系人
4.查找联系人
5.修改联系人
6.清空联系人
#include <iostream>
using namespace std;
#include <string>
#define MAX 1000

//Set up a Person struct to lay in the data
struct Person
{
	string Name;
	int Sex;
	int Age;
	string Phone;
	string Addr;
};
//Set up a Addressbooks struct to lay in person information
struct Addressbooks
{
	struct Person personArray[MAX];
	int Size;
};
//A Function to show the fundamental function
void showMenu()
{
	cout << "********************************" << endl;
	cout <<	"*****   1.Add Member       *****" << endl;
	cout <<	"*****   2.Show Member      *****" << endl;
	cout <<	"*****   3.Delete Member    *****" << endl;
	cout <<	"*****   4.Search Member    *****" << endl;
	cout <<	"*****   5.Edit Member      *****" << endl;
	cout <<	"*****   6.Clean up Member  *****" << endl;
	cout <<	"*****   0.Exit             *****" << endl;
	cout << "********************************" << endl;
	cout << "Please choose the number" << endl;
}
void addPerson(Addressbooks * abs)
{	
	//To judge whether the Addressbooks is full , if not , enter the data 
	if (abs -> Size == MAX)
	{
		cout <<" The List is full!" <<endl;
		return;
	}
	else
	{
	string name;
	cout << "Please set the name:"<<endl;
	cin >> name;
	abs ->personArray[abs->Size].Name = name;
	}
	cout << "Please set the sex:" << endl;
	cout << " *1 --- ♂* " << endl;
	cout << " *2 --- ♀* " << endl;
	int sex = 0;
	//enter 1 or 2 to jump out of the circulation , if not , continue the circulation
	while(true)
	{
		cin >> sex;
		if (sex == 1 || sex == 2)
		{
			abs -> personArray[abs->Size].Sex = sex;
			break;
		}
		else
		{
		cout << "Error.Please reset the Sex" << endl;
		}
	}
	//enter the data of Person
	cout << "Please set the Age:" << endl;
	int age = 0;
	cin >> age;
	abs -> personArray[abs -> Size].Age = age;

	cout << "Please set the Number:" << endl;
	string phone;
	cin >> phone;
	abs -> personArray[abs -> Size].Phone = phone;

	cout << "Please set the Address:" << endl;
	string address;
	cin >> address;
	abs -> personArray[abs -> Size].Addr = address;

	abs -> Size++;

	cout << "Success" << endl;
	system("pause");
	system("cls");
}
//This function can show all the person data in the addressbook
void showPerson(Addressbooks *abs)
{
	if(abs -> Size == 0)
	{
		cout << "Empty" << endl;
	}
	else
	{
		for (int i = 0; i < abs->Size; i++)
		{
			cout << "Name:" << abs -> personArray[i].Name << "\t"; 
			cout << "Sex:" << (abs -> personArray[i].Sex == 1? " ♂ " : " ♀ ") << "\t"; 
			cout << "Age:" << abs -> personArray[i].Age << "\t"; 
			cout << "PhoneNumber:" << abs -> personArray[i].Phone << "\t"; 
			cout << "Address:" << abs -> personArray[i].Addr << endl; 
		}
	}
	//Pause and clean the window
	system("pause");
	system("cls");
	
}
//This function can examine whether the person is exist
int isExist(Addressbooks *abs, string name)
{
	for (int i=0; i < abs->Size; i++)
	{
		if(abs -> personArray[i].Name == name)
		{
			return i;
		}
	}
	return -1;
}
//This function can delete the person data
void deletePerson(Addressbooks *abs)
{
	cout << "Please enter a name that you want to delete" << endl;

	string name;
	cin >> name;
	
	//ret == -1 , does not exist
	//ret != -1 , exist , delete
	int ret = isExist(abs, name);

	if (ret != -1)
	{
		//Found the one you want to delete
		//run the delete fuction
		for(int i = ret; i < abs->Size; i++)
		{
			//data cover
			abs -> personArray[i] = abs -> personArray[ i+1 ];
		}
		//refresh the person data 
		abs ->Size--;
		cout << "Detele Success" << endl;
	}
	system("pause");
	system("cls");
}
//Search the person data you want
void findPerson(Addressbooks *abs)
{
	cout << "Please enter a person you want to find" <<endl;
	string name;
	cin >> name;
	int ret = isExist(abs , name);
	
	if (ret != -1)
	{
		cout << "Name:" << abs -> personArray[ret].Name << "\t"; 
		cout << "Sex:" << (abs -> personArray[ret].Sex == 1? " ♂ " : " ♀ ") << "\t"; 
		cout << "Age:" << abs -> personArray[ret].Age << "\t"; 
		cout << "PhoneNumber:" << abs -> personArray[ret].Phone << "\t"; 
		cout << "Address:" << abs -> personArray[ret].Addr << endl;  
	}
	else
	{
		cout << "Error, doesn't find the person" <<endl;
	}
	system ("pause");
	system ("cls");
}
//Edit the person data
void modifyPerson(Addressbooks * abs)
{
	cout << "Please enter a person you want to modify:" <<endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name) ;

	if (ret != -1)
	{
		string name;
		cout << "Please enter a Person Name" <<endl;
		cin >> name;
		abs ->personArray[ret].Name = name;
		
		cout << "Please enter a Person Sex" <<endl;
		cout << "1 --- ♂" << endl;
		cout << "2 --- ♀" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2 )
			{
				abs ->personArray[ret].Sex = sex;
				break;
			}
			else
			{
			cout << "Error , please enter a right number" <<endl;
			}
		}
		
		cout << "Please enter the number" << endl;
		string phone;
		cin >> phone;
		abs -> personArray[ret].Phone = phone ;

		cout << "Please enter the address" << endl;
		string address;
		cin >> address;
		abs -> personArray[ret].Addr = address;

		cout << "Edit Success"<< endl;

	}
	else
	{
		cout << "Error , doesn't find the person" << endl;
	}
	system("pause");
	system("cls");
}
// Clear up the Addressbooks
void cleanPerson(Addressbooks *abs)
{
	abs ->Size = 0;
	cout << "The Addressbooks are Clear up" <<endl;
	system("pause");
	system("cls");
}
int main()
{
	Addressbooks abs;
	abs.Size = 0;
	int select;
	//enter a number from 0-6 and do the function
	while ( true )
	{
		showMenu();
		cin >> select;
		switch (select)
		{
		case 1:
			addPerson(&abs);
				break;
		case 2:
			showPerson(&abs);
				break;
		case 3:
			deletePerson(&abs);
				break;
		case 4:
			findPerson(&abs);
				break;
		case 5:
			modifyPerson(&abs);
				break;
		case 6:
			cleanPerson(&abs);
				break;
		case 0:
			cout << "Welcome Next Time!" <<endl;
			system("pause");
			return 0;
				break;
		default :
			cout << "* Please enter a number from 0-6 *" <<endl;
			break;
		}
	}
		system("pause");
		return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值