头图

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

    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;
                    }
                }
            }

瞿小凯
1.3k 声望593 粉丝