鱼C论坛

 找回密码
 立即注册
查看: 1786|回复: 5

[作品展示] c++简易通讯录

[复制链接]
发表于 2022-9-24 19:10:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 蜜柑面包 于 2022-9-24 19:11 编辑
#include<iostream>
#include<string>

#define zuidarenshu 1000

using namespace std;

struct lianxiren
{
        string name;
        int xingbie;//1男2女
        int age;
        string phoneNumber;
        string dizhi;
};

struct tongxunlu
{
        struct lianxiren renyuan[zuidarenshu];
        int renshu;
};

void tianjia(tongxunlu* txl)
{
        if (txl->renshu == zuidarenshu)
        {
                cout << "通讯录已满,无法添加!" << endl;
                return;
        }
        else
        {
                string name;
                cout << "请输入姓名:" << endl;
                cin >> name;
                txl->renyuan[txl->renshu].name = name;

                cout << "请输入性别:" << endl;
                cout << "1 --- 男" << endl;
                cout << "2 --- 女" << endl;
                int xingbie = 0;

                while (true)
                {
                        cin >> xingbie;
                        if (xingbie == 1 || xingbie == 2)
                        {
                                txl->renyuan[txl->renshu].xingbie = xingbie;
                                break;
                        }
                        cout << "输入有误,请重新输入" << endl;
                }

                cout << "请输入年龄:" << endl;
                int age = 0;

                while (true)
                {
                        cin >> age;
                        if (age > 0)
                        {
                                txl->renyuan[txl->renshu].age = age;
                                break;
                        }
                        cout << "输入有误,请重新输入" << endl;
                }

                cout << "请输入联系电话:" << endl;
                string pn;
                cin >> pn;
                txl->renyuan[txl->renshu].phoneNumber = pn;

                cout << "请输入家庭住址:" << endl;
                string zhuzhi;
                cin >> zhuzhi;
                txl->renyuan[txl->renshu].dizhi = zhuzhi;

                txl->renshu++;

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

                system("pause");
                system("cls");
        }
}

void xianshi(tongxunlu* txl)
{
        if (txl->renshu == 0)
        {
                cout << "当前的记录为空" << endl;
        }
        else
        {
                for (int i = 0; i < txl->renshu; i++)
                {
                        cout << "姓名: " << txl->renyuan[i].name << '\t';
                        cout << "性别: " << (txl->renyuan[i].xingbie == 1 ? "男" : "女") << '\t';
                        cout << "年龄: " << txl->renyuan[i].age << '\t';
                        cout << "电话: " << txl->renyuan[i].phoneNumber << '\t';
                        cout << "住址: " << txl->renyuan[i].dizhi << endl;
                }
        }

        system("pause");
        system("cls");
}

int jiance(tongxunlu* txl, string name)
{
        for (int i = 0; i < txl->renshu; i++)
        {
                if (txl->renyuan[i].name == name)
                {
                        return i;
                }
        }
        return -1;
}

void shanchu(tongxunlu* txl)
{
        cout << "请输入您要删除的联系人:" << endl;

        string name;
        cin >> name;

        int jieguo = jiance(txl, name);

        if (jieguo != -1)
        {
                for (int i = jieguo; i < txl->renshu; i++)
                {
                        txl->renyuan[i] = txl->renyuan[i + 1];
                }
                txl->renshu--;
                cout << "删除成功" << endl;
        }
        else
        {
                cout << "查无此人" << endl;
        }

        system("pause");
        system("cls");
}

void chazhao(tongxunlu* txl)
{
        cout << "请输入您要查找的联系人:" << endl;
        string name;
        cin >> name;

        int jieguo = jiance(txl, name);

        if (jieguo != -1)
        {
                cout << "姓名: " << txl->renyuan[jieguo].name << '\t';
                cout << "性别: " << (txl->renyuan[jieguo].xingbie == 1 ? "男" : "女") << '\t';
                cout << "年龄: " << txl->renyuan[jieguo].age << '\t';
                cout << "号码: " << txl->renyuan[jieguo].phoneNumber << '\t';
                cout << "住址: " << txl->renyuan[jieguo].dizhi << endl;
        }
        else
        {
                cout << "查无此人" << endl;
        }

        system("pause");
        system("cls");
}

void xiugai(tongxunlu* txl)
{
        cout << "请输入您要修改的联系人:" << endl;
        string name;
        cin >> name;

        int jieguo = jiance(txl, name);

        if (jieguo != -1)
        {
                string name;
                cout << "请输入姓名:" << endl;
                cin >> name;
                txl->renyuan[jieguo].name = name;

                cout << "请输入性别:" << endl;
                cout << "1 --- 男" << endl;
                cout << "2 --- 女" << endl;
                int xingbie = 0;

                while (true)
                {
                        cin >> xingbie;
                        if (xingbie == 1 || xingbie == 2)
                        {
                                txl->renyuan[jieguo].xingbie = xingbie;
                                break;
                        }
                        cout << "输入有误,请重新输入" << endl;
                }

                cout << "请输入年龄:" << endl;
                int age = 0;

                while (true)
                {
                        cin >> age;
                        if (age > 0)
                        {
                                txl->renyuan[jieguo].age = age;
                                break;
                        }
                        cout << "输入有误,请重新输入" << endl;
                }

                cout << "请输入联系电话:" << endl;
                string pn;
                cin >> pn;
                txl->renyuan[jieguo].phoneNumber = pn;

                cout << "请输入家庭住址:" << endl;
                string zhuzhi;
                cin >> zhuzhi;
                txl->renyuan[jieguo].dizhi = zhuzhi;

                cout << "修改成功" << endl;
        }
        else
        {
                cout << "查无此人" << endl;
        }

        system("pause");
        system("cls");
}

void qingkong(tongxunlu* txl)
{
        if (txl->renshu != 0)
        {
                flag:
                cout << "是否确定清空通讯录,清空后无法恢复!" << endl;
                cout << "1 --- 清空" << endl;
                cout << "2 --- 不清空" << endl;
                int jieguo = 0;
                cin >> jieguo;
                if (jieguo == 1)
                {
                        txl->renshu = 0;
                        cout << "通讯录已清空" << endl;
                }
                else if (jieguo == 2)
                        cout << "已取消" << endl;
                else
                {
                        
                        system("cls");

                        cout << "错误!请重新输入:" << endl;

                        goto flag;
                }
        }
        else
        {
                cout << "当前的记录为空" << endl;
        }
        system("pause");
        system("cls");
}

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

int main()
{
        tongxunlu txl;

        txl.renshu = 0;

        int xuanze = 0;

        while (true)
        {
                caidan();

                cin >> xuanze;
                
                switch (xuanze)
                {
                case 1:
                        tianjia(&txl);
                        break;
                case 2:
                        xianshi(&txl);
                        break;
                case 3:
                        shanchu(&txl);
                        break;
                case 4:
                        chazhao(&txl);
                        break;
                case 5:
                        xiugai(&txl);
                        break;
                case 6:
                        qingkong(&txl);
                        break;
                case 0:
                        cout << "欢迎下次使用" << endl;
                        system("pause");
                        return 0;
                        break;
                default:
                        cout << "请重新输入:" << endl;
                        break;
                }
        }
}

评分

参与人数 1荣誉 +5 收起 理由
高山 + 5 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-9-24 19:12:50 | 显示全部楼层
有bug可以指出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-25 10:51:00 | 显示全部楼层
挺不错的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-27 20:50:48 | 显示全部楼层
功能很简单,但是却很稳定,没有bug,建议加上
#include
system("color 数字")
我觉得会更好哟
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-28 10:13:19 | 显示全部楼层
发表一些我自己的看法吧,不喜勿喷。
我觉得这个代码定义名称的时候不要写拼音,比如通讯录可以写AddressBook、联系人可以写AddressPerson、最大人数就写MAXimum等,比拼音直接容易看懂。
然后你用1/2判断性别的时候,如果用户不输入这两个字的情况你也应该考虑,或者用户不输入数字,输入其他字符或者汉字。这个时候你的程序是否还能正常运行下去。
我的建议是内存足够的情况下,能尽量加判断语句就加吧,毕竟不是每个用户都那么“乖”。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-28 14:51:15 | 显示全部楼层
C++是面向对象的,这里没有对象,就是用了C++的语法干了C的事
是否能把员工弄成一个类,加人就NEW一个,减人就DELETE一个
我正在学C++,咱们共同加油
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-8 03:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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