数据结构课程设计-通讯录管理系统的设计与实现
来源:互联网 发布:linux查看dns配置 编辑:程序博客网 时间:2024/04/29 16:38
本人第一篇博客,代码写的很乱,还有很多BUG没有解决,先贴出来,以后再更改。这是一篇大学数据结构的实验源代码。用顺序表实现通讯录的。包含以下功能://1) 创建通讯录//2) 插入联系人//3) 查找联系人//4) 修改联系人//5) 保存通讯录//6) 读取通讯录//7) 删除联系人//8) 排序联系人Contact.cpp#include<iostream>#include<fstream>#include"process.h"#include"Contact.h"#include<string>#include<cstring>using namespace std;char pause;typedef string T;int main(){ int i; T e; Contact<string>L(100); system("cls"); int choice; do { cout<<"*******请选择操作********\n"; cout<<"* 1-创建联系表 *\n"; cout<<"* 2-在联系表的第i个位置插入联系人 *\n"; cout<<"* 3-删除第i个位置的联系人 *\n"; cout<<"* 4-返回第i联系人的信息 *\n"; cout<<"* 5-查找联系人 *\n"; cout<<"* 6-显示通讯簿 *\n"; cout<<"* 7-保存通讯录为Contact.txt *\n"; cout<<"* 8-修改通讯录 *\n"; cout<<"* 9-读取通讯录文件 *\n"; cout<<"*10-排序 *\n"; cout<<"*11-退出 *\n"; cout<<"********************\n"; cout<<"请输入选择_\b"; cin>>choice; switch(choice) { case 1:cout<<"请输入要创建的联系人的个数:_\b"; cin>>i; cin.get(); try { L.CreateCon(i); } catch(char *err) { cout<<err<<endl; } break; case 2: cout<<"请输入插入位置"; cout<<endl; try { L.Insert(); } catch(char * err ) { cout<<err<<endl; } break; case 3: cout<<"请输入删除位置:"; cin>>i; cout<<endl; try { L.Delete(i); } catch(char *err) { cout<<err<<endl; cin.get(pause); system("pause"); } break; case 4: cout<<"请输入要查询的联系人的元素位置:";cin>>i; try{ L.GetCon(i);} catch(char *err) { cout<<err<<endl; } cin.get(pause); system("pause"); break; case 5: cout<<"请输入查找联系人:"; cin>>e; cin.get(); i=L.Locate(e);if(i) { cout<<"要查询的联系人为:"<<e<<"位于表中的位置为:"<<i<<endl;} else cout<<"对不起,你的通讯录没有此联系人"; cin.get(pause); system("pause"); break; case 6: cout<<"*****通讯录******"<<endl; L.ConDisp(); break; case 7: cout<<"将通讯录保存为Contact.txt"<<endl; L.Conout(); break; case 8: cout<<"请输入要修改的联系人序号___\b\b\b"; cin>>i; L.Fix(i); break; case 9: L.Conin(); break; case 10: L.Sort(); break; case 11: break; default: cout<<"Invalid choice\n"; break; } }while(choice!=11);return 0;}
Contact.h
template<class T>class Contact{private:T * peo;char name[50][50];int length;int listsize;public:Contact(int m); //构造函数,创建容量为m的空表~Contact(); //析构函数,删除表空间void CreateCon(int n); //创建具有n个联系人的通讯录void Insert(); //在第i位置插入联系人void Save(); //保存联系人void Sort(); //排序void Delete(int i); //删除第i个联系人void GetCon(int i); //获取第i个联系人int Locate(T e); //联系人定位void Clear(); //清空表int Length(); //测表长void ConDisp(); //输出表元素void Conout(); //保存通讯录void Fix(int i); //修改通讯录void Conin(); //读取外部文件};template<class T>Contact<T>::Contact(int m){peo=new T[m];length=0;listsize=m;}template<class T>Contact<T>::~Contact(){delete [] peo;length=0;listsize=0;}template<class T>void Contact<T>::CreateCon(int n){if(n>listsize) throw"参数非法";cout<<"请依次输入"<<n<<"个联系人:"<<endl;for(int i=1;i<=n;i++){cin>>name[i-1];cin>>peo[i-1];}length=n;}template<class T>void Contact<T>::Insert(){int i;cin>>i;cin.get();if(length>=listsize) throw"上溢";if(i<1||i>length+1) throw"插入位置异常";cout<<"输入插入元素的值:";for(int j=length;j>=i;j--){strcpy(name[j],name[j-1]);peo[j]=peo[j-1];}cin>>name[i-1];cin>>peo[i-1];length++;}template<class T>void Contact<T>::Delete(int i){if(length==0) throw"下溢";if(i<1||i>length+1) throw "删除位置异常";cout<<"删除元素为"<<name[i-1]<<" "<<peo[i-1];for(int j=i;j<length;j++){strcpy(name[j-1],name[j]);peo[j-1]=peo[j];}length--;}template<class T>int Contact<T>::Locate(T e){T cmp;for(int i=0;i<length;i++)if(name[i]==e) return i+1;return 0;}template<class T>void Contact<T>::Clear(){length=0;}template<class T>void Contact<T>::GetCon(int i){T e;if(i<1||i>length) throw "位置不合法";cout<<name[i-1]<<" "<<peo[i-1];}template<class T>void Contact<T>::ConDisp(){for(int i=0;i<length;i++){cout<<i+1<<" ";cout<<name[i]<<" "<<peo[i]<<endl;}}template<class T>void Contact<T>::Conout(){ofstream outFile;outFile.open("Contact.txt");for(int i=0;i<length;i++){outFile<<i+1<<" ";outFile<<name[i]<<" "<<peo[i]<<endl;}}template<class T>void Contact<T>::Fix(int i){cout<<"请输入新的联系人信息\n";cin>>name[i-1];cin>>peo[i-1];}template <class T>void Contact<T>::Conin(){ifstream inFile;inFile.open("Contact.txt");while(inFile.good()){inFile>>name[length];inFile>>peo[length];length++;}length--;}template<class T>void Contact<T>::Sort(){int i;char z[50];T t;cout<<"0--姓名--\n";cout<<"1--号码--\n";cin>>i;if(i){for(int i=1;i<length;i++){for(int j=0;j<length-1;j++){if(peo[j]>peo[j+1]){t=peo[j];peo[j]=peo[j+1];peo[j+1]=t;strcpy(z,name[j]);strcpy(name[j],name[j+1]);strcpy(name[j+1],z);}}}}if(i==0){for(int i=1;i<length;i++){for(int j=0;j<length-1;j++){if(*(name[j])>*(name[j+1])){strcpy(z,name[j]);strcpy(name[j],name[j+1]);strcpy(name[j+1],z);t=peo[j];peo[j]=peo[j+1];peo[j+1]=t;}}}}}
0 0
- 数据结构课程设计-通讯录管理系统的设计与实现
- 数据结构课程设计 - 通讯录管理系统
- 数据结构课程设计之通讯录管理系统
- 数据结构实践课程设计【通讯录管理系统】
- 数据结构课程设计——通讯录管理系统
- 数据结构课程设计—通讯录管理系统
- 数据结构课程设计—通讯录管理系统
- 数据结构课程设计—通讯录管理系统
- 数据结构课程设计—通讯录管理系统
- 数据结构课程设计—通讯录管理系统
- 【课程设计-通讯录管理系统】
- 通讯录管理系统设计与实现
- 个人通讯录管理系统的设计与实现
- 课程设计:银行系统的设计与实现
- 数据结构课程设计——通讯录系统设计(C语言)
- 数据结构课程设计--通讯录管理(C语言实现)
- 个人帐簿管理系统设计[数据结构课程设计]
- 【数据结构】通讯录查询系统的设计与实现(散列表(哈希表))
- C语言中 scanf 和 printf 的小要点
- label 文字与input 垂直居中对齐问题
- 内置应用程序(apk或源码)
- gtk GtkTextView 使用尝试
- A Simple In-Place Algorithm for In-Shuffle 一种简单的原地洗牌算法
- 数据结构课程设计-通讯录管理系统的设计与实现
- linux内核中container_of
- 装饰器-decorator
- [ linux ] opensuse配置自启动服务!
- 使用Beaglebone Black的IO口
- 代码复用应该这样做(2)
- python所有库文件打zip包,尽量减少文件个数
- HDU 1022(栈模拟;暴力搜索)
- 源码中编译生成完整apk