经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C++类实现通讯录功能
来源:jb51  时间:2022/6/20 19:56:28  对本文有异议

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

软件使用的是Microsoft Visual Studio

编写通讯录之前,先思考一下要实现什么功能,大概的结构,要创建几个类等等。

首先,是思考要实现什么功能。

一般的通讯录有添加,删除,修改,查找,显示等功能,一般联系人的信息包括:姓名,性别,年龄,电话号码,家庭地址。

我们首先新建一个类,用来初始化姓名,年龄,性别,电话号码,家庭地址,这几个变量

  1. #pragma once
  2. #include<iostream>
  3. #include<string>
  4. //#include "MailList.hpp"
  5. using namespace std;
  6. class MailList
  7. {
  8. public:
  9. ?? ?void setName(string);//给变量赋值函数
  10. ?? ?void setAge(string);
  11. ?? ?void setSex(string);
  12. ?? ?void setTel(string);
  13. ?? ?void setAddress(string);
  14. ?? ?string getName();//返回变量值函数
  15. ?? ?string getAge();
  16. ?? ?string getSex();
  17. ?? ?string getTel();
  18. ?? ?string getAddress();
  19. ?
  20. private:
  21. ?? ?string name;//私有函数成员,定义变量
  22. ?? ?string age;
  23. ?? ?string sex;
  24. ?? ?string tel;
  25. ?? ?string address;
  26. };
  27. ?
  28. ?
  29. void MailList::setName(string name)
  30. {
  31. ?? ?this->name=name;
  32. }
  33. void MailList::setAge(string age)
  34. {
  35. ?? ?this->age=age;
  36. }
  37. void MailList::setSex(string sex)
  38. {
  39. ?? ?this->sex=sex;
  40. }
  41. void MailList::setTel(string tel)
  42. {
  43. ?? ?this->tel=tel;
  44. }
  45. void MailList::setAddress(string address)
  46. {
  47. ?? ?this->address=address;
  48. }
  49. string MailList::getName()
  50. {
  51. ?? ?return this->name;
  52. }
  53. string MailList::getAge()
  54. {
  55. ?? ?return this->age;
  56. }
  57. string MailList::getSex()
  58. {
  59. ?? ?return this->sex;
  60. }
  61. string MailList::getTel()
  62. {
  63. ?? ?return this->tel;
  64. }
  65. string MailList::getAddress()
  66. {
  67. ?? ?return this->address;
  68. }

这里也可以使用构造函数初始化函数成员,构造函数函数名与类名一样。

然后,创建一个通讯录管理类,先把总的结构搭建起来

  1. class MailListManager//通讯录管理类
  2. {
  3. public:
  4. ?? ?MailListManager();//构造函数
  5. ?? ?void initList();//初始化通讯录功能,在通讯录里记录为空时使用
  6. ?? ?void insertList();//添加联系人功能,在通讯录里有记录时使用
  7. ?? ?void showList();//显示联系人功能,显示通讯录中所有联系人的记录
  8. ?? ?void deleteList();//删除联系人功能
  9. ?? ?void selectList();//查找联系人功能
  10. ?? ?void updateList();//修改联系人功能
  11. ?? ?void dropList();//清空通讯录功能
  12. ?? ?void save();//保存到文件,文件的写入
  13. ?? ?void loading();//加载,读出文件
  14. ?? ?string pw();//密码加密(我没能实现)
  15. ?? ?int exiet(string);//检查联系人是否存在
  16. ?
  17. private:
  18. ?
  19. ?? ?MailList mail[Max];//数组,MailList类型,这属于实例化对象
  20. ?? ?int len;//计数器
  21. ?
  22. };
  23. MailListManager::MailListManager()//构造函数就是用来初始化函数成员的,未初始化的函数成员不可用。这里初始化一下计数器
  24. {
  25. ?? ?len = 0;
  26. }
  27. int MailListManager::exiet(string name)//定义检查函数,检查联系人是否存在,以姓名的匹配为条件
  28. {
  29. ?
  30. }
  31. void MailListManager::loading()//定义加载函数
  32. {
  33. ?
  34. }
  35. void MailListManager::save()//定义保存函数
  36. {
  37. ?
  38. }
  39. void MailListManager::initList()//定义初始化函数
  40. {
  41. ?
  42. }
  43. void MailListManager::insertList()//定义添加函数
  44. {
  45. ?
  46. }
  47. void MailListManager::showList()//定义显示函数
  48. {
  49. ?
  50. }
  51. void MailListManager::updateList()//定义修改函数
  52. {
  53. ?
  54. }
  55. void MailListManager::deleteList()//定义删除函数
  56. {
  57. ?
  58. }
  59. void MailListManager::selectList()//定义查找函数
  60. {
  61. ?
  62. }
  63. void MailListManager::dropList()//定义清空函数
  64. {
  65. ?
  66. }

总结构搭建好后,再开始编写里面的定义内容。

  1. //这里声明部分就不显示了,直接看定义
  2. ?
  3. void MailListManager::loading()//加载函数的定义
  4. {
  5. ?? ?len = 0;//计数器len,每次调用加载函数的时候都要重新初始化为0,这样做是防止之后添加联系人时重复加载导致保存多次。
  6. ?? ?string name, sex, age, tel, address;//局部变量,每次使用都要声明一下的
  7. ?? ?ifstream in;//实例化文件类“fstream”对象
  8. ?? ?in.open("maillist/mail.txt");//打开文件
  9. ?? ?if (!in)//如果文件未打开
  10. ?? ?{
  11. ?
  12. ?? ??? ?cout << "--文件打开失败--" << endl;
  13. ?? ??? ?system("pause");
  14. ?? ?}
  15. ?? ?else
  16. ?? ?{
  17. ?? ??? ?while (!in.eof())//如果未达到文件末尾
  18. ?? ??? ?{
  19. ?? ??? ??? ?in >> name >> age >> sex >> tel >> address;
  20. ?? ??? ??? ?if (in.fail())break;//ifstream类的作用是从文件中读出数据到控制台上,但没有显示出来,这就相当于再次赋值给数组,就是给之前声明的MailList类的数组,所以这里使用数组mail[*].***保存数据,因为不知道到底有多少数据,所以用死循环来控制,跳出条件是达到文件末尾就跳出,这样就可以保证将文件中的内容全部存到数组里
  21. ?? ??? ??? ?mail[len].setName(name);
  22. ?? ??? ??? ?mail[len].setAge(age);
  23. ?? ??? ??? ?mail[len].setSex(sex);
  24. ?? ??? ??? ?mail[len].setTel(tel);
  25. ?? ??? ??? ?mail[len].setAddress(address);
  26. ?? ??? ??? ?len++;
  27. ?? ??? ?}
  28. ?? ?}
  29. ?? ?in.close();
  30. }
  31. int MailListManager::exiet(string name)//检验联系人是否存在
  32. {
  33. ?? ?for (int i = 0; i < len; i++)//每当调用检验联系人的函数之前,一定要加载一下,让数据全部保存到控制台的数组中,且要从控制台输入一个名字传给检验联系人函数,让传入的名字与数组中的数据逐一对比,从而检验联系人是否存在
  34. ?? ?{
  35. ?? ??? ?if (mail[i].getName() == name)
  36. ?? ??? ?{
  37. ?? ??? ??? ?return i;//如果存在,返回数组下标
  38. ?? ??? ?}
  39. ?? ?}
  40. ?? ?return -1;//不存在,返回-1
  41. }
  42. void MailListManager::save()//保存文件函数
  43. {
  44. ?? ?ofstream out;//ofstream类的作用是把控制台上的数据写入文件
  45. ?? ?out.open("maillist/mail.txt");//打开文件
  46. ?? ?if (!out)//如果文件未打开
  47. ?? ?{
  48. ?? ??? ?cout << "--文件打开失败--" << endl;
  49. ?? ?}
  50. ?? ?else
  51. ?? ?{
  52. ?? ??? ?for (int i = 0; i < len; i++)//这里的计数器len的值来自之后定义的函数中,len的值取决于谁调用的保存函数
  53. ?? ??? ?{
  54. ?? ??? ??? ?out << mail[i].getName() << " " << mail[i].getAge() << " " << mail[i].getSex() << " " << mail[i].getTel() << " " << mail[i].getAddress() << endl;
  55. ?? ??? ?}
  56. ?? ?}
  57. ?
  58. ?
  59. ?? ?out.close();
  60. }
  61. ?
  62. ?
  63. void MailListManager::initList()//初始化联系人。之前我先写了添加,修改,删除,在解决一些问题的时候发现添加功能调用加载函数与删除修改功能调用加载函数有冲突,导致重复显示,所以在老师的帮助下加入了这个初始化联系人功能,当然有更好的方法,只是我暂时还不会用(TvT)
  64. {
  65. ?? ?string name, age, sex, tel, address;
  66. ?? ?cout << "请输入联系人的信息(在姓名后输入stop停止输入):" << endl;
  67. ?? ?while (true)
  68. ?? ?{
  69. ?? ??? ?cout << "姓名:";
  70. ?? ??? ?cin >> name;
  71. ?? ??? ?if (name == "stop") break;
  72. ?? ??? ?else
  73. ?? ??? ?{
  74. ?? ??? ??? ?mail[len].setName(name);//简单的调用函数,不想讲了
  75. ?? ??? ??? ?cout << "年龄:";
  76. ?? ??? ??? ?cin >> age;
  77. ?? ??? ??? ?mail[len].setAge(age);
  78. ?? ??? ??? ?cout << "性别:";
  79. ?? ??? ??? ?cin >> sex;
  80. ?? ??? ??? ?mail[len].setSex(sex);
  81. ?? ??? ??? ?cout << "电话号码:";
  82. ?? ??? ??? ?cin >> tel;
  83. ?? ??? ??? ?mail[len].setTel(tel);
  84. ?? ??? ??? ?cout << "家庭地址:";
  85. ?? ??? ??? ?cin >> address;
  86. ?? ??? ??? ?mail[len].setAddress(address);
  87. ?? ??? ??? ?len++;//这个len最终的值会给到save.....emmm..说“给”好像不太准确,找不到合适的词了,反正代码顺着往下执,len加到最后不会再变了,save直接用它。
  88. ?? ??? ?}
  89. ?? ?}
  90. ?? ?save();
  91. ?
  92. }
  93. void MailListManager::insertList()//添加联系人函数
  94. {
  95. ?? ?this->loading();//加载一下初始化联系人的数据
  96. ?? ?string name, age, sex, tel, address;//局部变量要重新声明
  97. ?? ?cout << "请输入插入联系人的数量:" << endl;
  98. ?? ?int count = 0;//再来个计数器,控制每次想要添加的联系人的数量
  99. ?? ?cin >> count;//让用户来指定每次添加多少人
  100. ?? ?for (int i = 0; i < count; i++)
  101. ?? ?{
  102. ?? ??? ?cout << "姓名:";
  103. ?? ??? ?cin >> name;
  104. ?? ??? ?mail[i].setName(name);//函数调用过程,还是说一下吧。mail[]数组的类型是MailList,然后它是MailListManager这个类的函数成员,通过它作为桥梁来调用MailListManager类成员函数
  105. ?? ??? ?cout << "年龄:";
  106. ?? ??? ?cin >> age;
  107. ?? ??? ?mail[i].setAge(age);
  108. ?? ??? ?cout << "性别:";
  109. ?? ??? ?cin >> sex;
  110. ?? ??? ?mail[i].setSex(sex);
  111. ?? ??? ?cout << "电话号码:";
  112. ?? ??? ?cin >> tel;
  113. ?? ??? ?mail[i].setTel(tel);
  114. ?? ??? ?cout << "家庭地址:";
  115. ?? ??? ?cin >> address;
  116. ?? ??? ?mail[i].setAddress(address);
  117. ?? ?}
  118. ?? ?ofstream out;//添加有单独的保存文件定义,因为只有添加功能需要使用文件追加
  119. ?? ?out.open("maillist/mail.txt", ios::app);
  120. ?? ?if (!out)
  121. ?? ?{
  122. ?? ??? ?cout << "--文件打开失败--" << endl;
  123. ?? ?}
  124. ?? ?else
  125. ?? ?{
  126. ?? ??? ?for (int i = 0; i < count; i++)
  127. ?? ??? ?{
  128. ?? ??? ??? ?out << mail[i].getName() << " " << mail[i].getAge() << " " << mail[i].getSex() << " " << mail[i].getTel() << " " << mail[i].getAddress() << endl;
  129. ?? ??? ?}
  130. ?? ??? ?out.close();
  131. ?? ?}
  132. }
  133. void MailListManager::showList()//显示联系人
  134. {
  135. ?
  136. ?? ?loading();
  137. ?? ?MailList temp;
  138. ?? ?for (int j=0;j<len;j++)//这里是给联系人排序,通过名字排序
  139. ?? ?{
  140. ?? ??? ?for (int i = j+1; i < len; i++)
  141. ?? ??? ?{
  142. ?? ??? ??? ?if (mail[j].getName() > mail[i].getName())
  143. ?? ??? ??? ?{
  144. ?? ??? ??? ??? ?temp = mail[i];
  145. ?? ??? ??? ??? ?mail[i] = mail[j];//交换
  146. ?? ??? ??? ??? ?mail[j] = temp;?? ?
  147. ?? ??? ??? ?}
  148. ?? ??? ?}
  149. ?? ??? ?
  150. ?? ?}
  151. ?? ?for (int i = 0; i < len; i++)//这里的len值来自loading()
  152. ?? ?{
  153. ?? ??? ?cout << setw(10) << mail[i].getName() << " ?" << setw(8) << mail[i].getAge() << " ?" << setw(4) <<
  154. ?? ??? ??? ?mail[i].getSex() << " ?" << setw(15) << mail[i].getTel() << " ?" << setw(20) << mail[i].getAddress();//setw()是格式控制函数
  155. ?? ??? ?cout << endl << endl;
  156. ?? ?}
  157. }
  158. void MailListManager::updateList()//修改联系人,修改联系人之前要找到这个联系人,存在才能删除
  159. {
  160. ?? ?loading();//加载一下
  161. ?? ?string name, age, sex, tel, address;
  162. ?? ?cout << "请输入要修改的联系人姓名:";
  163. ?? ?cin >> name;
  164. ?? ?int ret = exiet(name);//检验一下是否存在,存在exiet()会返回该联系人所在的数组下标,在这个数组下标里重新输入一遍数据覆盖掉原有数据就是修改联系人了
  165. ?? ?if (ret != -1)
  166. ?? ?{
  167. ?? ??? ?cout << "请重新输入联系人信息:" << endl;
  168. ?? ??? ?cout << "姓名:";
  169. ?? ??? ?cin >> name;
  170. ?? ??? ?mail[ret].setName(name);
  171. ?? ??? ?cout << "年龄:";
  172. ?? ??? ?cin >> age;
  173. ?? ??? ?mail[ret].setAge(age);
  174. ?? ??? ?cout << "性别:";
  175. ?? ??? ?cin >> sex;
  176. ?? ??? ?mail[ret].setSex(sex);
  177. ?? ??? ?cout << "电话号码:";
  178. ?? ??? ?cin >> tel;
  179. ?? ??? ?mail[ret].setTel(tel);
  180. ?? ??? ?cout << "家庭地址:";
  181. ?? ??? ?cin >> address;
  182. ?? ??? ?mail[ret].setAddress(address);
  183. ?? ?}
  184. ?? ?else
  185. ?? ??? ?cout << "啊哦~联系人不存在喔(-o-)";
  186. ?? ?save();//改完记得重新保存一下,不然是没有任何改动的哦
  187. }
  188. void MailListManager::deleteList()//删除联系人,理同修改一样,只不过是信息的覆盖变为了内容前移覆盖
  189. {
  190. ?? ?loading();
  191. ?? ?string name;
  192. ?? ?int o;
  193. ?? ?cout << "请输入要删除的联系人的姓名: ?";
  194. ?? ?cin >> name;
  195. ?? ?int ret = exiet(name);
  196. ?? ?if (ret == -1)
  197. ?? ?{
  198. ?? ??? ?cout << "啊哦~联系人不存在喔(-o-)";
  199. ?? ?}
  200. ?? ?else
  201. ?? ?{
  202. ?? ??? ?cout << "确定要删除吗?" << endl << "1.确定" << " ? ?" << "2.我再想想" << endl << "请选择:";
  203. ?? ??? ?cin >> o;
  204. ?? ??? ?if (o == 2)
  205. ?? ??? ??? ?cout << "好的~";
  206. ?? ??? ?else
  207. ?? ??? ?{
  208. ?
  209. ?? ??? ??? ?for (int i = ret; i < len; i++)
  210. ?? ??? ??? ?{
  211. ?? ??? ??? ??? ?mail[i] = mail[i + 1];
  212. ?? ??? ??? ?}
  213. ?
  214. ?
  215. ?? ??? ??? ?cout << "删除成功!";
  216. ?? ??? ?}
  217. ?? ?}
  218. ?? ?save();
  219. ?
  220. }
  221. void MailListManager::selectList()//查找联系人,查找联系人就更简单啦,调用一下检验存在的函数,然后根据下标直接输出此联系人信息就好
  222. {
  223. ?? ?loading();
  224. ?? ?string name;
  225. ?? ?cout << "请输入要查找的人的姓名: ? ";
  226. ?? ?cin >> name;
  227. ?? ?int ret = exiet(name);
  228. ?? ?if (ret != -1)
  229. ?? ?{
  230. ?? ??? ?cout << "姓名:" << mail[ret].getName() << endl;
  231. ?? ??? ?cout << "年龄:" << mail[ret].getAge() << endl;
  232. ?? ??? ?cout << "性别:" << mail[ret].getSex() << endl;
  233. ?? ??? ?cout << "电话号码:" << mail[ret].getTel() << endl;
  234. ?? ??? ?cout << "家庭地址:" << mail[ret].getAddress() << endl;
  235. ?? ?}
  236. ?? ?else
  237. ?? ??? ?cout << "啊哦~联系人不存在喔(-o-)";
  238. }
  239. void MailListManager::dropList()//清空通讯录,重新写入文件,写入一个空字符覆盖之前的数据,就清空啦
  240. {
  241. ?? ?int n;
  242. ?? ?cout << "确定清空吗?" << endl << " ?" << "1.YES" << " ? " << "2.NO" << "请选择:";
  243. ?? ?cin >> n;
  244. ?? ?if (n == 2)
  245. ?? ?{
  246. ?? ??? ?cout << "好的~";
  247. ?? ?}
  248. ?? ?else
  249. ?? ?{
  250. ?? ??? ?ofstream out;
  251. ?? ??? ?out.open("maillist/mail.txt");
  252. ?? ??? ?if (!out)
  253. ?? ??? ?{
  254. ?? ??? ??? ?cout << "--文件打开失败--" << endl;
  255. ?? ??? ?}
  256. ?? ??? ?else
  257. ?? ??? ?{
  258. ?? ??? ??? ?out << " ";
  259. ?? ??? ??? ?cout << "清除成功" << endl;
  260. ?? ??? ?}
  261. ?? ??? ?out.close();
  262. ?? ?}
  263. }
  264. string MailListManager::pw()//密码加密,未完成
  265. {
  266. ?? ?char psw[100] = { 0 }, c;
  267. ?? ?int i = 0;
  268. /*?? ?cin >> c*/;
  269. ?? ?while ((c = getch()) != '\r')
  270. ?? ?{
  271. ?? ??? ?if (c != '\b')
  272. ?? ??? ?{
  273. ?? ??? ??? ?cout << "*";
  274. ?? ??? ??? ?psw[i++];
  275. ?? ??? ?}
  276. ?? ??? ?else
  277. ?? ??? ?{
  278. ?? ??? ??? ?cout << "\b \b";
  279. ?? ??? ??? ?i--;
  280. ?? ??? ?}
  281. ?? ?}
  282. ?? ?psw[i] = '\0';
  283. ?? ?cout << psw;
  284. ? ??
  285. }

然后再写个登陆类

  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. using namespace std;
  5. class User
  6. {
  7. public:
  8. ?? ?User();
  9. ?? ?void loading();
  10. ?? ?int check(string);
  11. ?? ?int login(string, string);
  12. ?? ?void sign(string,string);
  13. private:
  14. ?? ?string s[50];
  15. ?? ?int len;
  16. ?
  17. };
  18. User::User()
  19. {
  20. ?? ?len = 0;
  21. }
  22. ?
  23. void User::loading()//加载函数
  24. {
  25. ?? ?string name, password;
  26. ?? ?ifstream in;
  27. ?? ?in.open("User/user.txt");
  28. ?? ?if (!in)
  29. ?? ?{
  30. ?? ??? ?cout << "错误!";
  31. ?? ?}
  32. ?? ?else
  33. ?? ?{
  34. ?? ??? ?while (!in.eof())
  35. ?? ??? ?{
  36. ?? ??? ??? ?in >> s[len];
  37. ?? ??? ??? ?if (in.fail())break;
  38. ?? ??? ??? ?len++;
  39. ?? ??? ?}
  40. ?
  41. ?? ?}
  42. ?? ?in.close();
  43. }
  44. int User::check(string name)//检验函数
  45. {
  46. ?? ?loading();
  47. ?? ?for (int i = 0; i < len; i=i+2)
  48. ?? ?{
  49. ?? ??? ?if (name == s[i])
  50. ?? ??? ?{
  51. ?? ??? ??? ?return i;
  52. ?? ??? ?}
  53. ?? ??? ?else
  54. ?? ??? ?{
  55. ?? ??? ??? ?return -1;
  56. ?? ??? ?}
  57. ?? ?}
  58. }
  59. int User::login(string name, string password)//登陆函数
  60. {
  61. ?? ?if (check(name) == -1)
  62. ?? ?{
  63. ?? ??? ?cout << "用户根本不存在喔!";
  64. ?? ?}
  65. ?? ?else
  66. ?? ?{
  67. ?? ??? ?if (s[check(name) + 1] == password)
  68. ?? ??? ?{
  69. ?? ??? ??? ?return 1;
  70. ?? ??? ?}
  71. ?? ??? ?else
  72. ?? ??? ?{
  73. ?? ??? ??? ?cout << "密码输入错误!";
  74. ?? ??? ?}
  75. ?? ?}
  76. }
  77. void User::sign(string name,string password)//注册函数
  78. {
  79. ?? ?loading();
  80. ?? ?if (check(name) != -1)//已有用户名
  81. ?? ?{
  82. ?? ??? ?cout << "用户名已存在!";
  83. ?? ?}
  84. ?? ?else
  85. ?? ?{
  86. ?? ??? ?ofstream out;
  87. ?? ??? ?out.open("User/user.txt", ios::app);
  88. ?? ??? ?if (!out)
  89. ?? ??? ?{
  90. ?? ??? ??? ?cout << "文件打开失败!";
  91. ?? ??? ?}
  92. ?? ??? ?else
  93. ?? ??? ?{
  94. ?? ??? ??? ?out <<endl<< name<<" "<< password;
  95. ?? ??? ?}
  96. ?? ??? ?out.close();
  97. ?? ??? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?注册成功!";
  98. ?? ??? ?system("pause");
  99. ?? ?}
  100. }

登陆类的加载函数,检验函数逻辑同之前管理类的一样,登陆函数与注册函数,实际上就是文件读出与写入。

最后,写主函数

  1. #include"MailListManager.hpp"
  2. #include"userl.hpp"
  3. void menu()
  4. {
  5. ?? ?cout << endl << endl << endl ;
  6. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-----------(^O^)---------" << endl;
  7. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?- ? ? 1、初始化联系人 ? -" << endl;
  8. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?- ? ? 2、显示联系人 ? ? -" << endl;
  9. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?- ? ? 3、删除联系人 ? ? -" << endl;
  10. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?- ? ? 4、查找联系人 ? ? -" << endl;
  11. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?- ? ? 5、修改联系人 ? ? -" << endl;
  12. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?- ? ? 6、清空联系人 ? ? -" << endl;
  13. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?- ? ? 7、添加联系人 ? ? -" << endl;
  14. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?- ? ? 0、退出通讯录 ? ? -" << endl;
  15. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?---------(·v·)---------" << endl;
  16. }//手动格式控制O(∩_∩)O哈哈~
  17. int main()
  18. {
  19. ?? ?User u;//实例化对象
  20. ?? ?int e;
  21. ?? ?cout << endl << endl << endl << endl << endl << endl;
  22. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ?******************欢迎使用通讯录管理系统~******************" << endl;
  23. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ?* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *" << endl;
  24. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ?* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *" << endl;
  25. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ?* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *" << endl;
  26. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ?********************** ? ? ? ? ? ? ?***********************" << endl;
  27. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ?**1.我已有账户,登录**" <<" ? ? ? ? ? ? ?"<< "** 2.我没有账户,注册**" << endl;
  28. ?? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ?********************** ? ? ? ? ? ? ?***********************" << endl;//这是一个登陆界面,有账户才能管理通讯录哦
  29. ?? ?cout << endl<<endl;
  30. ?? ?cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 请选择:";
  31. ?? ?cin >> e;
  32. ?? ?if (e == 1)
  33. ?? ?{
  34. ?? ??? ?while (true)
  35. ?? ??? ?{
  36. ?? ??? ??? ?MailListManager* m = new MailListManager;//来个指针指向堆区
  37. ?? ??? ??? ?system("cls");
  38. ?? ??? ??? ?string name, password;
  39. ?? ??? ??? ?cout <<endl<<endl<< " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?请输入:" << endl << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?用户名:";
  40. ?? ??? ??? ?cin >> name;
  41. ?? ??? ??? ?cout << endl << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?密码:";
  42. ?? ??? ??? ?cin >> password;
  43. ?? ??? ??? ?/*password = getch();*/ ?//这两段本来是用来密码加密的,但我没实现所以注释掉了
  44. ?? ??? ??? ?/*m->pw();*/
  45. ?? ??? ??? ?/*password = putch(getch());*/
  46. ?? ??? ??? ?int i = u.login(name, password);//调用User里的登陆函数,并将返回值给i,i=1就证明用户名密码输入正确,反之就是输入错误
  47. ?? ??? ??? ?if (i == 1)
  48. ?? ??? ??? ?{
  49. ?? ??? ??? ??? ?int n;
  50. ?? ??? ??? ??? ?while (true)
  51. ?? ??? ??? ??? ?{
  52. ?? ??? ??? ??? ??? ?system("cls");
  53. ?? ??? ??? ??? ??? ?cout <<endl<<endl<< " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?登录成功!欢迎" << name << endl;
  54. ?? ??? ??? ??? ??? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?通讯录管理系统功能选项" << endl;
  55. ?? ??? ??? ??? ??? ?menu();
  56. ?? ??? ??? ??? ??? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?请选择:";
  57. ?? ??? ??? ??? ??? ?cin >> n;
  58. ?? ??? ??? ??? ??? ?switch (n)
  59. ?? ??? ??? ??? ??? ?{
  60. ?? ??? ??? ??? ??? ?case 7:system("cls"); m->insertList(); system("pause"); break;
  61. ?? ??? ??? ??? ??? ?case 2:system("cls"); m->showList(); system("pause"); break;
  62. ?? ??? ??? ??? ??? ?case 3:system("cls"); m->deleteList(); ?system("pause"); break;
  63. ?? ??? ??? ??? ??? ?case 4:system("cls"); m->selectList(); system("pause"); break;
  64. ?? ??? ??? ??? ??? ?case 5:system("cls"); m->updateList(); system("pause"); break;
  65. ?? ??? ??? ??? ??? ?case 6:system("cls"); m->dropList(); system("pause"); break;
  66. ?? ??? ??? ??? ??? ?case 1:system("cls"); m->initList(); system("pause"); break;//调用各种函数
  67. ?? ??? ??? ??? ??? ?case 0:exit(0); break;
  68. ?? ??? ??? ??? ??? ?}
  69. ?? ??? ??? ??? ?}
  70. ?? ??? ??? ??? ?delete m;
  71. ?? ??? ??? ??? ?m = NULL;
  72. ?? ??? ??? ?}
  73. ?? ??? ??? ?else
  74. ?? ??? ??? ?{
  75. ?? ??? ??? ??? ?cout<<endl<<endl << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?请重新输入" << endl;
  76. ?? ??? ??? ??? ?system("pause");
  77. ?? ??? ??? ?}
  78. ?? ??? ?}
  79. ?
  80. ?? ?}
  81. ?? ?else
  82. ?? ?{
  83. ?? ??? ?u.loading();//调用User类里的加载函数
  84. ?? ??? ?while (true)
  85. ?? ??? ?{
  86. ?? ??? ??? ?system("cls");
  87. ?? ??? ??? ?string name, password;
  88. ?? ??? ??? ?cout <<endl<<endl<< " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?请输入:" << endl << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?用户名:";
  89. ?? ??? ??? ?cin >> name;
  90. ?? ??? ??? ?if (u.check(name) !=-1)
  91. ?? ??? ??? ?{
  92. ?? ??? ??? ??? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?用户名已存在!请重新输入:"<<endl;
  93. ?? ??? ??? ??? ?system("pause");
  94. ?? ??? ??? ?}
  95. ?? ??? ??? ?else
  96. ?? ??? ??? ?{
  97. ?? ??? ??? ??? ?MailListManager* m = new MailListManager;
  98. ?? ??? ??? ??? ?cout << endl << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?密码:";
  99. ?? ??? ??? ??? ?cin >> password;
  100. ?? ??? ??? ??? ?/*m->pw();*/
  101. ?? ??? ??? ??? ?u.sign(name, password);//调用User类的注册函数,保存注册用户信息
  102. ?? ??? ??? ??? ?int n;
  103. ?? ??? ??? ??? ?while (true)
  104. ?? ??? ??? ??? ?{
  105. ?? ??? ??? ??? ??? ?system("cls");
  106. ?? ??? ??? ??? ??? ?cout <<endl<<endl<< " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?注册成功!欢迎" << name << endl;
  107. ?? ??? ??? ??? ??? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?通讯录管理系统功能选项" << endl;
  108. ?? ??? ??? ??? ??? ?menu();
  109. ?? ??? ??? ??? ??? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?请选择:";
  110. ?? ??? ??? ??? ??? ?cin >> n;
  111. ?? ??? ??? ??? ??? ?switch (n)
  112. ?? ??? ??? ??? ??? ?{
  113. ?? ??? ??? ??? ??? ?case 1:system("cls"); m->insertList(); system("pause"); break;
  114. ?? ??? ??? ??? ??? ?case 2:system("cls"); m->showList(); system("pause"); break;
  115. ?? ??? ??? ??? ??? ?case 3:system("cls"); m->deleteList(); ?system("pause"); break;
  116. ?? ??? ??? ??? ??? ?case 4:system("cls"); m->selectList(); system("pause"); break;
  117. ?? ??? ??? ??? ??? ?case 5:system("cls"); m->updateList(); system("pause"); break;
  118. ?? ??? ??? ??? ??? ?case 6:system("cls"); m->dropList(); system("pause"); break;
  119. ?? ??? ??? ??? ??? ?case 7:system("cls"); m->initList(); system("pause"); break;//调用各种函数
  120. ?? ??? ??? ??? ??? ?case 0: exit(0); break;
  121. ?? ??? ??? ??? ??? ?}
  122. ?? ??? ??? ??? ?}
  123. ?? ??? ??? ?}
  124. ?? ??? ?}
  125. ?? ?}
  126. ?? ?return 0;
  127. }

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号