您的位置:首页 > 理论基础 > 数据结构算法

通讯录——数据结构课设

2015-06-10 09:23 465 查看
      帮同学写了n多课设,觉得蛮简单,没怎么在意,这又花20min帮人写了一个,决定发在博客上,以后有谁要类似的就直接给个链接, ;-) 机智的窝

     任务要求:

     题目描述:通讯录的基本属性包括编号、姓名、性别、住址、联系电话等。要求实现最基本的功能模块如下:

    (1)通讯录的建立;该模块主要完成将数据存储工作。记录可以从文本文件中读入,也可以从键盘逐条输入记                     录。

    (2)通讯录查询;用户可以按照联系人的姓名或电话号码查询。若查到,则显示该记录的信息;否则,显示查找                失败的提示信息。

    (3)通讯录的维护;实现对记录的修改、删除、插入和排序等操作。

    (4)通讯录的输出;实现屏幕显示和将记录信息写入文本文件中

 

     功能要求及说明:

     (1)使用菜单选择操作,具有友好的人机交互提示和显示,方便用户输入及查看程序运行过程、结果;

     (2)程序可以根据用户的选择多次运行,直到用户选择退出;

     (3)对于运行解决问题的步骤(例如从键盘输入的数据、输出到显示器的结果),除了能够在显示器上显示以                   外,能够将处理后的结果用文件的方式保存到outfile.txt文件中。

  其实就是对链表的一些操作了,下面直接贴代码吧:

#include <iostream>
#include <cstdio>
#include <string>
#include
4000
<fstream>

using namespace std;

typedef struct Node{ //联系人节点信息
string name;
string sex;
string adress;
string phoneNum;
struct Node * next;
Node(Node * p = NULL ){ next= p;}
Node(const string &na,const string &se,const string &ad,const string &ph, Node * p = NULL){
name = na;
sex= se;
adress = ad;
phoneNum =ph;
next= p;
}
}Person;

class phonePage{
public :
phonePage(){ //构造函数,
Head = new Person();
}
//为新插入的联系人创建节点
Person *creatP(string na, string se, string ad,string ph){
Person *tmp= new Person(na,se,ad,ph);
return tmp;
}
//为新插入的联系人创建节点
Person *creatP(){
Person *tmp= new Person();
cout<<"输入联系人的姓名:";
cin>> tmp->name;
cout<<"输入联系人的性别:";
cin>> tmp->sex;
cout<<"输入联系人的地址:";
cin>> tmp->adress;
cout<<"输入联系人的号码:";
cin>> tmp->phoneNum;
tmp->next= NULL;
return tmp;
}
//文件读取联系人
void addPfromFile(){
ifstream ins;
string na,se,ad,ph;
ins.open("input.txt");
Person * newP;
Person *inse;
while(ins>>na>> se>> ad >> ph ){
newP= creatP(na,se,ad,ph);
inse = findLast();
inse->next = newP;
}
cout<< "导入联系人完毕"<<endl;
ins.close();
}
//查找插入联系人的位置
Person *findLast(){
Person *p = Head;
while( p->next !=NULL  )
p=p->next;
return p;
}
//键盘录入联系人信息
void  addPformKb(){
Person * newP =creatP();
newP->next = Head->next;
Head->next = newP;
}
//将所有联系人信息写到output.txt
void WtoFile(){
ofstream outs;
outs.open("output.txt");
Person *p = Head->next;
while(p != NULL ){
outs<<p->name<<" "<< p->sex << " "<< p->adress << " "<< p->phoneNum<< endl; p=p->next;
}
outs.close();
}
//修改联系人信息
void resetPerson(Person *someP){
if(someP == NULL ){
cout<<"查无此人"<<endl;
}else{
cout<<"要修改的联系人为."<<endl;
printP(someP);
cout<< "重新输入该联系人的信息."<<endl;
cout<<"输入联系人的姓名:";
cin>> someP->name;
cout<<"输入联系人的性别:";
cin>> someP->sex;
cout<<"输入联系人的地址:";
cin>> someP->adress;
cout<<"输入联系人的号码:";
cin>> someP->phoneNum;
cout<<"修改成功."<<endl;
}
}
//查找需要删除的联系人,并返回其前一节点的指针
Person * findFrontDelP(){
Person * q= Head;
Person * p =Head->next;
int cho;
string str;
cout<<"输入要删除的联系的信息"<<endl;
cout<<"1:按姓名查找."<<endl;
cout<<"2:按号码查找."<<endl;
cout<<"输入选择:";
cin>> cho;
if(cho == 1 )
cout<< "输入联系人姓名:";
else if(cho == 2 )
cout<< "输入联系人号码:";
else{
cout<< "选择输入有误."<<endl;
return NULL;
}
cin>> str;
while( p!=NULL ){
if(cho == 1 ){
if(p->name == str )
break;
}else{
if(p->phoneNum == str )
break;
}
p=p->next;
q=q->next;
}
if(p!=NULL )
return q;
return NULL;
}
//删除某人
void delP(){
Person * someP= findFrontDelP();
if( someP == NULL ){
cout<< "联系人不存在."<<endl;
}else{
cout<<"要删除的联系人为:"<<endl;
printP(someP->next);
char suredel;
cout<< "是否确定删除:"<<endl;
cout<<"Y:确定删除."<<endl;
cout<<"N:放弃."<<endl;
cout<<"输入选项:";
cin>> suredel;
if(suredel == 'Y' || suredel == 'y'){
Person * del = someP->next;
someP->next = someP->next->next;
delete del;
WtoFile();
}
}
}
//查找联系人,并返回该节点指针
Person *findP(){
Person *p =Head->next;
int cho;
string str;
cout<<"1:按姓名查找."<<endl;
cout<<"2:按号码查找."<<endl;
cout<<"输入选择:";
cin>> cho;
if(cho == 1 )
cout<< "输入联系人姓名:";
else if(cho == 2 )
cout<< "输入联系人号码:";
else{
cout<< "选择输入有误."<<endl;
return NULL;
}
cin>> str;
while(p!=NULL){
if(cho == 1 ){
if(p->name == str )
break;
}else{
if(p->phoneNum == str )
break;
}

p=p->next;
}
if(p == NULL )
return NULL;
return p;
}
//打印所有联系人
void printAll(){
Person *someP= Head->next;
int num=0;
if(someP == NULL )
cout<< "通讯录为空"<<endl;
while(someP != NULL ){
cout<< ++num<< " :"<<endl;
printP(someP);
someP= someP->next;
}
}
//输出查找到的联系人
void printP(Person *somebody){
if(somebody !=NULL ){
cout<<"姓名:" <<somebody->name      <<endl;
cout<<"性别:" <<somebody->sex       <<endl;
cout<<"地址:" << somebody->adress   <<endl;
cout<<"电话:" <<somebody->phoneNum  <<endl;
}else{
cout<< "查无此人"<<endl;
}
}
private:
Person * Head;
};

int mnue(){
int cho;
cout<<"------------------------"<<endl;
cout << "      田茂茂通讯录    " << endl;
cout<<"功能菜单:"<<endl;
cout<<"1:通讯录的建立"<<endl;
cout<<"2:通讯录查询"<<endl;
cout<<"3:通讯录的维护"<<endl;
cout<<"4:通讯录的输出"<<endl;
cout<<"5:退出"<<endl;
cout<<"输入选择:";
cin>> cho;
while(cho<1 || cho > 5){
cout<<"输入有误,从新输入."<<endl;
cout<<"输入选择:";
cin>>cho;
}
return cho;
}

int main()
{   phonePage *tml = new phonePage();
int cho;
while(  cho =mnue()){
if(cho == 1){
int tmp;
cout<<"1:文件中导入通讯录"<<endl;
cout<<"2:键盘录入"<<endl;
cout<<"输入选项:";
cin>>tmp;
if(tmp == 1){
tml->addPfromFile();
}else if(tmp == 2){
tml->addPformKb();
}else{
cout<<"输入有误."<<endl;
}
}else if(cho == 2){
tml->printP( tml->findP() );
}else if(cho == 3){
int tmp;
cout<<"1:修改"<<endl;
cout<<"2:删除"<<endl;
cout<<"3:查找"<<endl;
cout<<"输入选项:";
cin>> tmp;
if(tmp == 1){
tml->resetPerson( tml->findP() );
}else if(tmp == 2){
tml->delP();
}else if(tmp == 3){
tml->printP( tml->findP() ) ;
}else{
cout<<"输入有误."<<endl;
}
}else if(cho == 4){
cout<<"你的通讯录如下:"<<endl;
tml->printAll();
tml->WtoFile();
}else{
break;
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: