C++入门篇 C++实现通信录管理系统

1. 任务描述

  编写一个通信录管理系统,使该系统有增、删、改、查的功能。

2.C++代码实现

#include<iostream>
#include<string>
#define MAX 10

using namespace std;

struct Person
{
    string m_name;
    int m_age;
    string m_phone;
    string m_address;
};


struct PersonInfo
{
    Person personArray[MAX];
    int size = 0;
};

void show_info()
{
    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;
}


void add_person(PersonInfo * abs)
{
    int index = abs->size;
    cout << "请输入姓名" << endl;
    string name;
    cin >> name;
    abs->personArray[index].m_name = name;

    cout << "请输入年龄" << endl;
    int age;
    cin >> age;
    abs->personArray[index].m_age = age;

    cout << "请输入电话" << endl;
    string phone;
    cin >> phone;
    abs->personArray[index].m_phone = phone;

    cout << "请输入地址" << endl;
    string address;
    cin >> address;
    abs->personArray[index].m_address = address;

    abs->size++;

    cout << "成功添加联系人" << endl;

}


void show_person(PersonInfo * abs)
{
    int size = abs->size;
    for(int i=0; i < size; i++)
    {
        cout << abs->personArray[i].m_name + "\t"
             << abs->personArray[i].m_age << "\t"
             << abs->personArray[i].m_phone + "\t"
             << abs->personArray[i].m_address + "\t"
             << endl;
    }
}


int is_exists(PersonInfo * abs, string name)
{
    int size = abs->size;
    for(int i=0; i<size; i++)
    {
        if(abs->personArray[i].m_name == name)
        {
            return i;
        }
    }
    return -1;
}


void del_person(PersonInfo * abs, int index)
{
    int size = abs->size;
    for(int i=index; i<size-1; i++)
    {
        abs->personArray[i] = abs->personArray[i+1];
    }
    abs->size--;
}


void show_person(PersonInfo * abs, int index)
{
    cout << abs->personArray[index].m_name + "\t"
         << abs->personArray[index].m_age << "\t"
         << abs->personArray[index].m_phone + "\t"
         << abs->personArray[index].m_address + "\t"
         << endl;
}


void modify_person(PersonInfo * abs, int index)
{
    cout << "请输入年龄" << endl;
    int age;
    cin >> age;
    abs->personArray[index].m_age = age;

    cout << "请输入电话" << endl;
    string phone;
    cin >> phone;
    abs->personArray[index].m_phone = phone;

    cout << "请输入地址" << endl;
    string address;
    cin >> address;
    abs->personArray[index].m_address = address;
}


void clear_person(PersonInfo * abs)
{
    abs->size = 0;
}

int main()
{
    PersonInfo abs;

    while(true)
    {
        show_info();
        cout << "请输入选项!";
        int num;
        cin >> num;
        if(num==1)
        {
            cout << "添加联系人" << endl;
            add_person(&abs);
        }
        else if(num==2)
        {
            cout << "显示联系人" << endl;
            show_person(&abs);
        }
        else if(num==3)
        {
            cout << "删除联系人" << endl;
            string name;
            cout << "请输入删除人的名字" << endl;
            cin >> name;
            int det = is_exists(&abs, name);
            if(det != -1)
            {
                del_person(&abs, det);
            }
            else
            {
                cout << "通讯录中没有找到此人" << endl;
            }
        }
        else if(num==4)
        {
            cout << "查找联系人" << endl;
            string name;
            cout << "请输入查找人的名字" << endl;
            cin >> name;
            int det = is_exists(&abs, name);
            if(det != -1)
            {
                show_person(&abs, det);
            }
            else
            {
                cout << "通讯录中没有找到此人" << endl;
            }

        }
        else if(num==5)
        {
            cout << "修改联系人" << endl;
            string name;
            cout << "请输入要修改人的名字" << endl;
            cin >> name;
            int det = is_exists(&abs, name);
            if(det == -1)
            {
                cout << "通讯录中没有找到此人" << endl;
            }
            else
            {
                modify_person(&abs, det);
            }
        }
        else if(num==6)
        {
            cout << "清空联系人" << endl;
            clear_person(&abs);
        }
        else if(num==0)
        {
            cout << "退出通讯录" << endl;
            break;
        }
        else
        {
            cout << "输入错误,请重新输入" << endl;
        }

    }
    return 0;
}

 

记录:供C++小白入门学习。

上一篇:《黑马程序员》通讯录管理系统实战


下一篇:设计模式——原型模式