C语言通信录管理系统(包括录入,删除,修改,保存等功能)
# include "stdio.h"# include "string.h"
# include "malloc.h"
# include "process.h"
# define LEN sizeof (struct snode)
char cfilename[10];
int pd=0;
FILE *Ffp;
struct snode
{char cname [10];
char caddress[28];
char cmobile [15];
char chomephone [16];
char cofficephone [16];
struct snode *pnext;
}*phead;
void titleletters()
{printf("**********************\n");
printf(" ****通迅管理系统****\n");
printf(" *******(四组)*******\n");
printf("**********************\n");
}
void helpletters()
{printf("需要帮助,请输入?\n");
printf("建立一个空通迅录,请输入n\n");
printf("打开一个通迅录,请输入0\n");
printf("保存当前通迅录,请输入s\n");
printf("关闭当前通迅录,请输入c\n");
printf("增加一条记录,请输入a\n");
printf("游览当前通迅录,请输入v\n");
printf("编辑一条记录,请输入e\n");
printf("删除一条记录,请输入d\n");
printf("退出,请输入q\n");
}
void panduan()
{printf ("没有打开文件\n");
return;
}
/*新建函数*/
void newlist()
{phead=NULL;
printf("一个新空通迅录已建立\n");
}
/*打开函数*/
void openlist()
{
struct snode *ppointer,*pinput;
printf("请输入文件名:");
scanf("%s",cfilename);
if((Ffp=fopen(cfilename,"r+"))==NULL)
{
printf("不能打开这个文件。\n");
return;
}
rewind(Ffp);
phead=NULL;
if(!feof(Ffp))
{
pinput=(struct snode*)malloc(LEN);
fscanf(Ffp,"%s %s %s %s %s\n",pinput->cname,pinput-
>caddress,pinput- >cmobile,pinput->chomephone ,pinput-
>cofficephone);
pinput->pnext=NULL;
phead=pinput;
ppointer=phead;
}
while(!feof(Ffp))
{
pinput=(struct snode*)malloc(LEN);
fscanf(Ffp,"%s %s %s %s %s\n",pinput->cname,pinput-
>caddress,pinput->cmobile,pinput->chomephone ,pinput-
>cofficephone);
pinput->pnext=NULL;
ppointer->pnext=pinput;
ppointer=ppointer->pnext;
}
printf("这个文件已打开.\n");
pd=1;
}
/*保存函数*/
void savelist()
{if (!pd)
{
panduan();
return;
}
struct snode *ppointer;
if(Ffp==NULL)
{
printf("请输入文件名:");
scanf("%s",cfilename);
if((Ffp=fopen(cfilename,"w"))==NULL)
{
printf("不能保存空文件。\n");
return;
}
}
rewind(Ffp);
ppointer=phead;
while(ppointer!=NULL)
{
fprintf(Ffp,"%-8s %-28s %-10s %-16s %-
10s\n",ppointer->cname,ppointer->caddress,ppointer-
>cmobile,ppointer ->chomephone,ppointer-
>cofficephone);
ppointer=ppointer->pnext;
}
printf("已保存\n");
}
/*关闭函数*/
void closelist()
{if (!pd)
{
panduan();
return;
}
if(fclose(Ffp)!=0)
{
printf("这个文件不能关闭\n");
return;
}
phead=NULL;
printf("这个文件已关闭\n");
}
/*追加函数*/
void appendrecord()
{if (!pd)
{
panduan();
return;
}
char corder[2];
int iinputcheck;
struct snode *ppointer,*pinput;
re:
printf("请输入新记录:姓名 地址 家庭电话 手机 办公电话
\n");
pinput=(struct snode*)malloc(LEN);
iinputcheck=scanf("%s%s%s%s%s",pinput->cname,pinput-
>caddress,pinput->cmobile,pinput->chomephone,pinput-
>cofficephone);
if(iinputcheck==0)
{
printf("输入数据错误\n");
return;
}
pinput->pnext=NULL;
if(phead==NULL)
{
phead=pinput;
printf("这条记录已追加\n");
}
else
{
ppointer=phead;
while((ppointer->pnext)!=NULL)
ppointer=ppointer->pnext;
ppointer->pnext=pinput;
printf("这条记录已追加\n");
}
printf("想要追加其他记录吗(y/n)?");
scanf("%s",corder);
if(!strcmp(corder,"y"))
goto re;
}
/*浏览记录函数*/
void viewrecord ()
{
if (!pd)
{
panduan();
return;
}
struct snode *ppointer;
if(phead==NULL)
{
printf("此通讯录显示为空\n");
return;
}
printf ("%-8s %-28s %-10s %-16s %-10s\n","姓名","地址","家
庭电话","手机","办公电话");
ppointer=phead;
while(ppointer!=NULL)
{
printf("%-8s,%-28s,%-10s,%-16s,%-10s\n",ppointer-
>cname,ppointer->caddress,
ppointer->cmobile,ppointer->chomephone,ppointer-
>cofficephone);
ppointer=ppointer->pnext;
}
}
/*编辑记录函数*/
void editrecord()
{
if (!pd)
{
panduan();
return;
}
char cnamekey[10];
int iinputcheck,ioutputcheck;
struct snode *ppointer;
if (phead==NULL)
{
printf("这条记录显示为空\n");
return;
}
printf("请输入姓名\n");
scanf("%s",cnamekey);
ppointer=phead;
while(ppointer!=NULL)
{
if (!strcmp(cnamekey,ppointer->cname))
break;
ppointer=ppointer->pnext;
}
if (ppointer!=NULL)
{
printf("这个记录:\n");
ioutputcheck=printf("%-8s %-28 %-10s
%-16s %-10s\n",ppointer->cname,ppointer-
>caddress,ppointer->cmobile,ppointer-
>chomephone,ppointer->cofficephone);
if(ioutputcheck<0)
{
printf("输出数据有错\n");
return;
}
printf("请输入新记录\n");
iinputcheck=scanf("%s %s %s %s %
s\n",ppointer->cname,ppointer->caddress,ppointer-
>cmobile,ppointer->chomephone,ppointer-
>cofficephone);
if (iinputcheck==0)
{
printf("输出数据有错\n");
return;
}
}
else
printf("这条记录没找到\n");
}
/*删除函数*/
void deleterecord()
{
if (!pd)
{
panduan();
return;
}
char cnamekey[10];
struct snode *ppointer,*pformer;
if(phead==NULL)
{
printf("这条记录显示为空\n");
return;
}
ppointer=phead;
pformer=NULL;
printf("请输入姓名: \n");
scanf("%s",cnamekey);
while (strcmp(cnamekey,ppointer->cname)&&((ppointer-
>pnext)!=NULL))
{
pformer=ppointer;
ppointer=ppointer->pnext;
}
if (!strcmp(cnamekey,ppointer->cname))
{
printf("这条记录已找到\n");
printf("%-10s %-28s %
-15s %-16s %-16s\n",ppointer
->cname, ppointer->caddress,ppointer->cmobile,
ppointer->chomephone, ppointer->cofficephone);
if(ppointer==phead)
phead=ppointer->pnext;
else
pformer->pnext=ppointer->pnext;
printf("这条记录已被删除\n");
}
else
printf("这条记录没找到\n");
}
/*退出函数*/
void quitsystem()
{
closelist();
exit (0);
}
/*主函数*/
main ()
{
char a;
titleletters();
helpletters();
while (1)
{
scanf("%c",&a);
switch (a)
{
case '?':helpletters();break;
case 'n':newlist() ;break;
case 'o':openlist();break;
case 's':savelist();break;
case 'c':closelist();break;
case 'a':appendrecord();break;
case 'v':viewrecord () ;break;
case 'e':editrecord() ;break;
case 'd':deleterecord();break;
case 'q':quitsystem();break;
}
}
}