喜迎
牛年
C++

C++ 增删查改

Posted by 陈陈菌 on 2020-07-17
Estimated Reading Time 11 Minutes
Words 2k In Total
Viewed Times

该练习使用了 文件操作、结构体、指针。用正则表达式读取 xml 的特定标签内容。这次练习算是对这些知识点有了加深的理解。

代码相关分享

GitHub : https://github.com/2670637999/Phone-book
码云:https://gitee.com/chenjiehai/Phone-book

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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <iostream>
#include <string.h>
#include <regex>
#include <fstream>
using namespace std;
#define MAX 1000

short inputValue;

struct Person{
string name;
string sex;
string age;
string phone;
};
struct Data{
Person person[MAX];
int sum;
};
/**初始化联系人**/
void CreatePerson(Data * data){
ifstream ifs;
ifs.open("Person.xml",ios::in|ios::binary);
if(ifs.is_open()){
cout << "文件打开成功,正在读出数据" << endl;
string buf;
cout << "正在读取姓名" << endl;
cout << "正在读取性别" << endl;
cout << "正在读取年龄" << endl;
cout << "正在读取电话" << endl;
while (getline(ifs,buf))
{

regex nameRx("<name>(.*)</name>");
regex sexRx("<sex>(.*)</sex>");
regex ageRx("<age>(.*)</age>");
regex phoneRx("<phone>(.*)</phone>");
smatch m;
if(regex_search(buf,m,nameRx)){
for (auto it = m.begin(); it != m.end(); it++){
data->person[data->sum].name = *it;
}
}
if(regex_search(buf,m,sexRx)){
for (auto it = m.begin(); it != m.end(); it++){
data->person[data->sum].sex = *it;
}
}
if(regex_search(buf,m,ageRx)){
for (auto it = m.begin(); it != m.end(); it++){
data->person[data->sum].age = *it;
}
}
if(regex_search(buf,m,phoneRx)){
for (auto it = m.begin(); it != m.end(); it++){
data->person[data->sum].phone = *it;
}
data->sum += 1;
}
}
cout << "读取完成" <<endl;
ifs.close();

}else{

ofstream ofs;
cout << "文件不存在,正在初始化文件数据" << endl;
ofs.open("Person.xml",ios::out|ios::binary);
for (int i = 0; i < data->sum; i++)
{
ofs << "<name>" << data->person[i].name << "</name>\n";
ofs << "<sex>" << data->person[i].name << "</sex>\n";
ofs << "<age>" << data->person[i].name << "</age>\n";
ofs << "<phone>" << data->person[i].name << "</phone>\n";
ofs << "\n";
}
ofs.close();

}
}

/*添加联系人*/
void AddPerson(Data * data){
if(data->sum == MAX){
cout << "通讯录已满: "<<endl;
return;
}else{
string name;
string sex;
string age;
string addr;
string phone;
cout << "请输入姓名: "<< endl;
cin >> name;
for (int i = 0; i < MAX; i++)
{
if(name == data->person[i].name){
cout << "已存在该姓名" <<endl;
system("pause");
return;
}
}
data->person[data->sum].name = name;
cout << "请输入性别: "<< endl;
cin >> sex;
data->person[data->sum].sex = sex;
cout << "请输入年龄: "<< endl;
cin >> age;
data->person[data->sum].age = age;
cout << "请输入电话: "<< endl;
cin >> phone;
for (int i = 0; i < MAX; i++)
{
if(phone == data->person[i].phone){
cout << "已存在该号码" <<endl;
cout << "正在终止操作" << endl;
system("pause");
return;
}
}
data->person[data->sum].phone = phone;
cout << "正在添加联系人... "<< endl;
data->sum += 1;
system("pause");
}
}
/*删除联系人*/
void deletePerson(Data * data){
string name;
cout << "请输入要删除的联系人: "<< endl;
cin >> name;
for (int i = 0; i < MAX; i++)
{
if(name != data->person[i].name){
cout << "不存在该联系人,无法删除" <<endl;
break;
}else if(name == data->person[i].name){
data->person[i].name = "";
data->person[i].sex = "";
data->person[i].age = "";
data->person[i].phone = "";
cout << "已删除联系人... "<< endl;
data->sum -= 1;
break;
}
}
system("pause");
}
/*查找联系人*/
void lookupPerson(Data * data){
string name;
cout << "请输入要查找的联系人: "<< endl;
cin >> name;
for (int i = 0; i < MAX; i++)
{
if(name == data->person[i].name){
cout << "\n姓名\t性别\t年龄\t电话" << endl;
cout << "------------------------------------" << endl;
cout << data->person[i].name << "\t" << data->person[i].sex <<
"\t" << data->person[i].age <<"\t"<< data->person[i].phone <<endl;
}else if(name != data->person[i].name){
cout << "查无此人" <<endl;
system("pause");
return;
}
}
cout << "\n\n查询完毕"<<endl;
system("pause");

}
/*修改联系人*/
void modifyPerson(Data * data){
string name;
cout << "请输入要修改的联系人: "<< endl;
cin >> name;
for (int i = 0; i < MAX; i++)
{
if(name == data->person[i].name){
string inputString;
cout << "修改项? (姓名)(性别)(年龄)(电话): "<< endl;
cin >> inputString;
if(inputString == "姓名"){
string inputName;
cout << "修改姓名为? : "<< endl;
cin >> inputName;
data->person[i].name = inputName;
cout << "\n修改完毕"<<endl;
system("pause");
}else if(inputString == "性别"){
string inputSex;
cout << "修改性别为? : "<< endl;
cin >> inputSex;
data->person[i].sex = inputSex;
cout << "\n修改完毕"<<endl;
system("pause");
}else if(inputString == "年龄"){
string inputAge;
cout << "修改年龄为? : "<< endl;
cin >> inputAge;
data->person[i].age = inputAge;
cout << "\n修改完毕"<<endl;
system("pause");
}else if(inputString == "电话"){
string inputPhone;
cout << "修改电话为? : "<< endl;
cin >> inputPhone;
data->person[i].phone = inputPhone;
cout << "\n修改完毕"<<endl;
system("pause");
}
}else if(name != data->person[i].name){
cout << "查无此人" <<endl;
system("pause");
return;
}
}
}
/*清空联系人*/
void emptyData(Data * data){
short inputValue;
cout << "确定要清空所有联系人吗? [1]是 [2]否 : "<< endl;
cin >> inputValue;
if(inputValue == 1){
for (int i = 0; i < MAX; i++)
{
data->person[i].name="";
data->person[i].sex="";
data->person[i].age="";
data->person[i].phone="";
}
data->sum = 0;
cout << "正在清空所有数据..."<< endl;
system("pause");
}else if(inputValue == 0){

}
}
/*显示通讯录*/
void showMenu(){
Data data;
Data * data1 = &data;
data.sum = 0;
CreatePerson(data1);
system("pause");
while (true)
{
system("cls");
cout << "+---------------+" <<endl;
cout << "|-[1]添加联系人-|" <<endl;
cout << "+---------------+" <<endl;
cout << "|-[2]显示联系人-|" <<endl;
cout << "+---------------+" <<endl;
cout << "|-[3]删除联系人-|" <<endl;
cout << "+---------------+" <<endl;
cout << "|-[4]查找联系人-|" <<endl;
cout << "+---------------+" <<endl;
cout << "|-[5]修改联系人-|" <<endl;
cout << "+---------------+" <<endl;
cout << "|-[6]清空联系人-|" <<endl;
cout << "+---------------+" <<endl;
cout << "|-[0]保存并退出-|" <<endl;
cout << "+---------------+" <<endl;
cout << "请输入相关操作: ";
cin >> inputValue;
if(inputValue == 0){
ofstream ofs;
ofs.open("Person.xml",ios::out|ios::binary);
for (int i = 0; i < data.sum; i++)
{
if(data1->sum==0){
ofs << "<name>" << data1->person[i].name << "</name>\n";
}else if(data1->sum>0){
ofs << "\n<name>" << data1->person[i].name << "</name>\n";
}
ofs << "<sex>" << data1->person[i].sex << "</sex>\n";
ofs << "<age>" << data1->person[i].age << "</age>\n";
ofs << "<phone>" << data1->person[i].phone << "</phone>\n";
}
cout << "正在保存数据并退出程序...\n"<<endl;
break;
}else if(inputValue == 1){
AddPerson(data1);

}else if(inputValue == 2){
if(data.sum == 0){
cout << "通讯录是空的" <<endl;
system("pause");
}else{
cout << "\n姓名\t性别\t年龄\t电话" << endl;
for (int i = 0; i < data.sum; i++)
{
cout << "------------------------------------" << endl;
cout << data.person[i].name <<"\t"<< data.person[i].sex <<"\t" << data.person[i].age <<"\t" << data.person[i].phone << endl;
}
cout << "\n总联系人 = " << data.sum <<endl;
cout << endl;
system("pause");
}
}else if(inputValue == 3){
if(data.sum == 0){
cout << "通讯录是空的" <<endl;
system("pause");
}else{
deletePerson(data1);
}
}else if(inputValue == 4){
if(data.sum == 0){
cout << "通讯录是空的" <<endl;
system("pause");
}else{
lookupPerson(data1);
}
}else if(inputValue == 5){
if(data.sum == 0){
cout << "通讯录是空的" <<endl;
system("pause");
}else{
modifyPerson(data1);
}
}else if(inputValue == 6){
if(data.sum == 0){
cout << "通讯录是空的" <<endl;
system("pause");
}else{
emptyData(data1);
}
}else{
cout << "输入无效命令,请重新输入" <<endl;
system("pause");
}
}
}
int main(){
showMenu();
return 0;
}