用c与c++编程的通讯录,比较简单适合入门

#include
using namespace std;
#include
//using namespace std;
#define MAX 1000
//一个显示通讯录的函数
void showMenu()
{
cout << “1添加联系人” << endl;
cout << “2显示联系人” << endl;
cout << “3删除联系人” << endl;
cout << “4查找联系人” << endl;
cout << “5修改联系人” << endl;
cout << “6清空联系人” << endl;
cout << “0退出通讯录” << endl;
}
//这是学生的结构体
struct Person{
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
//这是通讯录的结构体,有数组来储存
struct Addressbooks{
struct Person personArray[MAX];
int m_Size;
};
//这是添加联系人的函数,首先要注意的是通讯录是否满了,这是用size来遍历
void addPerson(Addressbooks * abs)
{
if (abs->m_Size == MAX){
cout << “通讯录已经满了” << endl;
}
else{
string name;
cout << “请输入姓名” << endl;
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
while (1)
{
cout << “请输入性别” << endl;
cout << “1–男,2–女” << endl;
int sex = 0;
cin >> sex;
if (sex == 1 || sex == 2){
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
cout << “输入有误” << endl;
}
cout << “请输入年龄” << endl;
int age = 0;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;

	cout << "请输入联系电话" << endl;
	string phone;
	cin >> phone;
	abs->personArray[abs->m_Size].m_Phone = phone;

	cout << "请输入家庭住址" << endl;
	string adress;
	cin >> adress;
	abs->personArray[abs->m_Size].m_Addr = adress;

	abs->m_Size++;
	cout << "添加成功" << endl;

	system("pause");
	system("cls");
}

}
//这是显示通信录的,把你自己输入的通讯录的东西显示出来
void showPerson(Addressbooks *abs){
if (abs->m_Size == 0)
cout << “当前记录为空” << endl;
else
{
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_Addr << endl;
}
}
system(“pause”);
system(“cls”);
}
//这是为了便于删除以及查找联系人的。for循环是遍历,找到返回一,否则为-1
int isExist(Addressbooks *abs,string name){
for (int i = 0; i < abs->m_Size; i++){
if (abs->personArray[i].m_Name == name){
return 1;
}
}
return -1;
}
//这是删除联系人–要注意的是引用了isExist这个函数,注意的是不要加星号,
void deletePerson(Addressbooks *abs){
cout << “请输入您要删除的联系人” << endl;
string name;
cin >> name;

int ret=isExist(abs, name);
if (ret != -1){
	for (int i = ret; i < abs->m_Size; i++){
		abs->personArray[i] = abs->personArray[i + 1];
	}
	abs->m_Size--;
	cout << "删除成功" << endl;
}
else{
	cout << "查无此人" << endl;
}
system("pause");
system("cls");

}
//这是查找联系人
void findPerson(Addressbooks abs){
cout << “请输入查找的联系人” << endl;
string name;
cin >> name;
int ret=isExist(abs, name);
if (ret != -1)
{
cout << “姓名” << abs->personArray[ret].m_Name << “\t”;
cout << “性别” << abs->personArray[ret].m_Sex << “\t”;
cout << “年龄” << abs->personArray[ret].m_Age << “\t”;
cout << “住址” << abs->personArray[ret].m_Addr << “\t”;
cout << “电话” << abs->personArray[ret].m_Phone << endl;
}
else
{
cout << “查无此人” << endl;
}
system(“pause”);
system(“cls”);
}
//这是修改联系人的方法,比较简单,可能要注意的abs不需要加
,因为数组的名字就是首地址
void modifyPerson(Addressbooks *abs){
cout << “请输入修改的联系人” << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1){
string name;
cout << “请输入姓名” << endl;
cin >> name;
abs->personArray[ret].m_Name = name;
int sex = 0;
cout << “请输入性别–1为男,二为女” << endl;
while (true)
{
cin >> sex;

		if (sex == 1 || sex == 2){
			abs->personArray[ret].m_Sex = sex;
			break;
		}
		cout << "输入有误" << endl;
	}

	cout << "请输入年龄" << endl;
	int age = 0;
	while (true)
	{
		cin >> age;
		if (0 < age && age < 100)
		{
			
			abs->personArray[ret].m_Age = age;
			break;
		}
		cout << "输入有误" << endl;
	}

	cout << "请输入住址" << endl;
	string adress;
	cin >> adress;
	abs->personArray[ret].m_Addr = adress;

	cout << "请输入电话" << endl;
	string phone;
	cin >> phone;
	abs->personArray[ret].m_Phone = phone;

	cout << "修改成功" << endl;
}
else
{
	cout << "查无此人" << endl;
}
system("pause");
system("cls");

}
//清空联系人的操作,这一步就是要把size这个标志位清零,就相当于逻辑清零
void clearPerson(Addressbooks *abs){
cout << “是否真的需要清空操作,是的话请按1” << endl;
int a = 0;
cin >> a;
while (true)
{
if (a == 1){
abs->m_Size = 0;//直接将当前的联系人数量变成零,这样就是相当于逻辑清空操作,把数据全部清空了。
cout << “清空了” << endl;
system(“pause”);
system(“cls”);
}
}
}
//这是程序的入口,可能注意的是system的这些用法,也比较简单
int main()
{
Addressbooks abs;
abs.m_Size = 0;
int select = 0;
while (true){
showMenu();
cin >> select;
switch (select)
{
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
//{
//cout << “请输入删除联系人姓名” << endl;
// string name;
//cin >> name;
//if (isExist(&abs, name) == -1){
//cout << “查无此人” << endl;
//}
//else
//{
//cout << “找到此人” << endl;
//}
//}
deletePerson(&abs);
break;
case 4:
findPerson(&abs);
break;
case 5:
modifyPerson(&abs);
break;
case 6:
clearPerson(&abs);
break;
case 0:
cout << “欢迎下次使用” << endl;
system(“pause”);
return 0;
break;
default:
break;
}
}
system(“pause”);
return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值