c语言课程设计例子电话本.docx
- 文档编号:8832177
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:17
- 大小:19.94KB
c语言课程设计例子电话本.docx
《c语言课程设计例子电话本.docx》由会员分享,可在线阅读,更多相关《c语言课程设计例子电话本.docx(17页珍藏版)》请在冰豆网上搜索。
c语言课程设计例子电话本
功能:
1、增加、修改、删除、查找记录
2、存入添加记录的时间,并在显示数据时候一并显示。
3、查询时输入姓名首字母即可查找的记录
4、保存在dat文件中,并可以读取
#include
#include
#include
#include
#defineNodestructnode
Node/*结构体类型定义,包括:
姓、名和电话号码*/
{
charszName[20];
charszPhoneNum[12];
Node*next;
};
Node*strpHead;/*全程变量,链头指针*/
Node*strpCurrent;/*全程变量,用于指明当前在链表中的位置*/
/*functionprototypes*/
voidHandleChoice_f(int);/*--函数原形说明*/
voidAddRecord_f();
voidInsertNode_f(Node*);
Node*InsertPoint_f(char*);
voidMakeNewHead_f(Node*);
voidAddToEnd_f(Node*);
voidMoveToEnd_f();
voidDisplayList_f();
voidDeleteRecord_f();
voidDelHead_f();
voidDelEnd_f(Node*);
voidDelMid_f(Node*);
intVerifyDel_f();
voidDelNode_f(Node*);
voidDelList_f();
voidSearchByName_f();
voidWriteFile_f();
voidLoadFile_f();
voidHelp_f();
intmain()
{
intnChoice;
printf("WelcometoPhoneBook1.0fordosandfreedos\n");
printf("PressEnterToContinue\n");
system("pause");
system("cls");/*执行系统命令:
cls—清屏*/
strpHead=NULL;/*InitializeheadpointertoNULL*/
LoadFile_f();/*Loaddatafromthediskfileintolinkedlist.*/
do
{/*Displaymenu.--主菜单显示*/
printf("1-Addrecord\n");
printf("2-Displayallrecords\n");
printf("3-Searchforfriendbyname\n");
printf("4-Deleterecord\n");
printf("5-Help\n");
printf("6-Exitprogram\n");
printf("Enterchoice:
");
scanf("%d",&nChoice);
HandleChoice_f(nChoice);/*Callfunctiontodirectflowbasedonchoice.*/
}while(nChoice!
=6);/*Repeatmenuuntiluserchoosestoexit.*/
return0;
}/*endofmainfunction*/
/*Functiontodirectprogramflowbasedonuser'schoice.*/
voidHandleChoice_f(intnChoice)/*根据用户选择(choice)调用对应处函数*/
{
switch(nChoice)/*choiceispassedintothefunctionbyvalue.*/
{
case1:
/*Ifchoicewastoaddarecordtothedatabase,*/
AddRecord_f();/*callfunctiontoaddarecordtothelinkedlist.*/
break;
case2:
/*Ifchoicewastodisplayallrecordsinthedatabase,*/
DisplayList_f();/*callfunctiontodisplayallrecordsin*/
break;/*thelinkedlist.*/
case3:
/*Ifchoicewastosearchforarecordinthedatabase,*/
SearchByName_f();/*callfunctiontosearchforrecordby*/
break;/*lastname.*/
case4:
/*Ifchoicewastodeletearecordinthedatabase,*/
DeleteRecord_f();/*callafunctionthatsearchedforrecord*/
break;/*bylastnameanddeletesit.*/
case5:
/*Ifchoiceisforhelp*/
Help_f();
break;
case6:
/*Ifchoicewastoexit,*/
WriteFile_f();/*savedatabasetoafileand*/
if(strpHead!
=NULL)/*deletethelistfrommemory.*/
{
DelList_f();
}
break;
default:
/*Ifanyother(invalid)choicewasentered,*/
printf("Invalidchoice\n");/*displayerrormessage.*/
break;
}
}/*endoffunctionHandleChoice_f*/
/*Functiontoaddrecordtothelinkedlist.*/
voidAddRecord_f()/*在链表中增加一个记录*/
{
Node*strpNew;/*Declaretemporarypointerforthenewnode.*/
strpNew=(Node*)malloc(sizeof(Node));/*Allocatememoryforanewnodeand*/
/*initializepointertopointtoit.*/
/*Getnameandphonenumberfromtheuser.*/
getchar();
printf("Name:
");
gets(strpNew->szName);
printf("PhoneNumber:
");
gets(strpNew->szPhoneNum);
InsertNode_f(strpNew);
system("cls");
}/*endoffunctionAddRecord_f*/
/*Functiontoinsertnewnodeintocorrectpositioninlist.*/
voidInsertNode_f(Node*strpNew)/*将一个由strpNew指向的新节点插入链表中*/
{
Node*strpFront;
Node*strpBack;
system("cls");
if(strpHead==NULL)
{/*Ifnonodesexist,makethenode*/
strpNew->next=NULL;/*thehead.*/
strpHead=strpNew;
}
else
{
if(strcmp(strpNew->szName,strpHead->szName)<0)
{/*Ifnewrecordcomesbeforehead,*/
MakeNewHead_f(strpNew);/*makeitthenewhead.*/
}
else/*Else,determinewherethenewnode*/
{/*shouldbeinserted.*/
strpCurrent=InsertPoint_f(strpNew->szName);
strpFront=strpCurrent;/*Usepointerstokeeptrackofnodes*/
strpBack=strpCurrent->next;/*oneachsideoftheinsertionpoint.*/
if(strpBack==NULL)/*IfstrpBackisNULL,thenodeneedstobe*/
{/*addedtotheendofthelist.*/
AddToEnd_f(strpNew);
}
else/*Elseaddthenodebetweenthenodespointedto*/
{/*bystrpFrontandstrpBack.*/
strpFront->next=strpNew;
strpNew->next=strpBack;
}
}
}
}/*endoffunctionInsertNode_f*/
/*FunctionthatpositionsstrpCurrentatthenodebeforetheposition*/
/*wherethenewnodeshouldbeinserted.*/
Node*InsertPoint_f(char*szName)
{/*根据姓氏,返回其在链表中的正确位置。
新节点即将插入此点*/
charszTempName[20];
Node*strpTemp;
intnTemp;
if(strpHead->next!
=NULL)/*Ifmorethanonenodeexists,searchthe*/
{/*listforthecorrectinsertionpoint.*/
strpCurrent=strpHead;
strpTemp=strpCurrent->next;
strcpy(szTempName,strpTemp->szName);
/*Loopuntiltheproperinsertionpointislocated.*/
nTemp=strcmp(szName,szTempName);
while((nTemp>0)&&(strpCurrent->next!
=NULL))
{
strpCurrent=strpTemp;
/*checktoseeifthecurrentnodeisthelastnode*/
if(strpCurrent->next!
=NULL)
{
strpTemp=strpCurrent->next;
strcpy(szTempName,strpTemp->szName);
nTemp=strcmp(szName,szTempName);
}
}
}
else/*Ifonlyonenodeexistsinthelist,strpCurrentisthesame*/
{/*asstrpHead.Newnodewillbeaddedtotheendofthelist.*/
strpCurrent=strpHead;
}
return(strpCurrent);
}/*endoffunctionInsertNode_f*/
/*FunctionthatmakesthenodepointedtobystrpNewthenew*/
/*headofthelinkedlist.Ithandlesthespecialcaseofinsertingat*/
/*thefrontofthelist.*/
voidMakeNewHead_f(Node*strpNew)
{
Node*strpTemp;/*temporarypointertokeeptrackofthehead*/
strpTemp=strpHead;/*SetstrpTemptopointatthecurrenthead.*/
strpNew->next=strpTemp;/*Makenewnodesnextpointerpointto*/
strpHead=strpNew;/*currentheadandmakenewnodethehead.*/
}/*endoffunctionMakeNewHead_f*/
/*Functionthataddsanodetotheendofthelinkedlist.Ithandles*/
/*thespecialcaseofinsertingattheendofthelist.*/
voidAddToEnd_f(Node*strpNew)
{
strpNew->next=NULL;/*SetnextnodepointerofnewnodetoNULL.*/
MoveToEnd_f();/*MakesurestrpCurrentisatendoflist.*/
strpCurrent->next=strpNew;/*Placenewnodeattheendofthelist.*/
}/*endoffunctionAddToEnd_f*/
/*FunctionthatmovesstrpCurrenttoendofthelinkedlist.*/
voidMoveToEnd_f()
{
strpCurrent=strpHead;/*MovestrpTemptoheadofthelist.*/
while(strpCurrent->next!
=NULL)
{/*TraverselistuntilNULLisreached.*/
strpCurrent=strpCurrent->next;
}
}/*endoffunctionMoveToEnd_f*/
/*Functionthatdisplaysentirelinkedlist.*/
voidDisplayList_f()
{
charszName[36];/*usedtocombinenamesintoonearray*/
strpCurrent=strpHead;/*MovestrpCurrenttoheadoflist.*/
if(strpCurrent!
=NULL)
{
printf("\n");
printf("NamePhoneNumber\n");
printf("-----------------------------------------------\n");
do
{
printf("%10s",strpCurrent->szName);
printf("%20s\n",strpCurrent->szPhoneNum);
strpCurrent=strpCurrent->next;/*SetstrpCurrenttonextnode.*/
printf("\n");
}while(strpCurrent!
=NULL);/*Loopuntilendoflist.*/
printf("PressEntertocontinue\n");
system("pause");
system("cls");
}
else/*Iflistisempty,displaymessage.*/
{
printf("\nNORECORDSTODISPLAY\n");
}
}/*endoffunctionDisplayList_f*/
/*Functionthatsearcheslinkedlistforthefirstoccurrenceofagiven*/
/*lastnameanddisplaystherecordtothescreen.*/
voidSearchByName_f()/*利用姓名进行查询*/
{
charszSearch[20];/*Characterarrayforlastnametosearchfor.*/
strpCurrent=strpHead;/*MovestrpCurrenttoheadoflist*/
system("cls");/*tobeginsearch.*/
getchar();
printf("\nEnterthenameforwhichyouwanttosearch:
");
gets(szSearch);
/*LoopuntilszSearchisfoundorendoflistisreached.*/
while((strpCurrent!
=NULL)&&(strcmp(strpCurrent->szName,szSearch)!
=0))
{
strpCurrent=strpCurrent->next;
}
if(strpCurrent!
=NULL)/*IfstrpCurrentisnotNULL,thenmatchwas*/
{/*found.*/
printf("\nRECORDFOUND\n");
printf("%s\n",strpCurrent->szName);
printf("%s\n",strpCurrent->szPhoneNum);
}
else
{
printf("NOMATCHFOUND\n");
printf("PressEntertoContiune\n");
system("pause");
system("cls");
}
}/*endoffunctionSearchByName_f*/
/*Functionthatdeletesindividualnodesfromthelinkedlist.*/
voidDeleteRecord_f()/*删除一个记录*/
{
charszSearch[20];
Node*strpFront;
system("cls");
strpFront=NULL;/*InitializestrpFronttoNULL.*/
strpCurrent=strpHead;/*MovestrpCurrenttoheadoflist*/
/*tobeginsearch.*/
getchar();
printf("\nEnterthenameofthefriendyouwanttodelete:
");
gets(szSearch);
/*Looptofindmatchingrecord.*/
while((strpCurrent!
=NULL)&&(strcmp(strpCurrent->szName,szSearch)!
=0))
{
strpFront=strpCurrent;/*Apointermustbemaintainedthat*/
strpCurrent=strpCurrent->next;/*pointstothenodebeforethenode*/
}/*tobedeleted.*/
if(strpCurrent!
=NULL)/*IfstrpCurrentisnotNULL,thenmatchwasfound*/
{
printf("\nRECORDFOUND\n");
printf("%s\n",strpCurrent->szName);
printf("%s\n",strpCurrent->szPhoneNum);
if(VerifyDel_f())/*Askuserifhe/shewantstodeletetherecord.*/
{/*Ifuserwantstodeletetherecord,*/
DelNode_f(strpFront);/*deletethenodethatfollowsthe*/
printf("\nRECORDDELETED\n");/*onepointedtobystrpFront.*/
}
else/*Otherwise,donothing.*/
{
printf("\nRECORDNOTDELETED\n");
}
}
else/*Ifnomatchfortherecordfound,displaymessage.*/
{
printf("\nNOMATCHFOUND.NORECORDDELETED.\n");
}
system("cls");
}/*endoffunct
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 课程设计 例子 电话本
![提示](https://static.bdocx.com/images/bang_tan.gif)