通讯录管理系统

#include<iostream>
#include<string>
using namespace std;
const int MAX=1000;

struct  Person
{
    string m_Name;//姓名 
    int m_Sex;//性别  1为男 2为女 
    int m_Age; //年龄 
    string m_Phone;//电话 
    string m_Adds;//地址 
};

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  sex=0;
        cout<<"请输入性别:"<<endl;
        cout<<"1---男"<<endl;
        cout<<"2---女"<<endl; 
        //如果输入的是1或者2可以退出循环,因为输入的是正确值;
        //如果不是继续输入,直到是1或者2为止 
        while(true)
        {
          cin>>sex;
         if(sex==1||sex==2){
              abs->personArray[abs->m_Size].m_Sex=sex;
              break;
         }
        cout<<"输入有误,重新输入"<<endl;
    }
        //年龄
            int  age=0;
        cout<<"请输入年龄:"<<endl;
        cin>>age;
         abs->personArray[abs->m_Size].m_Age=age;
        //电话
            string phone;
        cout<<"请输入电话:"<<endl;
     while(true){
             cin>>phone;
         if(phone.size()==11||phone.size()==6){
              abs->personArray[abs->m_Size].m_Phone=phone;
              break;
         }
     }
        //地址 
            string address;
        cout<<"请输入家庭住址:"<<endl;
        cin>>address;
         abs->personArray[abs->m_Size].m_Adds=address;
             //更新通讯录人数
        abs->m_Size++; 
        cout<<"添加成功"<<endl;
        system("pause");//请按任意键继续 
        system("cls");//清屏操作 
    }
}

//显示联系人 
void showperson(Addressbooks *abs) {
    if(abs->m_Size==0){
        cout<<"当前联系人为空"<<endl;
        return ; 
    }
    else{
    for(int i=0;i<abs->m_Size;i++){
            cout<<"姓名: "<<abs->personArray[i].m_Name<<"\t";// \t是水平制表符,空出8个字符位 
            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_Adds<<endl; 
     }
     }
          system("pause");//按任意键结束 
     system("cls");//清屏 
}


//检测该联系人是否存在
int  insertperson(string a,Addressbooks *abs){
 for(int i=0;i<abs->m_Size;i++){
         if(a==abs->personArray[i].m_Name){
            return i;
         }
 }
  return -1;
} 


//删除联系人
void deleteperson(Addressbooks *abs) {
    cout<<"请输入您要删除的联系人: "<<endl;
    string name;
    cin>>name;
       int t=insertperson(name,abs);
         if(t==-1) cout<<"查无此人"<<endl;
         else{
             //查到此人要进行删除;
            abs->personArray[t]=abs->personArray[t+1];
            abs->m_Size--;
            cout<<"删除成功"<<endl; 
         }
    system("pause");
    system("cls");
}

//查找指定联系人
void     insertpersons(Addressbooks *abs){
    cout<<"请输入您要查找的联系人: " <<endl;
    string name;
    cin>>name;
      int t=insertperson(name,abs);
         if(t==-1) cout<<"查无此人"<<endl;
         else{
             cout<<"姓名: "<<abs->personArray[t].m_Name<<"\t";// \t是水平制表符,空出8个字符位 
            cout<<"性别: "<<(abs->personArray[t].m_Sex==1?"":"")<< "\t"; 
            cout<<"年龄: "<<abs->personArray[t].m_Age<< "\t"; 
            cout<<"电话: "<<abs->personArray[t].m_Phone<< "\t"; 
            cout<<"住址: "<<abs->personArray[t].m_Adds<<endl; 
         }
         system("pause");
    system("cls");
}


//修改联系人 
void   modifypersons(Addressbooks *abs){
    cout<<"请输入您要修改的联系人: " <<endl;
    string name;
    cin>>name;
      int t=insertperson(name,abs);
         if(t==-1) cout<<"查无此人"<<endl;
         else{
             //姓名 
        string name;
        cout<<"请输入姓名:"<<endl;
        cin>>name;
         abs->personArray[ t].m_Name=name;
        //性别
        int  sex=0;
        cout<<"请输入性别:"<<endl;
        cout<<"1---男"<<endl;
        cout<<"2---女"<<endl; 
        //如果输入的是1或者2可以退出循环,因为输入的是正确值;
        //如果不是继续输入,直到是1或者2为止 
        while(true)
        {
          cin>>sex;
         if(sex==1||sex==2){
              abs->personArray[t].m_Sex=sex;
              break;
         }
        cout<<"输入有误,重新输入"<<endl;
    }
        //年龄
            int  age=0;
        cout<<"请输入年龄:"<<endl;
        cin>>age;
         abs->personArray[t].m_Age=age;
        //电话
            string phone;
        cout<<"请输入电话:"<<endl;
     while(true){
             cin>>phone;
         if(phone.size()==11||phone.size()==6){
              abs->personArray[t].m_Phone=phone;
              break;
         }
         
     }
        //地址 
            string address;
        cout<<"请输入家庭住址:"<<endl;
        cin>>address;
         abs->personArray[t].m_Adds=address;
  cout<<"修改成功!"<<endl; 
         }
         system("pause");
    system("cls");
}


//清空联系人 
void cleanperson(Addressbooks *abs)
{
    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()
{
    //创建通讯录结构体变量
    struct  Addressbooks abs;
    //初始化通讯录中的当前人员的个数
    abs.m_Size=0; 
    int select;
    while(true){
        showMenu();//显示菜单 
        cin>>select;
          string a;
        switch(select)
        {
            case 1://添加联系人 
            addPerson(&abs);//值传递不修饰实参,传入的是地址才能修饰形参 
                break;
            case 2://显示通讯录 
            showperson(&abs);
                break;    
           case 3://删除联系人 
           deleteperson(&abs);
            break;
         case 4://查找联系人
         insertpersons(&abs) ;
                break;
         case 5://修改联系人 
          modifypersons(&abs);
                break;
        case 6://清空联系人 
        cleanperson(&abs);
                break;        
         case 0://退出通讯录 
             cout<<"欢迎下次使用"<<endl;
                return 0;    
                    break;
         default :
                break;                        
        }
    } 
}

 

posted @ 2023-04-23 20:12  艾鑫4646  阅读(16)  评论(0编辑  收藏  举报