收获:
培养简单的逻辑分析 工作分块的能力 学会了结构体指针的使用方法
#include<iostream>
#include <string> //string头文件
using namespace std;
#define MAX 1000 //通讯录最大人数
void showMenu();
void AddPerson(address_book* AddressBook);
void showPerson(address_book* AddressBook);
int isExist(address_book* AddressBook, string name);
void Dele(address_book* AddressBook);
void Find(address_book* AddressBook);
void Revise(address_book* AddressBook);
void clean(address_book* AddressBook);
struct Person
{
string name;
int sex;
int age;
string phoneid;
string address;
};
通讯录结构体
struct address_book
{
Person perArr[MAX]; //结构体数组 通讯录中保存的联系人数组
int size;//通讯录中人员个数
};
int main() {
//创建通讯录
address_book AddressBook;
//初始化通讯录人数
AddressBook.size = 0;
int select = 0;
//字符串型输入
/*string str="13171110708";
cout << str << endl;
int n = sizeof(str);
cout << n << endl;*/
while (true)
{
showMenu();
cout << "请选择对应功能:" << endl;
cin >> select;
switch (select)
{
case 1://添加联系人
AddPerson(&AddressBook);//结构体做参数 地址传递
//addPerson(&abs);
break;
case 2://显示联系人
showPerson(&AddressBook);
//Show(&abs);
break;
case 3: //删除联系人
Dele(&AddressBook);
break;
case 4: //查找联系人
Find(&AddressBook);
break;
case 5: //修改联系人
Revise(&AddressBook);
break;
case 6: //清空联系人
clean(&AddressBook);
break;
case 0: //退出通讯录
cout << "欢迎下次使用 退出address_book!" << endl;
//T = false;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
//菜单界面
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;
}
//添加联系人函数
void AddPerson(address_book * AddressBook)
{
//***************************************
//***************************************
// address_book名称 AddressBook结构体变量(这里地址传递 用->获取属性)
// Person perArr[MAX]; //结构体数组 .获取Person结构体内属性
//***************************************
//***************************************
if (AddressBook->size == MAX)
{
cout << "AddressBook 已满,不能添加" << endl;
return;
}
else
{
string name, phoneid, adress;
int sex=0, age;
cout << "请输入姓名:" << endl;
cin >> name;
AddressBook->perArr[AddressBook->size].name = name;
while (true)
{
cout << "请输入性别:(1->man 2->woman)" << endl;
cin >> sex;
if (sex == 1 || sex == 2)
{
AddressBook->perArr[AddressBook->size].sex = sex;
break;
}
cout << "输入有误,重新输入" << endl;
}
cout << "请输入年龄:" << endl;
cin >> age;
AddressBook->perArr[AddressBook->size].age = age;
/*cout << "请输入电话号:" << endl;
cin >> phoneid;
AddressBook->perArr[AddressBook->size].phoneid = phoneid;*/
while (true)
{
cout << "请输入电话号:" << endl;
cin >> phoneid;
if (sizeof(phoneid) == 40)
{
AddressBook->perArr[AddressBook->size].phoneid = phoneid;
break;
}
cout << "输入电话格式有误,重新输入" << endl;
}
cout << "请输入地址:" << endl;
cin >> adress;
AddressBook->perArr[AddressBook->size].address = adress;
//更新通讯录人数
AddressBook->size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
//2、显示所有联系人信息
void showPerson(address_book * AddressBook)
{
if (AddressBook->size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < AddressBook->size; i++)
{
cout << "姓名:" << AddressBook->perArr[i].name << "\t";
cout << "性别:" << (AddressBook->perArr[i].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << AddressBook->perArr[i].age << "\t";
cout << "电话:" << AddressBook->perArr[i].phoneid << "\t";
cout << "住址:" << AddressBook->perArr[i].address << endl;
}
}
system("pause");
//system("cls");
}
//判断是否存在查询的人员,存在返回在数组中的索引位置,不存在返回-1
int isExist(address_book* AddressBook,string name)
{
for (int i = 0; i < AddressBook->size; i++)
{
if (AddressBook->perArr[i].name == name)
{
return i;
}
}
return -1;
}
//3.删除联系人
void Dele(address_book* AddressBook)
{
cout << "请输入您要删除的联系人" << endl;
string name;
cin >> name;
int t = isExist(AddressBook, name);
if (t != -1)
{
for (int i = 0; i < AddressBook->size; i++)
{
AddressBook->perArr[t] = AddressBook->perArr[t + 1];
}
AddressBook->size--;
}
else
cout << "查无此人" << endl;
system("pause");
system("cls");
}
//4.查找联系人
void Find(address_book* AddressBook)
{
cout << "请输入您要查找的联系人" << endl;
string name;
cin >> name;
int t = isExist(AddressBook, name);
if (t != -1)
{
cout << "姓名:" << AddressBook->perArr[t].name << "\t";
cout << "性别:" << (AddressBook->perArr[t].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << AddressBook->perArr[t].age << "\t";
cout << "电话:" << AddressBook->perArr[t].phoneid << "\t";
cout << "住址:" << AddressBook->perArr[t].address << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//5.修改联系人
void Revise(address_book* AddressBook)
{
cout << "请输入您要查找的联系人" << endl;
string name;
cin >> name;
int t = isExist(AddressBook, name);
if (t != -1)
{
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
AddressBook->perArr[t].name = name;
cout << "请输入性别:" << endl;
cout << "1 -- 男" << endl;
cout << "2 -- 女" << endl;
//性别
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
AddressBook->perArr[t].sex = sex;
break;
}
cout << "输入有误,请重新输入";
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
AddressBook->perArr[t].age = age;
//联系电话
cout << "请输入联系电话:" << endl;
string phone = "";
cin >> phone;
AddressBook->perArr[t].phoneid = phone;
//家庭住址
cout << "请输入家庭住址:" << endl;
string address;
cin >> address;
AddressBook->perArr[t].address = address;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//6、清空所有联系人
void clean(address_book * AddressBook)
{
AddressBook->size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}