一、通讯录管理系统案例
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<stdio.h>
#define MAX 1000
using namespace std;
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;
}
struct person
{
string m_name;
int m_sex;
int m_age;
int m_phonenumber;
string m_address;
};
struct adressbooks
{
struct person personarray[MAX];
int m_size;
};
void addperson(adressbooks * p)
{
if (p -> m_size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
string name;
cout << "添加联系人姓名:" << endl;
cin >> name;
p->personarray[p->m_size].m_name = name;
cout << "添加联系人性别:" << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
p->personarray[p->m_size].m_sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "添加联系人年龄:" << endl;
int age;
cin >> age;
p->personarray[p->m_size].m_age = age;
cout << "添加联系人电话:" << endl;
int phonenumber;
cin >> phonenumber;
p->personarray[p->m_size].m_phonenumber = phonenumber;
cout << "添加联系人地址:" << endl;
string address;
cin >> address;
p->personarray[p->m_size].m_address = address;
p->m_size++;
cout<< "添加成功" << endl;
system("pause");
system("cls");
}
}
void showperson(adressbooks * p)
{
if (p->m_size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < p->m_size; i++)
{
cout << "姓名:" << p->personarray[i].m_name << "\t";
cout << "性别:" << (p->personarray[i].m_sex == 1?"男":"女") << "\t";
cout << "年龄:" << p->personarray[i].m_age << "\t";
cout << "电话:" << p->personarray[i].m_phonenumber << "\t";
cout << "住址:" << p->personarray[i].m_address << endl;
}
}
system("pause");
system("cls");
}
int isExist(adressbooks* p, string name)
{
for (int i = 0; i < p->m_size; i++)
{
if (p->personarray[i].m_name == name)
{
return i;
}
}
return -1;
}
void deleteperson(adressbooks* p)
{
cout << "输入需要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(p, name);
if (ret != -1)
{
for (int i = 0; i < p->m_size; i++)
{
p->personarray[i] = p->personarray[i + 1];
}
p->m_size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void findperson(adressbooks* p)
{
cout << "请输入您要查找的联系人" << endl;
string name;
cin >> name;
int ret = isExist(p, name);
if (ret != -1)
{
cout << "姓名:" << p->personarray[ret].m_name << "\t";
cout << "性别:" << p->personarray[ret].m_sex << "\t";
cout << "年龄:" << p->personarray[ret].m_age << "\t";
cout << "电话:" << p->personarray[ret].m_phonenumber << "\t";
cout << "住址:" << p->personarray[ret].m_address << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void modifyperson(adressbooks* p)
{
cout << "输入修改联系人姓名" << endl;
string name;
cin >> name;
int ret = isExist(p, name);
if (ret != -1)
{
string name;
cout << "请输入姓名:" << endl;
cin >> name;
p->personarray[ret].m_name = name;
cout << "请输入性别:" << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
p->personarray[ret].m_sex = sex;
break;
}
cout << "输入有无,请重新输入" << endl;
}
int age;
cout << "请输入年龄:" << endl;
cin >> age;
p->personarray[ret].m_age = age;
int phone;
cout << "请输入电话:" << endl;
cin >> phone;
p->personarray[ret].m_phonenumber = phone;
string address;
cout << "请输入家庭住址:" << endl;
cin >> address;
p->personarray[ret].m_address = address;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void cleanperson(adressbooks* p)
{
p->m_size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
int main()
{
adressbooks 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:
deleteperson(&abs);
break;
case 4:
findperson(&abs);
break;
case 5:
modifyperson(&abs);
break;
case 6:
cleanperson(&abs);
break;
case 0:
cout << "谢谢使用,欢迎下次光临!" << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}