关于c++:通讯录管理系统的设计实现

51次阅读

共计 7332 个字符,预计需要花费 19 分钟才能阅读完成。

一想到“管理系统”,咱们脑海里通常浮现四个字,增查删改。这也是必不可少的局部。
我先把这个零碎我要实现的局部列举进去(当然,我的思路有很多有余,恳请大佬斧正,批评,不胜感激)

    cout << "***********1. 增加联系人 **********" << endl;
    cout << "***********2. 显示联系人 **********" << endl;
    cout << "***********3. 删除联系人 **********" << endl;
    cout << "***********4. 查找联系人 **********" << endl;
    cout << "***********5. 批改联系人 **********" << endl;
    cout << "***********6. 清空联系人 *********" << endl;
    cout << "***********0. 退出零碎 ***********" << endl;

(备注:退出也是其中一个性能哦)

首先,构建构造体变量

#define MAX 1000

struct person
{
    string name;
    int sex{ };
    int age{ };
    string phonenumber;
    string address;
};
struct addressbook
{struct person personArr[MAX];
    int person_size{};

增加联系人

void addPerson(addressbook* aaa) // 增加联系人模块
{if (aaa->person_size < MAX)
    {
            string name;
            cout << "请输出姓名:" << endl;
            cin >> name;
            aaa->personArr[aaa->person_size].name = name;

            int sex;
            cout << "请输出性别对应序号:(1-- 男    2-- 女 )" << endl;
            while (true)
            {
                cin >> sex;
                if ((sex == 1) || (sex == 2))
                {aaa->personArr[aaa->person_size].sex = sex;
                    break;
                }
                else
                {cout << "您输出的有误,请查看后从新输出!" << endl;}
            }

            int age = 0;
            cout << "请输出年龄:" << endl;
            cin >> age;
            aaa->personArr[aaa->person_size].age = age;

            string phonenumber;
            cout << "请输出电话:" << endl;
            cin >> phonenumber;
            aaa->personArr[aaa->person_size].phonenumber = phonenumber;

            string address;
            cout << "请输出地址:" << endl;
            cin >> address;
            aaa->personArr[aaa->person_size].address = address;

            aaa->person_size++;
            cout << "增加联系人胜利!" << endl;
    }
    else
    {cout << "联系人已满,请删除局部联系人再增加!" << endl;}
    system("pause");
    system("cls");
}

显示联系人

void showperson(addressbook person)
{if (person.person_size == 0)
    {cout << "联系人列表为空" << endl;}
    for (int i = 0; i < person.person_size; i++)
    {cout << i + 1 << "." << "姓名:" << person, personArr[i].name << " "
            << "性别:" <<(person.personArr[i].sex == 1 ? "男":"女") << " "
            << "年龄:" << person.personArr[i].age << " "
            << "电话:" << person.personArr[i].phonenumber << " "
            << "住址:" << person.pearsonArr[i].address << " " << endl;
    }
    system("pause");
    system("cls");
}

删除联系人

void deleteperson(addressbook * person)

{
    string name;
    cout << "请输入您要删除的联系人姓名" << endl;
    cin >> name;
    int exist = isExist(person, name);
    if (exist != -1)
    {for (int i = exist; i < person->person_size; i++)
        {
            {person->personArr[i] = person->personArr[i + 1];

            }
            (person->person_size)--;
            cout << "删除胜利!" << endl;

        }
    else
    {cout << "没有这个人!" << endl;}
    system("pause");
    system("cls");

    }

查找联系人

void showperson(addressbook person)
{if (person.person_size == 0)
    {cout << "联系人列表为空" << endl;}
    for (int i = 0; i < person.person_size; i++)
    {cout << i + 1 << "." << "姓名:" << person, personArr[i].name << " "
            << "性别:" <<(person.personArr[i].sex == 1 ? "男":"女") << " "
            << "年龄:" << person.personArr[i].age << " "
            << "电话:" << person.personArr[i].phonenumber << " "
            << "住址:" << person.pearsonArr[i].address << " " << endl;
    }
    system("pause");
    system("cls");

批改联系人

void modifyperson(addressbook * person)
        {
            string name;
            cout << "请输出要批改的联系人姓名:" << endl;
            cin >> name;
            int exist = isExist(person, name);
            if (exist != -1)
            {
                string modifyName;
                cout << "请输出批改好的名字:";
                cin >> modifyName;
                if (modifysex == 1 || modifysex == 2)
                {person->personArr[exist].name = modifyName;

                    while (true)
                    {
                        int modifysex;
                        cout << "请输出批改后的性别(1. 男 2. 女):";
                        cin >> modifysex;
                        if (modifysex == 1 || modifysex == 2)
                        {person->personArr[exist].sex = modifysex;
                            break;

                        }
                        else
                        {cout << "您该当输出 1 或者 2,请从新输出" << endl;}
                        int modifyAge;
                        cout << "请输出批改后的年龄:";
                        cin >> modifyAge;
                        person->personArr[exist].age = modifyAge;

                        string modifyPhone;
                        cout << "请输出批改后的电话:";
                        cin >> modifyPhone;
                        person->personArr[exist].phonenumber = modifyPhone;

                        string modifyAddress;
                        cout << "请输出批改后的住址:";
                        cin >> modifyAddress;
                        person->personArr[exist].address = modifyAddress;

                        cout << "批改胜利" << endl;
                    }
                else
                {cout << "没有查到这个名字的人,故无奈批改" << endl;}
                system("pause");
                system("cls");
                }

清空联系人

void emptyperson(addressbook * person)
                {
                    string ensure;
                    cout << "您确定清空所有联系人吗,此操作不可逆,请审慎,如需清空,请输出:我批准" << endl;
                    cin >> ensure;
                    if (ensure == "我批准")
                    {
                        person->person_size = 0;
                        cout << "清空联系人胜利" << endl;
                    }
                    else
                    {cout << "撤销了清空联系人的操作" << endl;}
                    system("pause");
                    system("cls");
                }

退出零碎

int isExist(addressbook* person, string name)
{for (int i = 0; i < person->person_size; i++)
    {if (person->personArr[i].name == name)
        {return i;}
        return -1;
    }

残缺代码为:

#include <iostream>
#include<string.h>
#include<windows.h>
#include <stdlib.h>

using namespace std;
#define MAX 1000

struct person
{
    string name;
    int sex{ };
    int age{ };
    string phonenumber;
    string address;
};
struct addressbook
{struct person personArr[MAX];
    int person_size{};};
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;

}
void addperson(addressbook* aaa)
{if (aaa->person_size < MAX)
    {
        string name;
        cout << "请输出姓名:" << endl;
        cin >> name;
        aaa->personArr[aaa->person_size].name = name;

        int sex;
        cout << "请输出对应性别 1-- 男  2-- 女" << endl;
        while (true)
        {
            cin >> sex;
            if ((sex == 1)) || (sex == 2))
            {aaa->personArr[aaa->person_size].sex = sex;
            break;
            }
            else
            {cout << "您输出的有误,请从新查看后输出" << endl;}



        }

    }
}

int age = 0;
cout << "请输出年龄" << endl;
cin >> age;
aaa->personArr[aaa->person_size].age = age;

string phonenumeber;
cout << "请输出电话:" << endl;
cin >> phonenumber;
aaa->personArr[aaa->person_size].phonenumeber = phonenumeber;

string address;
cout << "请输出地址:" << endl;
cin >> address;
aaa->personArr[aaa->person_size].phonenumber = phonenumeber;

string address;
cout << "请输出地址:" << endl;
cin >> address;
aaa->personArr[aaa->person_size].address = address;

aaa->person_size++;
cout << "增加联系人胜利!" << endl;
    }
    else
    {cout << "联系人已满,请删除局部联系人再增加!" << endl;}
    system("pause");
    system("cls");
}
void showperson(addressbook person)
{if (person.person_size == 0)
    {cout << "联系人列表为空" << endl;}
    for (int i = 0; i < person.person_size; i++)
    {cout << i + 1 << "." << "姓名:" << person, personArr[i].name << " "
            << "性别:" <<(person.personArr[i].sex == 1 ? "男":"女") << " "
            << "年龄:" << person.personArr[i].age << " "
            << "电话:" << person.personArr[i].phonenumber << " "
            << "住址:" << person.pearsonArr[i].address << " " << endl;
    }
    system("pause");
    system("cls");
}
int isExist(addressbook* person, string name)
{for (int i = 0; i < person->person_size; i++)
    {if (person->personArr[i].name == name)
        {return i;}
        return -1;
    }
    void deleteperson(addressbook * person)
    {
        string name;
        cout << "请输入您要删除的联系人姓名" << endl;
        cin >> name;
        int exist = isExist(person, name);
        if (exist != -1)
        {for (int i = exist; i < person->person_size; i++)
            {
                {person->personArr[i] = person->personArr[i + 1];

                }
                (person->person_size)--;
                cout << "删除胜利!" << endl;

            }
        else
        {cout << "没有这个人!" << endl;}
        system("pause");
        system("cls");

        }
        void modifyperson(addressbook * person)
        {
            string name;
            cout << "请输出要批改的联系人姓名:" << endl;
            cin >> name;
            int exist = isExist(person, name);
            if (exist != -1)
            {
                string modifyName;
                cout << "请输出批改好的名字:";
                cin >> modifyName;
                if (modifysex == 1 || modifysex == 2)
                {person->personArr[exist].name = modifyName;

                    while (true)
                    {
                        int modifysex;
                        cout << "请输出批改后的性别(1. 男 2. 女):";
                        cin >> modifysex;
                        if (modifysex == 1 || modifysex == 2)
                        {person->personArr[exist].sex = modifysex;
                            break;

                        }
                        else
                        {cout << "您该当输出 1 或者 2,请从新输出" << endl;}
                        int modifyAge;
                        cout << "请输出批改后的年龄:";
                        cin >> modifyAge;
                        person->personArr[exist].age = modifyAge;

                        string modifyPhone;
                        cout << "请输出批改后的电话:";
                        cin >> modifyPhone;
                        person->personArr[exist].phonenumber = modifyPhone;

                        string modifyAddress;
                        cout << "请输出批改后的住址:";
                        cin >> modifyAddress;
                        person->personArr[exist].address = modifyAddress;

                        cout << "批改胜利" << endl;
                    }
                else
                {cout << "没有查到这个名字的人,故无奈批改" << endl;}
                system("pause");
                system("cls");
                }
                void emptyperson(addressbook * person)
                {
                    string ensure;
                    cout << "您确定清空所有联系人吗,此操作不可逆,请审慎,如需清空,请输出:我批准" << endl;
                    cin >> ensure;
                    if (ensure == "我批准")
                    {
                        person->person_size = 0;
                        cout << "清空联系人胜利" << endl;
                    }
                    else
                    {cout << "撤销了清空联系人的操作" << endl;}
                    system("pause");
                    system("cls");
                }
                int main()
                {
                    int userselect = 0;
                    struct addressbook aaa;
                    aaa.peson_size = 0;
                    cout << "请在下方输入您想抉择的性能(输出后面的数字即可):" << endl;
                    cin >> userselect;
                    switch (userselect)
                    {
                    case 1:
                        addPerson(&aaa);
                        break;
                    case 2:
                        showPerson(aaa);
                        break;
                    case 3:
                        deletePerson(&aaa);
                        break;
                    case 4:
                        findPerson(&aaa);
                        break;
                    case 5:
                        modifyPerson(&aaa);
                        break;
                    case 6:
                        emptyPerson(&aaa);
                        break;
                    case 0:
                        cout << "退出零碎胜利,欢迎您下次应用!" << endl;
                        system("pause");
                        return 0;
                    default:
                        cout << "输出有误,请从新输出!" << endl;
                        break;
                    }
                }
            }

正文完
 0