#include <iostream>
#include <string>
using namespace std;
#define MAX 1000
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= 0 ;
} ;
void addperson ( addressbooks * abs)
{
if ( abs- > m_size == MAX)
{
cout << "通讯录已满" << endl;
return ;
}
else
{
string name;
cout << "请输入姓名" << endl;
cin >> name;
abs- > personarray[ abs- > m_size] . m_name= name;
cout << "请输入姓别,1--男,2--女" << endl;
int sex= 0 ;
while ( true )
{
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 addr;
cin >> addr;
abs- > personarray[ abs- > m_size] . m_addr = addr;
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 << endl;
cout << "性别:" << ( abs- > personarray[ i] . m_sex== 1 ? "男" : "女" ) << endl;
cout << "年龄:" << abs- > personarray[ i] . m_age << endl;
cout << "电话:" << abs- > personarray[ i] . m_phone << endl;
cout << "住址:" << abs- > personarray[ i] . m_addr << endl;
cout << "****************" << endl;
}
}
system ( "pause" ) ;
system ( "cls" ) ;
}
int judgeperson ( addressbooks * abs)
{
cout << "请输入查询人姓名" << endl;
string name;
cin >> name;
for ( int i = 0 ; i < abs- > m_size; i++ )
{
if ( name == abs- > personarray[ i] . m_name)
{
return i;
}
}
return - 1 ;
}
void deletperson ( addressbooks * abs)
{
int ret = judgeperson ( abs) ;
if ( ret == - 1 )
{
cout << "删除失败" << endl;
}
else
{
for ( int i = ret; i < abs- > m_size; i++ )
{
abs- > personarray[ i] = abs- > personarray[ i + 1 ] ;
}
abs- > m_size-- ;
cout << "删除成功" << endl;
}
system ( "pause" ) ;
system ( "cls" ) ;
}
void findperson ( addressbooks * abs)
{
cout << "请输入您要查找的人" << endl;
int i= judgeperson ( abs) ;
if ( i== - 1 )
{
cout << "查找失败" << endl;
}
else
{
cout << "查找成功,信息如下" << endl;
cout << "姓名:" << abs- > personarray[ i] . m_name << endl;
cout << "性别:" << ( abs- > personarray[ i] . m_sex == 1 ? "男" : "女" ) << endl;
cout << "年龄:" << abs- > personarray[ i] . m_age << endl;
cout << "电话:" << abs- > personarray[ i] . m_phone << endl;
cout << "住址:" << abs- > personarray[ i] . m_addr << endl;
cout << "****************" << endl;
}
system ( "pause" ) ;
system ( "cls" ) ;
}
void modifyperson ( addressbooks * abs)
{
cout << "请输入想要修改联系人姓名" << endl;
int i = judgeperson ( abs) ;
if ( i == - 1 )
{
cout << "查找失败" << endl;
}
else
{
string name;
cout << "请输入修改后姓名" << endl;
cin >> name;
abs- > personarray[ i] . m_name = name;
cout << "请输入修改后姓别,1--男,2--女" << endl;
int sex = 0 ;
while ( true )
{
cin >> sex;
if ( sex == 1 || sex == 2 )
{
abs- > personarray[ i] . m_sex = sex;
break ;
}
cout << "输入错误,请重新输入" << endl;
}
cout << "请输入修改后年龄" << endl;
int age = 0 ;
cin >> age;
abs- > personarray[ i] . m_age = age;
cout << "请输入修改后电话号码" << endl;
string phone;
cin >> phone;
abs- > personarray[ i] . m_phone = phone;
cout << "请输入修改后地址" << endl;
string addr;
cin >> addr;
abs- > personarray[ i] . m_addr = addr;
}
system ( "pause" ) ;
system ( "cls" ) ;
}
void cleanperson ( addressbooks * abs)
{
int i;
cout << "是否确定清空所有数据?" << endl;
cout << "1--是\n2--否\n" ;
cin >> i;
if ( i == 1 )
{
abs- > m_size = 0 ;
cout << "通讯录已清空" << endl;
}
system ( "pause" ) ;
system ( "cls" ) ;
}
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;
}
int main ( )
{
addressbooks abs;
int select = 0 ;
while ( true )
{
showmenu ( ) ;
cin >> select;
switch ( select)
{
case 1 :
addperson ( & abs) ;
break ;
case 2 :
showperson ( & abs) ;
break ;
case 3 :
deletperson ( & abs) ;
break ;
case 4 :
findperson ( & abs) ;
break ;
case 5 :
modifyperson ( & abs) ;
break ;
case 6 :
cleanperson ( & abs) ;
break ;
case 0 :
cout << "欢迎下次使用" << endl;
system ( "pause" ) ;
break ;
}
}
system ( "pause" ) ;
return 0 ;
}