吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1164|回复: 6
收起左侧

[C&C++ 原创] 通讯录管理系统优化代码

[复制链接]
hzh193536 发表于 2024-8-15 08:57
[C++] 纯文本查看 复制代码
#include "menu.h"

int main()
{
    // 创建通讯录
    AddressBooks abs;
    abs.size = 0;

    int select = 0;

    while (true)
    {
        // system("clear");
        showMenu();

        cin >> select;

        switch (select)
        {
        case 1:
            addPerson(&abs);
            break;
        case 2:
            showPerson(&abs);
            break;
        case 3:
            deletePerson(&abs);
            break;
        case 4:
            findPerson(&abs);
            break;
        case 5:
            modifyPerson(&abs);
            break;
        case 6:
            cleanPerson(&abs);
            break;
        case 0:
            cout << "欢迎下次使用" << endl;
            return 0;
            break;
        default:
            cout << "输入错误,请重新输入" << endl;
            break;
        }
    }

    return 0;
}


menu.h
[C++] 纯文本查看 复制代码
#pragma once // 编译器保证文件只包含一次
// 缺点:依赖编译器,影响代码移植性,物理文件只包含一次,如果拷贝多份还是会重复包含
// #ifndef--#define--#endif,使用的是宏定义,支持c环境即可,移植性好

#include <iostream>
#include <string>

using namespace std;

#define MAX_NUM 1000

struct Person
{
    string name;
    int sex; // 1男 2女
    int age;
    string phone;
    string address;
};

struct AddressBooks
{
    struct Person personArray[MAX_NUM];
    int size; // 通讯录中人员个数
};

void showMenu();
void addPerson(AddressBooks *abs);
void showPerson(AddressBooks *abs);
void deletePerson(AddressBooks *abs);
void findPerson(AddressBooks *abs);
void modifyPerson(AddressBooks *abs);
void cleanPerson(AddressBooks *abs);


menu.cpp
[C++] 纯文本查看 复制代码
#include "menu.h"

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 setPerson(AddressBooks *abs, int pos)
{
    cout << "请输入姓名:" << endl;
    string name = "";
    cin >> name;
    abs->personArray[pos].name = name;

    cout << "请输入性别:" << endl;
    cout << "1 -- 男" << endl;
    cout << "2 -- 女" << endl;
    int sex = 0;
    while (true)
    {
        cin >> sex; // sex是int类型,当输入字符时,会产生cin.fail,产生错误状态,输入流无法再次读取,会进入死循环
        if (cin.fail())
        {
            cin.clear(); // 清除cin的错误状态
            // cin.sync();  // 清除输入缓冲区,不生效,使用ignore
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "输入类型有误,请重新输入" << endl;
            continue;
        }

        if (sex == 1 || sex == 2)
        {
            abs->personArray[pos].sex = sex;
            break;
        }
        cout << "输入有误,请重新输入" << endl;
    }

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

    cout << "请输入联系电话:" << endl;
    string phone = "";
    cin >> phone;
    abs->personArray[pos].phone = phone;

    cout << "请输入家庭住址:" << endl;
    string address = "";
    cin >> address;
    abs->personArray[pos].address = address;
}

void addPerson(AddressBooks *abs)
{
    if (abs->size == MAX_NUM)
    {
        cout << "通讯录已满,无法添加" << endl;
    }
    else
    {
        setPerson(abs, abs->size);

        // 更新通信录人数
        abs->size++;

        cout << "添加成功" << endl;
    }
}

void printPerson(AddressBooks *abs, int pos)
{
    cout << "姓名:" << abs->personArray[pos].name << "\t";
    cout << "性别:" << (abs->personArray[pos].sex == 1 ? "男" : "女") << "\t";
    cout << "年龄:" << abs->personArray[pos].age << "\t";
    cout << "电话:" << abs->personArray[pos].phone << "\t";
    cout << "住址:" << abs->personArray[pos].address << endl;
}

void showPerson(AddressBooks *abs)
{
    if (abs->size == 0)
    {
        cout << "当前记录为空" << endl;
    }
    else
    {
        for (int i = 0; i < abs->size; i++)
        {
            printPerson(abs, i);
        }
    }
}

// 判断是否存在查询的人员,存在返回在数组中索引位置,不存在返回-1
int isExist(AddressBooks *abs, string name)
{
    for (int i = 0; i < abs->size; i++)
    {
        if (abs->personArray[i].name == name)
        {
            return i;
        }
    }

    return -1;
}

void deletePerson(AddressBooks *abs)
{
    cout << "请输入您要删除的联系人:" << endl;
    string name;
    cin >> name;

    int ret = isExist(abs, name);
    if (ret != -1)
    {
        for (int i = ret; i < abs->size - 1; i++)
        {
            abs->personArray[i] = abs->personArray[i + 1];
        }
        abs->size--;
        cout << "删除成功" << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
}

void findPerson(AddressBooks *abs)
{
    cout << "请输入您要查找的联系人:" << endl;

    string name;
    cin >> name;

    int ret = isExist(abs, name);
    if (ret != -1)
    {
        printPerson(abs, ret);
    }
    else
    {
        cout << "查无此人" << endl;
    }
}

void modifyPerson(AddressBooks *abs)
{
    cout << "请输入您要修改的联系人:" << endl;

    string name;
    cin >> name;

    int ret = isExist(abs, name);
    if (ret != -1)
    {
        setPerson(abs, ret);
        cout << "修改成功" << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
}

void cleanPerson(AddressBooks *abs)
{
    abs->size = 0;
    cout << "通讯录已清空" << endl;
}

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

GXNNYWF 发表于 2024-8-15 12:46
嗯,这个是好东西,有成品吗?
nxyuym 发表于 2024-8-15 14:49
ailmail 发表于 2024-8-15 14:55
wu2_cgj 发表于 2024-8-15 15:24
只能保存在内存里
xiaoyao2 发表于 2024-8-20 03:15
看不到的吗
guozishula 发表于 2024-8-29 23:59
这个哪里有呀?楼主。使用起来方便
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-12 13:45

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表