c++写的通讯录 (2.0)
来源:互联网 发布:银联钱包 网络不给力 编辑:程序博客网 时间:2024/06/16 05:12
/*这次终于真切体会到c++封装的利与弊了150行的程序被改到了300行,汗。。。这次修改添加了分组功能添加了对输入合法性的检查去掉了修改功能,因为目前只做到,等同于删除后再新建去掉了map,因为用了vector,下标在不断变化,不用map后,省掉了不停维护下标这一步骤。当然搜索就直接暴力了这次添加了Group类,adresslist类和Menu类,整个程序还是有一些模块化了未完成的功能:1.名字按字典序输出2.修该联系人或小组单独某项的信息3.小组功能中,显示小组的内容,并未设计完全4.文件记录*/#include <cstdio>#include <string>#include <iostream>#include <vector>using namespace std;void Error(){ cout << "\n输入错误,请重新输入" << endl;}void Checkerror(string &s,int maxn){ while(1){ cin >> s; if(s.length() > 1 || s[0] < '0' || s[0] - '0' > maxn) Error(); else return; }}class Group{ private: string name;//小组的名字 vector <string> v;//储存小组成员的名字 public: void assign(string s){//创建时名字赋值 v.clear(); name = s; } void push(string s){//向该组中加入成员 v.push_back(s); } void showname(){//输出该组的名字 cout << name << endl; } string getname(){ return name; } void print(){//输出该组所有成员的名字 if(v.size() == 0){ cout << "None" << endl; return; } for(int i = 0;i < v.size();i++) cout << i << ". " << v[i] << endl; } void clear(){//删除该小组中所有成员 v.clear(); } void cut(string s){//从该组中删除某个成员 for(int i = 0;i < v.size();i++) if(v[i] == s) v.erase(v.begin()+i); }};class Contact_person{ private: char First;//该联系人姓名的首字母 string name,tel,remark,group;//remark为备注,grouop为所在组名 public: Contact_person(){group = "None";}//group初始为无 void assign(string n,string t,string r){//创建时赋值 First = n[0]; name = n; tel = t; remark = r; } void divide_into_group(string s){//修改group的值 group = s; } string getname(){//为外部得到name提供接口 return name; } void showname(){//输出联系人的名字 cout << name << endl; } void print(){//输出联系人所有信息 cout << "Name :" << name << endl; cout << "Tel :" << tel << endl; cout << "Remark:" << remark << endl; cout << "Group :" << group << endl; }};class Adresslist{ private: vector <Group> vgroup; vector <Contact_person> vperson; public: void print_groupname(){//输出所有分组的名字 if(vgroup.size() == 0){ cout << "分组为空!" << endl; return; } cout << "\n所有分组:" << endl; for(int i = 0;i < vgroup.size();i++){ printf("%d. ",i); vgroup[i].showname(); } } void print_personname(){//输出所有联系人的名字 if(vperson.size() == 0){ cout << "通讯录为空!" << endl; return; } cout << "\n所有联系人如下:" << endl; for(int i = 0;i < vperson.size();i++){ printf("%d. ",i); vperson[i].showname(); } } void divide_into_group(Contact_person &c){//分组 if(!vgroup.size()){ cout << "\n当前并未创建小组,请创建后再分配" << endl; return; } string s; while(1){ print_groupname(); cout << "\n请输入分到哪个小组(编号):"; cin >> s; if(s.length() > 1 || s[0] < '0' || s[0] - '0' >= vgroup.size()) Error(); else break; } int p = s[0] - '0'; s = vgroup[p].getname(); c.divide_into_group(s); vgroup[p].push(c.getname()); cout << "\n分组成功!" << endl; } void buildperson(){//新建联系人 string name,tel,remark; Contact_person p; cout << "\n请输入联系人姓名:"; cin >> name; cout << "\n请输入电话:"; cin >> tel; cout << "\n请输入备注:"; cin >> remark; p.assign(name,tel,remark); while(1){ cout << "\n是否要对其分组(YES / NO)? "; string s; cin >> s; if(s == "YES"){ divide_into_group(p); break; } if(s == "NO"){ break; } Error(); } vperson.push_back(p); cout << "\n联系人创建成功!" << endl; } void buildgroup(){//新建小组 string name; cout << "\n请输入小组名称:"; cin >> name; Group group; group.assign(name); vgroup.push_back(group); cout << "\n小组创建成功!" << endl; } void managegroup(){//group菜单函数 string s; Checkerror(s,3); switch(s[0]){ case '0': cout << "返回上一级!" << endl; return; case '1': buildgroup(); break; case '2': if(vgroup.size() == 0){ cout << "分组为空!" << endl; return; } for(int i = 0;i < vgroup.size();i++){ cout << "分组: "; vgroup[i].showname(); vgroup[i].print(); cout << endl; } break; case '3': vgroup.clear(); break; } } void managesearch(int t){//search菜单函数 string s; Checkerror(s,1); if(s[0] == '0'){ cout << "返回上一级!" << endl; return; } if(s[0] == '1'){ vperson.erase(vperson.begin()+t); cout << "\n删除成功!" << endl; } } void clear(){//清空通讯录 vperson.clear(); for(int i = 0;i < vgroup.size();i++) vgroup[i].clear(); cout << "\n清空成功!" << endl; } int search(){//查找联系人 cout << "\n请输入您要查找的联系人:"; string s; cin >> s; for(int i = 0;i < vperson.size();i++) if(vperson[i].getname() == s){ cout << "\n查找成功!" << endl; vperson[i].print(); return i; } cout << "抱歉,您要查找的联系人不存在!" << endl; return -1; }};class Menu{ public: void primary(){//主菜单的显示 cout << endl << endl; cout << "1 . 新建联系人" << endl; cout << "2 . 显示所有联系人" << endl; cout << "3 . 管理分组" << endl; cout << "4 . 清空通讯录" << endl; cout << "5 . 查找联系人" << endl; cout << "0 . 退出" << endl ; cout << "\n请输入指令:"; } void group(){//分组中的显示 cout << endl << endl; cout << "1 . 新建分组" << endl; cout << "2 . 显示所有小组" << endl; cout << "3 . 删除所有分组" << endl; cout << "0 . 退出" << endl; cout << "\n请输入指令:"; } void search(){//查找中的显示 cout << endl << endl; cout << "1. 删除该联系人" << endl; cout << "0. 退出" << endl; cout << "\n请输入指令:"; }};void System(){ Menu menu; Adresslist adresslist; while(1){ string s; while(1){ menu.primary(); cin >> s; if(s.length() > 1 || s[0] < '0' || s[0] > '5') Error(); else break; } switch(s[0]){ case '0': cout << "谢谢使用!" << endl; return; case '1': adresslist.buildperson(); break; case '2': adresslist.print_personname(); break; case '3': menu.group(); adresslist.managegroup(); break; case '4': adresslist.clear(); break; case '5': int t = adresslist.search(); if(t != -1){ menu.search(); adresslist.managesearch(t); } break; } }}int main(){ cout << "欢迎使用通讯录管理系统!" << endl; System(); return 0;}
- c语言写的通讯录
- 自己用C写的通讯录
- c++写的通讯录 (2.0)
- python 写的通讯录
- python写的通讯录
- 用C语言写一个简单的通讯录
- Linux c编写用数据库写的通讯录
- 基于二叉排序树写的通讯录
- 用STL写的 通讯录
- 用STL写的 通讯录
- C编写的通讯录
- c语言的通讯录
- (C语言)自写通讯录
- C语言写通讯录 链表版本
- C语言写通讯录 数据库版本
- c语言通讯录2.0
- 帮同学写的一个c语言的期末作业(班级通讯录)
- 一个通讯录代码。提供参考,写的既不像c,又不像c++
- log4j配置
- 从零开始一步步教你:用C++开发一个简单的hadoop分布式计算应用程序
- 在java中equals和“==”的比较
- 特性 property 类别 category 协议 protocol
- Linux tcpdump命令详解
- c++写的通讯录 (2.0)
- 2012年度IT博客大赛10强花落谁家暨圆满落幕
- 传说中的WCF(13):群聊天程序
- javascript JSON详解
- net 简单图表控件 (介绍测试示例使用部分) [c/s桌面应用程序控件] II
- Android Tombstone/Crash的log分析和定位
- 用Delphi发邮件
- LINQ查询表达式--学习linq的资料和笔记(三)
- C库函数—strcpy实现