#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Node struct node
Node//结构体类型定义,包括:姓名和电话号码
{
char szName[20];//姓名
charszPhoneNum[12];//电话号码
Node *next;
};
Node *strpHead//全局变量,表头指针
Node *strpCurrent;//全局变量,指明当前正在操作的节点
//函数原型
void HandleChoice_f( int );
void AddRecord_f( int );
void InsertNode_f( Node * );
Node *InsertPoint_f( char * );
void MakeNewHead_f( Node * );
void AddToEnd_f( Node * );
void MoveToEnd_f();
void Displaylist_f();
void DeleteRecord_f();
void DelHead_f();
void Delend_f( Node * );
void DelMid_f( Node * );
int VerifyDel();
void DelNode_f( Node * );
void Dellist_f();
void WriteFile_f():
void SearchByName_f();
void LoadFile_f();
void Help_f();
int main()
{
int nChoice;
printf("Welcome to phoneBook for dos & freedos/n");
printf("Press Enter to continue/n");
system("pause");
system("cls");//清屏
strpHead = NULL;//初始化指针
LoadFile_f();//读取数据链表
{
do
{
printf("1- Add record/n");
printf("2- Display all records/n");
printf("3- Search for Friend by name /n");
printf("4- Delete record/n");
printf("5-Help/n");
printf("6-Exit program/n");
printf("Enter choice:/n");
scanf("%d",&nChoice);
HandleChoice_f(nChoice);//接受用户的选择
}while(nChoice !=6) // 退出
return 0;
}
void HandleChoice_f( int nChoice)//处理用户的选择
{
switch(nChoice)
{
case 1:
AddRecord_f();//增加记录
break;
case 2:
DisplayList_f();//显示所有的用户
break;
case 3:
SearchByName_f();//跟据姓名查找
break;
case 4://跟据姓名删除
DeleteRecord_f();
casse 5://显示帮助信息
Help_f();
break;
case 6://选择退出
WriteFile_f();//将链表中的数据文件写入文件
if(strpHead != NULL)
{
DelList_f()
}
break;
default://用户输入其他选择
printf("Invalid choice/n");//显示出错信息
break;
}
}
void AddRecord_f()//再链表中添加一个记录
{
Node *strpNew;//新纪录的指针变量
strpNew= (Node*)malloc(sizeof(Node));
getchar();
printf("Name:/n");
gets(strpNew->szName);
printf("Photo Number:/n");
gets(strpNew->szPhotoNum);
InsertNode_f(strpNew);
system("cls");
if(strpHead == NULL)
{
strpNew->next = NULL;
strpHead = strpNew;
}
else
{
if(strpcmp(strpNew->szName,strp->szName)<0);
{
MakeNewHead_f(strpNew);//新节点在头结点之前,使之成为头结点、
}
else //否则,查找新节点的位置
{
strpCurrent = InsertPoint_f(strpNew->szName);
strpFront = strpCurrent;
strpBack = strpCurrent->next;
if(strpBack == NULL)
{
AddToEnd_f(strpNew);
}
else {
strpFront->next = strpNew;
strpNew->next = strpBack;
}
}
}
}
Node *InsertPoint_f(char *szName)//根据新增记录的姓氏,返回其插入的正确位置
{
char szTemName[20];
Node *strpTemp;
int nTemp;
if(strpHead->next != NULL)//如果有多于一个节点的链表,查找节点在链表中的正确位置
{
strpCurrent = strpHead;
strpTemp = strpCurrent->next;
strcopy(szTempName,strTemp->szName):
nTemp = strcmp(szName,szTempName);
while((nTemp>0)&&(strpCurrent->next != NULL))
{
strpCurrent = strpTemp;
//查找当前的节点是不是最后一个
if(strpCurrent->next != NULL)
{
strpTemp = strpCurrent->next;
strcopy(szTempName,strpTemp->szName);
nTemp = strcmp(szName,szTempName);
}
}
}
else
{
strpCurrent = strpHead;
}
return (strpCurrent);
}
void makeNewHead_f(Node *strpNew)//新节点成为链表的新头结点
{
Node *strpTemp;
strpTemp = strpHead;
strpNew->next = strpTemp;
strpHead = strpNew;
}
void AddToEnd_f(Node * strpNew)//新节点成为链表的尾节点
{
strpNew->next = NULL;
MoveToEnd_f();
strpCurernt->next = strpNew;
}
void MoveToEnd_f()/*当前指针移到链表尾*/
{
strpCurrent=strpHead;
while(strpCurrent->next!=NULL)
{
strpCurrent=strpCurrent->next;
}
}
void DisplayList_f()/*显示节点内容*/
{
char szName[36];
strpCurrent=strpHead;
if(strpCurrent!=NULL)
{
printf("/n");
printf("Name Phone Number/n");
printf("-------------------------------");
do
{
printf("%s",strpCurrent->szName);
printf("%20s",strpCurrent->szPhoneNum);
strpCurrent=strpCurrent->next;
printf("/n");
}while(strpCurrent!=NULL);
printf("Press Enter to continue/n");
system("pause");
system("cls");
}
else
{
printf("/nNO RECORD TO DISPLAY/N");
}
}
/*利用姓名进行查询*/
void SearchByName_f()
{
char szSearch[20];
strpCurrent=strpHead;
system("cls");
getchar();
printf("/nEnter the name for which you want to search:");
gets(szSearch);
while((szCurrent!=NULL)&&(strcmp(strpCurrent->szName,szSearch)!=0))
{
strpCurrent=strpCurrent->next;
}
if(strpCurrent!=NULL)
{
printf("RECORD FOUND/N");
printf("%s/n",strpCurrent->szName);
printf("%s/n",strpCurrent->szPhoneNum);
}
else
{
printf("NO MATCH FOUND/N");
printf("Press Enter to Continue/n");
system("pause");
system("cls");
}
}
/*删除一个记录*/
void DeleteRecord_f()
{
char szSearch[20];
Node *strpFront;
system("cls");
strpFront=NULL;
strpCurrent=strpHead;
getchar();
printf("/nEnter the name of the friend you want to delete:");
gets(szName);
while((strpCurrent!=NULL)&&(strcmp(strpCurrent->szName,szSearch)!=0))
{
strpFront=strpCurrent;
strpCurrent=strpCurrent->next;
}
if(strpCurrent!=NULL)
{
printf("/nRECORD FOUND/n");
printf("%s",strpCurrent->szName);
printf("%s",strpCurrent->szPhoneNum);
if(VerifyDel_f())
{
DelNode_f(strpFront);
printf("/nRECORD DELETED/n");
}
else
{
printf("/nRECORD NOT DELETED/n");
}
}
else
{
printf("/nNO MATCH FOUND.NO RECORD DELETED./n");
}
system("cls");
}
void Help_f()
{
int nChoice;
do
{
system("cls");
printf("Welcome to help please choose help type/n");
printf("1:What happened to my records/n");
printf("2:How do I clear all records/n");
printf("3:When will there be a new version/n");
printf("4:What will be in the new version/n");
printf("5:Exit/n");
scanf("%d",&nChoice);
switch(nChoice)
{
case 1:
printf("Check the directory with the executable for a file named Friends.dat/n");
printf("If it's not there its gone forever if ranamed raneme it to Friends.dat/n");
printf("Press Enter to continue/n")
getch();
system("cls");
break;
case 2:
printf("Delete The file Friends.dat/n");
printf("Press Enter to continue");
getch();
system("cls");
break;
case 3:
printf("When I get time/n");
printf("Press Enter to continue");
getch();
system("cls");
break;
case 4:
printf("I hope add the following/n");
printf("Multi User's and passwords/n");
printf("Encryption/n");
printf("Clear all records command/n");
printf("Bug fixs/n");
printf("Press Enter to continue/n");
getch();
system("cls");
break;
default:
case 5:
printf("Press Enter To exit/n");
printf("Press Enter to continue/n");
break;
}
}while(nChoice!=5);
}
/*删除信息时要求予以确认*/
int VerifyDel_f()
{
char chYesNo;
printf("/nAre you sure(Y/N)");
scanf("%c",&chYesNo);
if((chYesNo=='Y')||(chYesNo=='y'))
{
return (1);
}
else
{
return(0);
}
}
/*删除节点*/
void DelNode_f(Node *strpFront)
{
if(strpCurrent==strpHead)
DelHead_f();
else
{
if(strpCurrent->next==NULL)
DelEnd_f(strpFront);
else
DelMid_f(strpFront);
}
}
/*删除头结点*/
void DelHead_f()
{
strpCurrent=strpHead;
if(strpHead->next!=NULL)
strpHead=strpCurrent->next;
else
strpHead=NULL;
if(strpHead->next!=NULL)
strpHead=strpCurrent->next;
else
strpHead=NULL;
free(strpCurrent);
}
void DelEnd_f(Node *strpFront)//删除尾节点
{
free(strpCurrent);
strpFront->next=NULL;
strpCurrent=strpHead;
}
void DelMid_f(Node *strpFront)//删除链表中的节点(非尾节点)
{
strpFront->next=strpCurrent->next;
free(strpCurrent);
strpCurrent=strpHead;
}
void DelList_f()//删除链表释放空间
{
Node *strpTemp;
strpCurrent=strpHead;
do
{
strpTemp=strpCurrent->next;
free(strpCurrent);
strpCurrent=strpTemp;
}
while(strpTemp!=NULL);
}
void Deln(char *szString)//删除字符串后的回车符
{
int nLength;
nLength=StrLen(szString);
if(nLength>=2)
{
if(szString[nLength-1]=='/n')
(szString[nLength-1]='/0';
}
}
Void WriteFile_f()//程序退出,将链表数据写回文件
{
FILE *fpoutfile;//输出文件指针
if((fpoutfile=fopen("FRIENDS.DAT","w"))==NULL)
{
printf("File Error!/n");
exit(0);
}
strpCurrent=strpHead;
if(strpHead!=NULL)
{
do
{
fprint(fpoutfile,"%s/n",strpCurrent->szName);
fprint(fpoutfile,"%s/n",strpCurrent->szPhoneNum);
strpCurrent=strpCurrent->next;
}while(strpCurrent!=NULL);
}
fprint(fpoutfile,"END OF FILE");
fclose(fpoutfile);//关闭文件
}
void LoadFile_f()//从数据文件 FRIENDS.DAT 中读取数据重建链表
{
Node *strpNew;
FILE *fpinfile;//删除文件指针
int nEndLoop=0;
if((fpinfile=fopen("FRIENDS.DAT","r"))==NULL)
{
printf("No usable data file located. list is empty./n");
}
else
{
do
{
strpNew=(Node *)malloc(sizeof(Node));
if(strpNew!=NULL)
{
fgets(strpNew->szName,20,fpinfile);
if((strcmp(strpNew->szName,"")!=0)&&(strcmp(strpNew->szName,"END OF FILE")!=0))
{
fgets(strpNew->szPhoneNum,15,fpinfile);
Deln(strpNew->szName);
Deln(strpNew->szPhoneNum);
InsertNode_f(strpNew);
}
else
{
free(strpNew);
nEndLoop=1;
}
}
else
{
printf("WARNING:Memory error.Load from disk was unsuccessful./n");
nEndLoop=1;
}
}while(nEndLoop==0);
fclose(fpinfile);
}
}