C語言實現簡單通訊錄系統

本文實例為大傢分享瞭C語言通訊錄系統(增刪改查),供大傢參考,具體內容如下

全部代碼如下所示:

#include <iostream>
#include <string>
using namespace std;

const int MAX = 1000;

//聯系人結構體
typedef struct person
{
    string m_name;          //姓名
    int m_sex;              //性別 1 男,0 女
    int m_age;              //年齡
    string m_phone;         //電話
    string m_addr;          //住址
}person_t;

//通訊錄結構體
typedef struct addressbooks
{
    person_t personArray[MAX];  //通訊錄中保存的聯系人數組
    int m_size;                 //通訊錄中人員個數
}addressbooks_t;

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

void addPerson(addressbooks_t *abs)
{
    //判斷通訊錄是否滿瞭,如果滿瞭就不再添加
    if(abs->m_size == MAX)
    {
        cout  << "通訊錄已滿,無法添加!" << endl;
        return ;
    }
    else
    {
        //添加具體聯系人
        cout << "請輸入姓名: ";
        cin >> abs->personArray[abs->m_size].m_name;

        int sex;
        while(true)
        {
            cout << "請輸入性別(1男,0女): ";
            cin >> sex;
            if(sex == 1 || sex == 0)
            {
                abs->personArray[abs->m_size].m_sex = sex;
                break;
            }
            else
            {
                cout << "輸入有誤,請重新輸入!" << endl;
            }
        }

        cout << "請輸入年齡: ";
        cin >> abs->personArray[abs->m_size].m_age;

        cout << "請輸入電話: ";
        cin >> abs->personArray[abs->m_size].m_phone;

        cout << "請輸入地址: ";
        cin >> abs->personArray[abs->m_size].m_addr;

        //更新通訊錄人數
        ++ abs->m_size;

        cout << "添加成功!" << endl;
        system("pause");
    }
}

void displayPerson(addressbooks_t *abs)
{
    if(abs->m_size == 0)
    {
        cout << "記錄為空!" << endl;
    }
    else
    {
        cout << "姓名\t" << "年齡\t" << "性別\t" << "電話\t\t" << "地址\t\t" << endl;
        for(int i = 0; i < abs->m_size; ++i)
        {
            cout << abs->personArray[i].m_name << "\t";
            cout << abs->personArray[i].m_age << "\t";
            cout << (abs->personArray[i].m_sex == 1 ? "男" : "女") << "\t";
            cout << abs->personArray[i].m_phone << "\t";
            cout << abs->personArray[i].m_addr << "\t";
            cout << endl;
        }
    }
    system("pause");
}

//按姓名查找用戶,如果查找到返回聯系人在數組中的下標,未查找到返回-1
int findByName(addressbooks_t *abs,string name)
{
    for(int i = 0; i < abs->m_size; ++i)
    {
        if(name == abs->personArray[i].m_name)
        {
            return i;
        }
    }
    return -1;
}

void deletePerson(addressbooks_t *abs)
{
    string name;
    cout << "請輸入要刪除聯的系人姓名:";
    cin >> name;
    int index = findByName(abs,name);
    if(index == -1)
    {
        cout << "通訊錄中未查到" << name << endl;
    }
    else
    {
        for(int i = index; i < abs->m_size; ++i)
        {
            abs->personArray[i] = abs->personArray[i+1];
        }

        --abs->m_size;
        cout << "刪除成功" << endl;
    }
    system("pause");
}

void findPerson(addressbooks_t *abs)
{
    string name;
    cout << "請輸入要查找的聯系人姓名:";
    cin >> name;
    int index = findByName(abs,name);
    if(index == -1)
    {
        cout << "通訊錄中未查到" << name << endl;
    }
    else
    {
        cout << "姓名\t" << "年齡\t" << "性別\t" << "電話\t\t" << "地址\t\t" << endl;
        cout << abs->personArray[index].m_name << "\t";
        cout << abs->personArray[index].m_age << "\t";
        cout << (abs->personArray[index].m_sex == 1 ? "男" : "女") << "\t";
        cout << abs->personArray[index].m_phone << "\t";
        cout << abs->personArray[index].m_addr << "\t";
        cout << endl;
    }
    system("pause");
}

void updatePerson(addressbooks_t *abs)
{
    string name;
    cout << "請輸入要修改的聯系人姓名:";
    cin >> name;
    int index = findByName(abs,name);
    if(index == -1)
    {
        cout << "通訊錄中未查到" << name << endl;
    }
    else
    {
        //添加具體聯系人
        cout << "請輸入姓名: ";
        cin >> abs->personArray[index].m_name;

        int sex;
        while(true)
        {
            cout << "請輸入性別(1男,0女): ";
            cin >> sex;
            if(sex == 1 || sex == 0)
            {
                abs->personArray[index].m_sex = sex;
                break;
            }
            else
            {
                cout << "輸入有誤,請重新輸入!" << endl;
            }
        }

        cout << "請輸入年齡: ";
        cin >> abs->personArray[index].m_age;

        cout << "請輸入電話: ";
        cin >> abs->personArray[index].m_phone;

        cout << "請輸入地址: ";
        cin >> abs->personArray[index].m_addr;

        cout << "修改成功!" << endl;
    }
    system("pause");
}

void clearPerson(addressbooks_t *abs)
{
    abs->m_size = 0;
    cout << "清空成功!" << endl;
    system("pause");
}

int main()
{
    //創建一個結構體變量
    addressbooks_t abs;
    //初始化通訊錄中人員個數
    abs.m_size = 0;

    int select = 0;                 //創建用戶選擇輸入的變量
    while(true)
    {
        system("cls");              //清空屏幕
        showMenu();                 //菜單調用
        cout << "請輸入您的選擇: ";
        cin >> select;
        switch (select)
        {
        case 0:                      //0.退出通訊錄
            cout << "歡迎下次使用" << endl;
            return 0;
            break;
        case 1:                      //1.添加聯系人
            addPerson(&abs);
            break;
        case 2:                      //2.顯示聯系人
            displayPerson(&abs);
            break;
        case 3:                      //3.刪除聯系人
            deletePerson(&abs);
            break;
        case 4:                      //4.查找聯系人
            findPerson(&abs);
            break;
        case 5:                      //5.修改聯系人
            updatePerson(&abs);
            break;
        case 6:                      //6.清空聯系人
            clearPerson(&abs);
            break;
        default:
            break;
        }
    }

    return 0;
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: