#include<iostream>
#include<stdlib.h>
#include<sys/fcntl.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;
string m_Phone;
string m_Address;
};
struct AddressBooks{
struct Person personArray[MAX];
int m_Size;
};
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;
int age;
cout<< "请输入年龄:" <<endl;
cin>>age;
abs->personArray[abs->m_Size].m_Age = age;
int sex;
cout<< "请输入性别(1男 2女):" <<endl;
while(true){
cin>>sex;
if(sex == 1 || sex == 2){
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}else{
cout<< "输入错误,请重新输入:" <<endl;
}
}
string phone;
cout<< "请输入联系电话:" <<endl;
cin>>phone;
abs->personArray[abs->m_Size].m_Phone = phone;
string address;
cout<< "请输入地址:" <<endl;
cin>>address;
abs->personArray[abs->m_Size].m_Address = address;
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_Address <<endl;
}
}
}
int isExist(AddressBooks* abs, string name){
for(int i = 0; i < abs->m_Size; i++){
if(abs->personArray[i].m_Name == name){
return i;
}else{
return -1;
}
}
}
void deletePerson(AddressBooks* abs){
if(abs->m_Size == 0){
cout << "通讯录为空,无法进行删除操作" << endl;
return;
}else{
string name;
cout<< "请输入要删除的联系人的姓名" <<endl;
cin>>name;
int ret = isExist(abs,name);
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){
string name;
cout<< "请输入要查找联系人的姓名:" <<endl;
cin>>name;
int ret = isExist(abs,name);
if(ret == -1){
cout<< "通讯录中不存在此人!" <<endl;
}else{
cout<< " 姓名: "<< abs->personArray[ret].m_Name <<"\t";
cout<< " 性别: "<< (abs->personArray[ret].m_Sex == 1 ? "男" : "女")<<"\t";
cout<< " 年龄: "<< abs->personArray[ret].m_Age <<"\t";
cout<< " 电话: "<< abs->personArray[ret].m_Phone <<"\t";
cout<< " 地址: "<< abs->personArray[ret].m_Address <<endl;
cout<<" 查找成功! "<<endl;
}
}
void changePerson(AddressBooks* abs){
string name;
cout<< "请输入你想要修改的联系人:" <<endl;
cin>>name;
int ret = isExist(abs,name);
if(ret == -1){
cout<< "修改有误,通讯录中不存在此人!" <<endl;
}else{
while(true){
int number;
cout<< "修改年龄,请输入1; 修改电话,请输入2; 修改地址,请输入3;退出修改,请输入0" <<endl;
cin>>number;
if(number == 1 || number == 2 || number == 3 || number == 0){
if(number == 1){
int age;
cout<< "请输入年龄:" << endl;
cin>>age;
abs->personArray[ret].m_Age = age;
}
if(number == 2){
string phone;
cout<< "请输入新的电话:" << endl;
cin>>phone;
abs->personArray[ret].m_Phone = phone;
}
if(number == 3){
string address;
cout<< "请输入新的地址:" << endl;
cin>>address;
abs->personArray[ret].m_Address = address;
}
else{
break;
}
}else{
cout<< "输入有误,请重新进行输入" <<endl;
break;
}
}
cout<<"修改后的通讯录"<<endl;
showPerson(abs);
}
system("pause");
system("cls");
}
void emptyPerson(AddressBooks* abs){
int num = abs->m_Size;
if(num == 0){
cout<< "通讯录已经为空!" <<endl;
}else{
abs->m_Size == 0;
}
system("pause");
system("cls");
}
int main()
{
AddressBooks abs;
abs.m_Size = 0;
int select;
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:
changePerson(&abs);
break;
case 6:
emptyPerson(&abs);
break;
case 0:
cout<<"*** bye 欢迎下次使用 ***"<<endl;
return 0;
break;
default:
break;
}
}
return 0;
}
**/
|