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

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

服务器之家 - 编程语言 - C/C++ - C++实现通讯录小功能

C++实现通讯录小功能

2023-02-07 14:42糯米团子鸭 C/C++

这篇文章主要为大家详细介绍了C++实现通讯录小功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现通讯录功能的具体代码,供大家参考,具体内容如下

思路:

1.显示菜单栏

?
1
2
3
4
5
6
7
8
9
10
11
12
void menu() {
 
    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;
}

2.退出

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
 
    int select = 0;
    cin >> select;
    switch (select) {
        
        case 0://退出通讯录
            cout << "欢迎下次使用" << endl;
            system("pause");
            return 0;
            break;
        
    }
    
    system("pause");
    return 0;
}

3.创建结构体

3.1创建联系人结构体
3.2创建通讯录结构体

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//定义联系人结构体
//姓名、电话号码、邮箱、地址
struct person {
    string name;
    string number;
    string Email;
    string address;
};
// 定义通讯录结构体
struct contacts {
    int people_num;
    struct person personarr[MAX];//子结构体:联系人信息
};

4.添加联系人

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void addperson(struct contacts* p) {
    if (p->people_num == MAX ) {
        cout << "通讯录已满" << endl;
    }
    else {//添加联系人信息
        string name, number, Email, address;
        cout << "请输入姓名:" << endl;
        cin >> name;
        cout << "请输入电话:" << endl;
        cin >> number;
        cout << "请输入邮箱:" << endl;
        cin >> Email;
        cout << "请输入地址:" << endl;
        cin >> address;
        p->personarr[p->people_num].name = name;
        p->personarr[p->people_num].number = number;
        p->personarr[p->people_num].Email = Email;
        p->personarr[p->people_num].address = address;
        
        p->people_num++;
        cout << "添加成功!" << endl;
    }
}

5.删除联系人

判断联系人是否存在

?
1
2
3
4
5
6
7
8
9
int existperson(struct contacts* p,string name) {
    
    for (int i = 0; i < p->people_num; i++) {
        if ( p->personarr[i].name == name ) {
            return i;
        }
    }
    return -1;
}

若存在,获取联系人在通讯录位置,将position后面的都往前移动一个位置,覆盖之前的值

?
1
2
3
4
5
6
7
8
9
//删除联系人
void delperson(struct contacts* p,int position) {
    while (position < (p->people_num)) {
        p->personarr[position] = p->personarr[position + 1];
        position++;
    }
    p->people_num--;
    cout << "删除成功!" << endl;
}

6.修改

检查要修改联系人是否存在,并获取当前位置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void modifyperson(struct contacts* p, int position) {
    string  number, Email, address;
    
    cout << "请输入修改电话:" << endl;
    cin >> number;
    cout << "请输入修改邮箱:" << endl;
    cin >> Email;
    cout << "请输入修改地址:" << endl;
    cin >> address;
 
    p->personarr[position].number = number;
    p->personarr[position].Email = Email;
    p->personarr[position].address = address;
    cout << "修改成功!" << endl;
}

7.查找

?
1
2
3
4
5
6
void searchperson(struct contacts* p, int position) {
    cout << "姓名:" << p->personarr[position].name << "    "
        << "电话:" << p->personarr[position].number << "    "
        << "邮箱:" << p->personarr[position].Email << "    "
        << "地址:" << p->personarr[position].address << "    ";
}

8.显示通讯录

?
1
2
3
4
5
6
7
8
9
void showcontact(struct contacts* p) {
    for (int i = 0; i < (p->people_num); i++) {
        cout <<"姓名:" << p->personarr[i].name << "    "
            <<"电话:" << p->personarr[i].number << "    "
            <<"邮箱:" << p->personarr[i].Email << "    "
            <<"地址:" << p->personarr[i].address << "    "<< endl;
 
    }        
}

9.清空通讯录

?
1
2
3
4
void cleancontact(struct contacts* p) {
    p->people_num = 0;
    cout << "已清空!" << endl;
}

全部代码

?
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
#include<iostream>
using namespace std;
#define MAX 200 
 
//1.菜单栏显示
void menu() {
 
    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;
}
 
//2.定义结构体
//2.1定义联系人结构体
//姓名、电话号码、邮箱、地址
struct person {
    string name;
    string number;
    string Email;
    string address;
};
//2.2 定义通讯录结构体
struct contacts {
    int people_num;
    struct person personarr[MAX];//子结构体:联系人信息
};
 
//3.添加联系人
void addperson(struct contacts* p) {
    if (p->people_num == MAX ) {
        cout << "通讯录已满" << endl;
    }
    else {//添加联系人信息
        string name, number, Email, address;
        cout << "请输入姓名:" << endl;
        cin >> name;
        cout << "请输入电话:" << endl;
        cin >> number;
        cout << "请输入邮箱:" << endl;
        cin >> Email;
        cout << "请输入地址:" << endl;
        cin >> address;
        p->personarr[p->people_num].name = name;
        p->personarr[p->people_num].number = number;
        p->personarr[p->people_num].Email = Email;
        p->personarr[p->people_num].address = address;
        
        p->people_num++;
        cout << "添加成功!" << endl;
    }
}
//4.删除联系人
//4.1检测联系人是否存在
int existperson(struct contacts* p,string name) {
    
    for (int i = 0; i < p->people_num; i++) {
        if ( p->personarr[i].name == name ) {
            return i;
        }
    }
    return -1;
}
//删除联系人
void delperson(struct contacts* p,int position) {
    while (position < (p->people_num)) {
        p->personarr[position] = p->personarr[position + 1];
        position++;
    }
    p->people_num--;
    cout << "删除成功!" << endl;
}
//5.修改联系人
void modifyperson(struct contacts* p, int position) {
    string  number, Email, address;
    
    cout << "请输入修改电话:" << endl;
    cin >> number;
    cout << "请输入修改邮箱:" << endl;
    cin >> Email;
    cout << "请输入修改地址:" << endl;
    cin >> address;
 
    p->personarr[position].number = number;
    p->personarr[position].Email = Email;
    p->personarr[position].address = address;
    cout << "修改成功!" << endl;
}
 
//6.查找联系人
void searchperson(struct contacts* p, int position) {
    cout << "姓名:" << p->personarr[position].name << "    "
        << "电话:" << p->personarr[position].number << "    "
        << "邮箱:" << p->personarr[position].Email << "    "
        << "地址:" << p->personarr[position].address << "    ";
}
 
//7.显示通讯录
void showcontact(struct contacts* p) {
    for (int i = 0; i < (p->people_num); i++) {
        cout <<"姓名:" << p->personarr[i].name << "    "
            <<"电话:" << p->personarr[i].number << "    "
            <<"邮箱:" << p->personarr[i].Email << "    "
            <<"地址:" << p->personarr[i].address << "    "<< endl;
 
    }        
}
//8.清空联系人
void cleancontact(struct contacts* p) {
    p->people_num = 0;
    cout << "已清空!" << endl;
}
 
int main() {
    //创建通讯录结构体变量
    struct contacts c;
    //初始化通讯录当前联系人个数
    c.people_num = 0;
    int select = 0;
    string name;
    while (1) {
        menu();
        cin >> select;
        switch (select) {
            case 1:// 添加联系人
                addperson(&c);
                system("pause");
                break;
 
            case 2:// 删除联系人
    
                cout << "请输入删除联系人的姓名:";
                cin >> name;
                if (existperson(&c,name)==-1) {
                    cout << "该联系人不存在!";
                }
                else{
                    delperson(&c, existperson(&c, name));
                }
                
                system("pause");
                break;
 
            case 3:// 修改联系人
            
                cout << "请输入要修改联系人的姓名:";
                cin >> name;
                if (existperson(&c,name) == -1) {
                    cout << "该联系人不存在!";
                }
                else {
                    modifyperson(&c, existperson(&c, name));
                }
                
                system("pause");
                break;
 
            case 4:// 查找联系人
                
                cout << "请输入查找联系人的姓名:";
                cin >> name;
                if (existperson(&c,name) == -1) {
                    cout << "该联系人不存在!";
                }
                else {
                    searchperson(&c, existperson(&c, name));
                }
                
                system("pause");
                break;
 
            case 5:// 显示通讯录 
                showcontact(&c);
                system("pause"); 
                break;
            case 6:// 清空联系人
                cleancontact(&c);
                system("pause");
                break;
            case 0://退出通讯录
                cout << "欢迎下次使用" << endl;
                system("pause");//暂停批文件的处理
                return 0;
                break;
        }
        system("cls");//清屏
    }
    
    system("pause");
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_43332829/article/details/124420414

延伸 · 阅读

精彩推荐