1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #define MAX 1000
    5. //联系人
    6. struct Person {
    7. string uname;
    8. // 性别 1 男 2 女
    9. int sex;
    10. int age;
    11. string phone;
    12. string address;
    13. };
    14. //通讯录
    15. struct AddressBooks {
    16. //通讯录中保存的联系人数组
    17. struct Person personArray[MAX];
    18. //通讯录中当前记录联系人个数
    19. int size;
    20. };
    21. void showMenu() {
    22. cout << "-------------" << endl;
    23. cout << "1、添加联系人" << endl;
    24. cout << "2、显示联系人" << endl;
    25. cout << "3、删除联系人" << endl;
    26. cout << "4、查找联系人" << endl;
    27. cout << "5、修改联系人" << endl;
    28. cout << "6、清空联系人" << endl;
    29. cout << "0、退出通讯录" << endl;
    30. cout << "-------------" << endl;
    31. }
    32. /**
    33. * @func 添加联系人
    34. */
    35. void addPerson(AddressBooks *abs) {
    36. if(abs->size == MAX){
    37. cout << "通讯录已满,无法添加!" << endl;
    38. return;
    39. } else {
    40. //添加具体联系人
    41. //姓名
    42. string name;
    43. cout << "请输入姓名:" << endl;
    44. cin >> name;
    45. abs->personArray[abs->size].uname = name;
    46. //性别
    47. int sex = 0;
    48. cout << "请输入性别:" << endl;
    49. cout << "1---男" << endl;
    50. cout << "2---女" << endl;
    51. while(true){ //必须输入正确的数据
    52. cin >> sex;
    53. if(sex == 1 || sex == 2){
    54. abs->personArray[abs->size].sex = sex;
    55. break;
    56. }
    57. cout << "输入数据不正确,请重新输入!" << endl;
    58. }
    59. //年龄
    60. int age = 0;
    61. cout << "请输入年龄:" << endl;
    62. cin >> age;
    63. abs->personArray[abs->size].age = age;
    64. //联系电话
    65. string phone;
    66. cout << "请输入联系电话:" << endl;
    67. cin >> phone;
    68. abs->personArray[abs->size].phone = phone;
    69. //住址
    70. string address;
    71. cout << "请输入家庭住址:" << endl;
    72. cin >> address;
    73. abs->personArray[abs->size].address = address;
    74. //更新通讯录人数
    75. abs->size++;
    76. cout << "添加成功!" << endl;
    77. system("pause"); //按任意键继续
    78. system("cls"); //清屏
    79. }
    80. }
    81. /**
    82. * @func 显示联系人
    83. */
    84. void showPerson(struct AddressBooks *abs) {
    85. if(abs->size == 0){
    86. cout << "当前记录为空" << endl;
    87. } else {
    88. for(int i=0;i < abs->size;i++){
    89. cout << "姓名:" << abs->personArray[i].uname << "\t";
    90. cout << "性别:" << (abs->personArray[i].sex == 1 ? "男" : "女") << "\t";
    91. cout << "年龄:" << abs->personArray[i].age << "\t";
    92. cout << "电话:" << abs->personArray[i].phone << "\t";
    93. cout << "住址:" << abs->personArray[i].address << endl;
    94. }
    95. }
    96. system("pause");
    97. system("cls");
    98. }
    99. /**
    100. * @func 判断联系人是否存在
    101. */
    102. int isExist(struct AddressBooks *abs, string name) {
    103. for(int i=0;i<abs->size;i++){
    104. if(abs->personArray[i].uname == name){
    105. return i;
    106. }
    107. }
    108. return -1;
    109. }
    110. /**
    111. * @func 删除联系人
    112. */
    113. void delContact(AddressBooks *abs) {
    114. cout << "请输入要删除的联系人的姓名:" << endl;
    115. string name;
    116. cin >> name;
    117. int position = isExist(abs, name);
    118. if(position == -1){
    119. cout << "查无此人!" << endl;
    120. } else {
    121. cout << "已查询到该联系人,正在删除..." << endl;
    122. for(int i=position;i<abs->size;i++){
    123. //数据前移 ==> 删除
    124. abs->personArray[i] = abs->personArray[i+1];
    125. }
    126. abs->size--; //更新通讯录中人员数
    127. cout << "删除成功!" << endl;
    128. }
    129. system("pause");
    130. system("cls");
    131. }
    132. /**
    133. * @func 查找联系人
    134. */
    135. void queryPerson(struct AddressBooks *abs) {
    136. cout << "请输入要查找的联系人姓名:" << endl;
    137. string name;
    138. cin >> name;
    139. int exist = isExist(abs, name);
    140. if(exist == -1){
    141. cout << "查无此人!" << endl;
    142. } else {
    143. cout << "姓名:" << abs->personArray[exist].uname << "\t";
    144. cout << "性别:" << (abs->personArray[exist].sex == 1 ? "男" : "女") << "\t";
    145. cout << "年龄:" << abs->personArray[exist].age << "\t";
    146. cout << "电话:" << abs->personArray[exist].phone << "\t";
    147. cout << "住址:" << abs->personArray[exist].address << endl;
    148. }
    149. system("pause");
    150. system("cls");
    151. }
    152. /**
    153. * @func 修改联系人
    154. */
    155. void modifyPerson(struct AddressBooks *abs) {
    156. cout << "请输入要修改的联系人名称:" << endl;
    157. string name;
    158. cin >> name;
    159. int exist = isExist(abs, name);
    160. if(exist == -1){
    161. cout << "查无此人!" << endl;
    162. } else {
    163. cout << "以下为该联系人相关信息:" << endl;
    164. cout << "------------------------" << endl;
    165. cout << "姓名:" << abs->personArray[exist].uname << "\t";
    166. cout << "性别:" << (abs->personArray[exist].sex == 1 ? "男" : "女") << "\t";
    167. cout << "年龄:" << abs->personArray[exist].age << "\t";
    168. cout << "电话:" << abs->personArray[exist].phone << "\t";
    169. cout << "住址:" << abs->personArray[exist].address << endl;
    170. cout << "------------------------" << endl;
    171. cout << "请输入修改后的联系人信息:" << endl;
    172. cout << "------------------------" << endl;
    173. //姓名
    174. string name;
    175. cout << "请输入姓名:" << endl;
    176. cin >> name;
    177. abs->personArray[exist].uname = name;
    178. //性别
    179. int sex = 0;
    180. cout << "请输入性别:" << endl;
    181. cout << "1---男" << endl;
    182. cout << "2---女" << endl;
    183. while(true){ //必须输入正确的数据
    184. cin >> sex;
    185. if(sex == 1 || sex == 2){
    186. abs->personArray[exist].sex = sex;
    187. break;
    188. }
    189. cout << "输入数据不正确,请重新输入!" << endl;
    190. }
    191. //年龄
    192. int age = 0;
    193. cout << "请输入年龄:" << endl;
    194. cin >> age;
    195. abs->personArray[exist].age = age;
    196. //联系电话
    197. string phone;
    198. cout << "请输入联系电话:" << endl;
    199. cin >> phone;
    200. abs->personArray[exist].phone = phone;
    201. //住址
    202. string address;
    203. cout << "请输入家庭住址:" << endl;
    204. cin >> address;
    205. abs->personArray[exist].address = address;
    206. cout << "修改成功!" << endl;
    207. }
    208. system("pause");
    209. system("cls");
    210. }
    211. /**
    212. * @func 清空通讯录
    213. */
    214. void cleanBook(struct AddressBooks *abs) {
    215. cout << "即将删除所有联系人数据!确认删除吗?" << endl;
    216. cout << "1---是" << endl;
    217. cout << "0---否" << endl;
    218. int confirm = 0;
    219. cin >> confirm;
    220. if(confirm == 1){
    221. abs->size = 0; //逻辑删除
    222. cout << "通讯录已经清空!" << endl;
    223. } else if(confirm == 0){
    224. cout << "取消删除!" << endl;
    225. } else {
    226. cout << "请输入是/否!" << endl;
    227. }
    228. system("pause");
    229. system("cls");
    230. }
    231. int main() {
    232. //创建通讯录结构体变量
    233. AddressBooks abs;
    234. abs.size = 0;
    235. int select = 0; //用户选择输入的变量
    236. while(true){
    237. //菜单调用
    238. showMenu();
    239. cin >> select;
    240. switch (select) {
    241. case 1: //添加
    242. addPerson(&abs);
    243. break;
    244. case 2: //显示
    245. showPerson(&abs);
    246. break;
    247. case 3:
    248. delContact(&abs);
    249. break;
    250. case 4:
    251. queryPerson(&abs);
    252. break;
    253. case 5:
    254. modifyPerson(&abs);
    255. break;
    256. case 6:
    257. cleanBook(&abs);
    258. break;
    259. case 0:
    260. cout << "欢迎下次使用" << endl;
    261. system("pause");
    262. return 0;
    263. break;
    264. }
    265. }
    266. system("pause");
    267. return 0;
    268. }