【C++】通讯录管理系统

  • 初学C++所写的简单的通讯录管理系统
  • 存储结构使用的是单链表形式
  • 测试无bug
#include
#include
using namespace std;

class Person{
     
    public:
        string name;
        string sex;
        int age;
        string phone;
        string addr;
        Person *next;
};

class Addr_List{
     
    private:
        Person *head,*rear;
        int list_count;
    public:
        Addr_List()  //构造函数,初始化通讯录
        {
     
            head = rear = new Person;
            head->next = NULL;
            list_count = 0;
        }
    
        void Increase_List()
        {
     
            Person *obj;
            obj = new Person;
            if(obj)
            {
     
                cout<<"请输入联系人信息:"<<endl;
                cout<<"姓名:";
                cin >> obj->name;
                cout<<"性别:";
                cin >> obj->sex;
                cout<<"年龄:";
                cin >> obj->age;
                cout<<"手机号码:";
                cin >> obj->phone;
                cout<<"家庭地址:";
                cin >> obj->addr;

                obj->next = NULL;
                rear->next = obj;
                rear = obj;
            }
            else
            {
     
                cout<<"空间有限"<<endl;
            }
        }

        void Dsiplay_List()
        {
     
            int count = 1;
            Person *obj = head->next;
            if(obj)
            {
     
                cout<<"显示联系人:"<<endl;
                while(obj)
                {
     
                    cout<<count<<"、"<<obj->name<<" "<<obj->sex<<" "<<obj->age<<" "<<obj->phone<<" "<<obj->addr<<endl;
                    count++;
                    obj = obj->next;
                }
            }
            else
            {
     
                cout<<"联系人为空"<<endl;
            }
        }

        void Delete_List()
        {
     
            if(head == rear)
            {
     
                cout<<"联系人列表为空,无法删除!"<<endl;
            }
            else
            {
     
                Person *obj = head, *temp;
                string _name;
                cout<<"请输入要删除联系人的姓名:";
                cin>>_name;
                while(obj->next)
                {
     
                    if(obj->next->name == _name)
                    {
     
                        cout<<"正在删除..."<<endl;
                        temp = obj->next;
                        obj->next = temp->next;
                        if(temp == rear)  //如果删除的节点是最后一个节点
                        {
     
                            rear = obj;
                        }
                        free(temp);
                        cout<<"删除完毕"<<endl;
                        return;
                    }
                    else
                    {
     
                        obj = obj->next;
                    }  
                }
                cout<<"查无此人,无法删除"<<endl;
            }
        }

        void Find_List()
        {
     
            Person *obj = head->next;
            string _name;
            cout<<"请输入要查找联系人的姓名:";
            cin>>_name;
            while(obj)
            {
     
                if(obj->name == _name)
                {
     
                    cout<<obj->name<<" "<<obj->sex<<" "<<obj->age<<" "<<obj->phone<<" "<<obj->addr<<endl;
                    return;
                }
                else
                {
     
                    obj = obj->next;
                } 
            }
            cout<<"查无此人!"<<endl;
        }

        void Revise_List()
        {
     
            Person *obj = head->next;
            string _name;
            cout<<"请输入要修改联系人的姓名:";
            cin>>_name;
            while(obj)
            {
     
                if(obj->name == _name)
                {
     
                    cout<<"性别:";
                    cin>>obj->sex;
                    cout<<"年龄:";
                    cin>>obj->age;
                    cout<<"电话:";
                    cin>>obj->phone;
                    cout<<"地址:";
                    cin>>obj->addr;
                    cout<<"修改完毕"<<endl;
                    return;
                }
                else
                {
     
                    obj = obj->next;
                }
            }
            cout<<"查无此人,无法修改"<<endl;
        }

        void Clean_List()
        {
     
            Person *obj;
            while(head != rear)
            {
     
                obj = head->next;
                head->next = obj->next;
                if(obj == rear)
                {
     
                    rear = head;
                }
                free(obj);
            }
            cout<<"联系人已清空"<<endl;;
        }
};

void menu_show()
{
     
    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()
{
     
    bool system_state = true;
    int user_select = 0;
    Addr_List CUI;
    while(system_state)
    {
     
        menu_show();
        cin >> user_select;
        switch(user_select)
        {
     
            case 1:  //1、添加联系人
                    CUI.Increase_List();
                break;
            case 2:  //2、显示联系人
                    CUI.Dsiplay_List();
                break;
            case 3:  //3、删除联系人
                    CUI.Delete_List();
                break;
            case 4:  //4、查找联系人
                    CUI.Find_List();
                break;
            case 5:  //5、修改联系人
                    CUI.Revise_List();
                break;
            case 6:  //6、清空联系人
                    CUI.Clean_List();
                break;
            case 0:  //0、退出通讯录
                    cout<<"*****欢迎下次光临*****"<<endl;
                    system_state = false;
                break;
            default:  //非法输入
                    cout<<"*****非法输入,退出系统*****"<<endl;
                    system_state = false;
                break;
        }
    }
    return 0;
}

你可能感兴趣的:(C++,通讯录管理系统,C++,链表)