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

数据结构课程设计(题2)

2015-01-10 17:11 323 查看
题目及问题描述

1、李刚是一爱折腾的人,当然爱折腾的人均有梦想,他想当中国的盖次呢。可不,现在个人好友信息多了,复杂了,他想制作一个个人通讯录的制作管理软件。 刚好这个学期学了数据结构课,所以他准备使用数据结构知识来实现了。并考虑使用双向链表作数据结构。并制定了初步要求:

(1)每个好友信息包含姓名、性别、住址、邮编、几岁、电话、QQ、微信帐号、生日等。

(2)作为一个完整的系统,应具有友好的界面和较强的容错能力。

2、编程完成通讯录的一般性管理工作,如通讯录中记录录入、修改、查找、删除等功能。每个记录包含姓名、性别、电话号码、住址等基本呢信息。用数据结构中的双链表,结合C++语言基本知识,编写一个通讯录管理系统,以把所学知识应用到实际软件开发中去。了解并掌握数据结构和算法的设计方法,具备初步的独立分析和设计功能,初步掌握软件开发过程的问题分析,系统设计,程序编码,测试等基本方法和技能。

问题分析及功能

个人通讯录管理系统的主要任务是通过大量的资料获得管理所需要的信息,这就需要存储和管理个人的姓名、性别、住址、邮编、年龄、电话、QQ、微信、生日等信息了。因此利用数据结构的双链表建立一个良好的资料组织结构,使整个系统可以进行以下操作:

添加:能通过相应操作录入新数据。

查询:按姓名查询,显示此人全部信息。

删除:按姓名删除,删除此人全部信息。

修改:按姓名修改,修改此人某项信息。

逻辑结构和存储结构设计分析

逻辑结构:利用数据结构所学的双链表知识,开辟新节点存放通讯录的信息,修改。

存储结构:开辟4个链表指针,指向链表的第一个节点,输入内容记录在一个新的节点里。

四、源代码

#include<iostream>

#include<fstream>

#include<string>

#include<conio.h>//使用getchar()、getch()

usingnamespace std;

typedef structnode *link;

struct node

{

char stu_name[10];

char stu_sex[4];

char stu_adress[20];

char stu_postcode[8];

char stu_age[4];

char stu_phonenumber[10];

char stu_QQ[10];

char stu_weixin[10];

char stu_birth[10];


link prev,next;

};

linkptr,head,rear,current;
//4个link类型的指针,指向链表第一个节点


bool fFlag;

/********以下定义各种功能函数*******/

voidinit_dlink(); //初始化节点

voidread_file();

void write_file();

voidinsert_item();

voidsearch_item();

voidsort_item();

voiddelete_item();

voidprint_item();

voidmodify_item();

void anykey();

/********以上定义各种功能函数*******/

int main()

{

char option1,option2;

system("cls"); //控制台命令清0

init_dlink();

read_file(); //打开程序记录文件

while(1)

{


system("cls");



cout<<"*********************\n"<<endl;



cout<<"1.insertitem\n"<<endl;



cout<<"2.deleteitem\n"<<endl;



cout<<"3.searchitem\n"<<endl;



cout<<"4.sortitem\n"<<endl;



cout<<"5.modifyitem\n"<<endl;


cout<<"6.quit\n"<<endl;


cout<<"*********************\n"<<endl;



cout<<"Please enter yourchioce(1-5)...";



option1=getche(); //输入立刻运行,不带回显



switch(option1)



{



case'1':



fFlag=true; //布尔量=true和false



insert_item();



break;



case'2':



delete_item();



break;



case'3':



search_item();



break;



case'4':



modify_item();



break;



case'5':



if(fFlag) //insert过



{



cout<<"\n"<<endl;



cout<<"Save changes?(Y or N)"<<endl;



option2=getche();//等待输入Y或者N,不回显



if(option2=='Y')



{



write_file(); //写入到程序记录文件



exit(0);



}



if(option2=='N')



exit(0);



}



else //没有insert过



{



write_file();



exit(0);



}



}//end switch


}//end while(1)

return 0;

}//end main()

/********以下实现各种功能函数*******/

voidinit_dlink(void)


{

ptr=(link)malloc(sizeof *ptr);
//生成一个link大小的空间,类型是link,ptr指向这个空间的头。


/*******内容全部赋值0*************/

strcpy(ptr->stu_name,"0");


strcpy(ptr->stu_sex,"0");


strcpy(ptr->stu_adress,"0");

strcpy(ptr->stu_postcode,"0");

strcpy(ptr->stu_age,"0");

strcpy(ptr->stu_phonenumber,"0");

strcpy(ptr->stu_QQ,"0");

strcpy(ptr->stu_weixin,"0");

strcpy(ptr->stu_birth,"0");


/*******内容全部赋值0**************/

ptr->prev=ptr;

ptr->next=ptr;

head=ptr;

rear=ptr;

}

voidread_file(void)

{

FILE *fptr;

if((fptr=fopen("dlist.dat","r"))==NULL)//只读方式打开&&打开失败

{


cout<<"Data file notexist!\n"<<endl;



cout<<"Press any key to editfirst record...\n"<<endl;



getch();



insert_item();


}

else
//只读方式打开&&打开成功


{


ptr=(link)malloc(sizeof *ptr);//fsanf格式化输入,遇到空格和换行时结束。返回值:整型,成功读入的参数的个数



while(fscanf(fptr,"%s%s%s%s%s%s%s%s%s",ptr->stu_name,ptr->stu_sex,ptr->stu_adress,



ptr->stu_postcode,ptr->stu_age,ptr->stu_phonenumber,ptr->stu_QQ,ptr->stu_weixin,ptr->stu_birth)!=EOF)//返回EOF如果读取到文件结尾。



{ //把文本内容读取到 ptr的9个属性中去



if(strcmp(ptr->stu_name,"")!=0)//如果名字不为空



{



sort_item();



ptr=(link)malloc(sizeof *ptr);//每读一行,新建一个节点承载



}



else //如果名字为空



free(ptr);



}



fclose(fptr);


}

}

voidwrite_file(void)

{

FILE *fptr;

fptr=fopen("dlist.dat","w");//只写方式打开

current=head->next;

while(current!=head)

{


//fprint格式化输出到一个流/文件中



fprintf(fptr,"%s%s%s%s%s%s%s%s%s\n",current->stu_name,current->stu_sex,current->stu_adress,



current->stu_postcode,current->stu_age,current->stu_phonenumber,current->stu_QQ,current->stu_weixin,current->stu_birth);



//把prt9个属性写入到文本中



current=current->next;


}

fclose(fptr);

}

voidinsert_item(void)

{

system("cls");

ptr=(link)malloc(sizeof *ptr);

//开始输入

cout<<"Personname:"<<endl;

gets(ptr->stu_name);
//gets从标准输入设备读字符串函数,无限读,回车结束


cout<<"Personsex:"<<endl;

gets(ptr->stu_sex);

cout<<"Personadress:"<<endl;

gets(ptr->stu_adress);

cout<<"Personpostcode:"<<endl;

gets(ptr->stu_postcode);

cout<<"Personage:"<<endl;

gets(ptr->stu_age);

cout<<"Personphonenumber:"<<endl;

gets(ptr->stu_phonenumber);

cout<<"Person QQ:"<<endl;

gets(ptr->stu_QQ);

cout<<"Personweixin:"<<endl;

gets(ptr->stu_weixin);

cout<<"Person birth:"<<endl;

gets(ptr->stu_birth);

//输入内容全部记录在一个新的节点里。

sort_item();

}

voidsort_item(void)

{

current=head->next;

while(current!=head)

{


if(strcmp(ptr->stu_name,current->stu_name)<0)//如果第一个名字小于当前名字



{



ptr->next=current;



ptr->prev=current->prev;



current->prev->next=ptr;



current->prev=ptr;



break;



}



current=current->next;


}

if(head->next == head || current == head)

{


ptr->next=head;



ptr->prev=head->prev;



head->prev->next=ptr;



head->prev=ptr;



rear=ptr;


}

}

voiddelete_item(void)

{

char del_name[10];

int count=0;

link clear;

system("cls");

if(head->next==head)


cout<<"No personrecord!\n"<<endl;


else

{


cout<<"Delete personname:"<<endl;



gets(del_name);



current=head->next;



while(current->next!=head)



{



if(strcmp(del_name,current->stu_name)==0)



{



count++;



clear=current;



current->prev->next=current->next;



current->next->prev=current->prev;



current=current->next;



free(clear);



}



current=current->next;



}



if(strcmp(del_name,current->stu_name)==0)



{



count++;



clear=current;



current->prev=current->prev;



rear=current->prev;



free(clear);



}



if(count>0)



cout<<del_name<<"student record(s) deleted\n"<<endl;



else



cout<<del_name<<" notfound\n"<<endl;


}

anykey();


}

voidsearch_item(void)

{

char search_name[10];

system("cls");

if(head->next==head)


cout<<"No personrecord\n"<<endl;


else

{


cout<<"Search personname:"<<endl;



gets(search_name);



current=head->next;



while((current!=head)&&(strcmp(search_name,current->stu_name)!=0))



current = current->next;



if (current != head)



{


cout<<"--------------------------------------\t"<<endl;


cout<<" Personname:"<<current->stu_name<<"\t"<<endl;



cout<<" Person sex:"<<current->stu_sex<<"\t"<<endl;



cout<<" Personadress:"<<current->stu_adress<<"\t"<<endl;



cout<<" Personpostcode:"<<current->stu_postcode<<"\t"<<endl;



cout<<" Personage:"<<current->stu_age<<"\t"<<endl;



cout<<"Personphonenumber:"<<current->stu_phonenumber<<"\t"<<endl;



cout<<" PersonQQ:"<<current->stu_QQ<<endl;


cout<<"--------------------------------------\t"<<endl;


}



else



cout<<" Person"<<search_name<<" not found\n"<<endl;


}//end else

anykey();

}

voidmodify_item(void)

{

int count = 0;

char modify_name[10];

system("cls");

if(head->next == head)

cout<<" No personrecord\n"<<endl;

else

{


cout<<" Modify personname:"<<endl;



gets(modify_name);



current = head->next;



while(current != head)



{



if(strcmp(modify_name, current->stu_name)== 0)



{


cout<<"******************************\n"<<endl;


cout<<" Personname:"<<current->stu_name<<"\n"<<endl;



cout<<" Personsex:"<<current->stu_sex<<"\n"<<endl;



cout<<" Personadress:"<<current->stu_adress<<"\n"<<endl;



cout<<" Personpostcode:"<<current->stu_postcode<<"\n"<<endl;



cout<<" Personage:"<<current->stu_age<<"\n"<<endl;



cout<<" Personphonenumber:"<<current->stu_phonenumber<<"\n"<<endl;


cout<<" PersonQQ:"<<current->stu_QQ<<"\n"<<endl;

cout<<" Personweixin:"<<current->stu_weixin<<"\n"<<endl;


cout<<" Please enter new_sex:"<<endl;



gets(current->stu_sex);



cout<<" Please enter newadress:"<<endl;



gets(current->stu_adress);



cout<<" Please enter newpostcode:"<<endl;



gets(current->stu_postcode);


cout<<" Please enter new_age:"<<endl;


gets(current->stu_age);



cout<<" Please enter new_phonenumber:"<<endl;



gets(current->stu_phonenumber);


cout<<" Please enter new_QQ:"<<endl;


gets(current->stu_QQ);


cout<<" Please enter new_weixin:"<<endl;


gets(current->stu_weixin);


cout<<" Please enter new_birth:"<<endl;


gets(current->stu_birth);



count++;



}



current = current->next;



}



if(count > 0)



cout<<count<<" personrecord(s) modified\n"<<endl;



else



cout<<" Person"<<modify_name<<" not found\n"<<endl;


}

anykey();

}

voidprint_item(void)

{

int count = 0;

system("cls");

if(head->next == head)


cout<<" No personrecord\n"<<endl;


else

{


cout<<" person_NAME SEX ADDRESS
AGE PHONENUMBER QQ WEIXIN BIRTH\n"<<endl;


cout<<"--------------------------------------------------------------------------\n"<<endl;


current = head->next;



while(current != head)



{


cout<<current->stu_name<<"
"<<current->stu_sex<<" "<<current->stu_adress<<" "<<current->stu_postcode<<"
"<<current->stu_QQ<<" "<<current->stu_weixin<<" "<<current->stu_birth<<endl;



count++;



current = current->next;



if(count % 20 == 0)



getch();



}


cout<<"-------------------------------------------------------------------------------\n"<<endl;

cout<<" Total"<<count<<" record(s) found\n"<<endl;

}

anykey();

}

voidanykey(void)

{

cout<<" Press any key tocontinue..."<<endl;

getch();

五、程序运行结果





按任意键,首先出现一个界面,显示出各项功能选择,信息的录入、删除、查找和修改。





选择功能1,录入个人的信息,包括姓名、性别、住址、邮编、几岁、电话、QQ、微信帐号、生日。





根据姓名查找显示各项信息:












六、体会及不足

数据结构这次的个人通讯录管理的课程设计,我认识到了要做出一个完整的管理系统真的挺不容易的,这次的程序设计是要用双链表的知识去弄的,但是关于双链表的知识点自己还不能够完全熟练掌握,还有些东西已经忘记了,需要翻阅课本才能记起来,还请教同学,与同学们一起讨论该如何更好地实现。在这期间还遇到了其他的困难,比如在开始编程序之前,我需要想一下这个需要用什么样的逻辑结构和存储结构才能实现,程序报错的时候修改的过程中不能马上准确知道报错的地方是哪里等等。我所欠缺的这些,都是应该在日后的学习当中所要注重培养的能力。不过还是挺感谢为我提供帮助的同学们,让我遇到困难还可以继续前进。

因为能力有限,在这个通讯录管理系统当中,还存在了很多的不足的地方,例如系统界面还不是很完美,还有功能实现的时候也是比较简单的,只是涉及到管理系统的信息录入、查询、修改和删除这些。无论是数据结构,还是数据库或者是Java,我都需要抽出更多的时间去上机操作,在不断练习的过程中提升自己的能力。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: