服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 利用C++实现通讯录管理系统的完整代码

利用C++实现通讯录管理系统的完整代码

2022-12-22 15:35codexixi C/C++

通讯录是一个可以记录亲人、好友信息的工具,下面这篇文章主要给大家介绍了关于利用C++实现通讯录管理系统的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

通讯录管理系统

学习目标:

对C++的基础进行复习,为后续深入学习打好基础

案例描述:

通讯录是一个可以记录亲人、好友信息的工具。

本教程主要利用C++来实现一个通讯录管理系统

系统中需要实现的功能如下:

  • 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
  • 显示联系人:显示通讯录中所有联系人信息
  • 删除联系人:按照姓名进行删除指定联系人
  • 查找联系人:按照姓名查看指定联系人信息
  • 修改联系人:按照姓名重新修改指定联系人
  • 清空联系人:清空通讯录中所有信息
  • 退出通讯录:退出当前使用的通讯录

实现代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <iostream>
#include <string>
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 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.personArr[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 findPerson(addressbook* person)//查找联系人
{
    string name;
    cout << "请输入您要查找的联系人姓名:" << endl;
    cin >> name;
    int exist = isExist(person, name);
    if (exist != -1)
    {
        cout << "该联系人信息如下:" << endl;
        cout << "姓名:" << person->personArr[exist].name << " "
            << "性别:" << (person->personArr[exist].sex == 1 ? "男" : "女") << " "
            << "年龄:" << person->personArr[exist].age << " "
            << "电话:" << person->personArr[exist].phonenumber << " "
            << "住址:" << person->personArr[exist].address << " " << 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;
        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.person_size = 0;
 
    while (true)
    {
        showMenu();
        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;
        }
    }
}

运行结果:

利用C++实现通讯录管理系统的完整代码

这个系统里用到了system(“cls”),这个是清屏的意思。

“system("cls")”是在C语言程序中,调用系统命令cls完成清屏操作。

system函数是C语言提供的与操作系统衔接的函数,函数原型如下:

?
1
2
#include <stdlib.h> //所在头文件
int system(const char *command);  //参数为操作系统命令

函数功能:execute a shell command 执行一个操作系统命令

如:

?
1
2
system("time /t") ;显示时间
system("dir"); //列目录

示例:

?
1
2
3
4
5
#include<stdlib.h>
main()
{system("cls");/*清屏*/
system("dirc://");/*列C盘根目录*/
}

总结

到此这篇关于利用C++实现通讯录管理系统的文章就介绍到这了,更多相关C++通讯录管理系统内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_44721910/article/details/122590205

延伸 · 阅读

精彩推荐
  • C/C++C语言实现的猴子偷桃之类算法

    C语言实现的猴子偷桃之类算法

    本文给大家分享的是前些日子去面试的时候的试题,哎,真是没想到会出这么个题,好多年没碰过C了。。。。分享给大家,小伙伴们过来参观下吧。...

    C语言教程网10202021-02-23
  • C/C++c语言求阶乘精确值示例

    c语言求阶乘精确值示例

    这篇文章主要介绍了c语言求阶乘精确值示例,需要的朋友可以参考下...

    C语言程序设计4192021-01-18
  • C/C++大家注意vector, list, set, map成员函数erase

    大家注意vector, list, set, map成员函数erase

    set和map是由红黑树来实现的,当erase的时候迭代器就失效了,也就是说我们要在迭代器失效之前保留一个副本,根据这个副本我们才能继续遍历下一个元素...

    C语言教程网10962020-12-31
  • C/C++C语言运用回调函数实现计算器

    C语言运用回调函数实现计算器

    这篇文章主要为大家详细介绍了C语言运用回调函数实现计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    久留不见i10042022-01-20
  • C/C++C++ 控制台弹出文件管理对话框案例

    C++ 控制台弹出文件管理对话框案例

    这篇文章主要介绍了C++ 控制台弹出文件管理对话框案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    汪蛋9492021-10-19
  • C/C++c++ 判断是64位还是32位系统的实例

    c++ 判断是64位还是32位系统的实例

    这篇文章主要介绍了c++ 判断是64位还是32位系统的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    wjbooks10762021-10-11
  • C/C++C语言图文并茂讲解分支语句用法

    C语言图文并茂讲解分支语句用法

    分支结构的执行是依据一定的条件选择执行路径,而不是严格按照语句出现的物理顺序。分支结构的程序设计方法的关键在于构造合适的分支条件和分析程...

    清风自在 流水潺潺4962022-11-12
  • C/C++C语言中fgetgrent()函数和fgetpwent()函数的用法对比

    C语言中fgetgrent()函数和fgetpwent()函数的用法对比

    这篇文章主要介绍了C语言中fgetgrent()函数和fgetpwent()函数的用法对比,分别用于读取组格式函数和读取密码格式,需要的朋友可以参考下...

    C语言教程网10192021-03-09