c语言中做一个通讯录,能够添加、删除、修改、查找

c语言中做一个通讯录,能够添加、删除、修改、查找,第1张

# include<stdio.h>

# include<string.h>

struct tongxun

{char name[20]

char number[20]

struct tongxun *next

}

int all=0

struct tongxun* tj() /*创建链表并添加成员*//**/

{

struct tongxun *head=NULL

struct tongxun *p,*q

system("CLS")

p=q=(struct tongxun *)malloc(sizeof(struct tongxun))

for()

{printf("请输入姓名:(如果输入0则退出添加)\n")

scanf("%s",p->name)

if(!(strcmp(p->name,"0"))){ free(p)return head}

else {printf("请输入电话号码:\n")

scanf("%s",p->number)

all++

if(all==1)

{p->next=head

q=p

head=q}

else

{p->next=NULL

q->next=p

q=p

}

p=(struct tongxun *)malloc(sizeof(struct tongxun))

}

}

}

cz(struct tongxun *head) /*查找函数*/

{char name1[20],*a

struct tongxun *p

p=heada=name1

system("CLS")

printf("请输入要查找的姓名:\n")

scanf("%s",a)

while(p!=NULL)

{if((strcmp(p->name,a))==0) {printf("姓名:%s\n电话号码:%s\n",p->name,p->number)return}

else p=p->next

}

printf("没有此人\n")

return

}

insert(struct tongxun *head) /*插入新成员*/

{struct tongxun* pnew

pnew=(struct tongxun *)malloc(sizeof(struct tongxun))

while(1)

{printf("请输入姓名:(如果输入0则退出添加)\n")

scanf("%s",pnew->name)

if(!(strcmp(pnew->name,"0"))){ free(pnew)return head}

else {printf("请输入电话号码:\n")

scanf("%s",pnew->number)

all++

pnew->next=head

head=pnew

pnew=(struct tongxun *)malloc(sizeof(struct tongxun))

}

}

}

shuchu(struct tongxun *head)/*输出成员*/

{struct tongxun *p

p=head

printf("这里一共有%d个成员\n",all)

while(p!=NULL)

{printf("姓名:%s\n电话号码:%s\n",p->name,p->number)

p=p->next

}

}

xg(struct tongxun *head) /*修改成员*/

{char name1[20],*a

struct tongxun *p

p=heada=name1

system("CLS")

printf("请输入要修改的姓名:\n")

scanf("%s",a)

while(p!=NULL)

{if((strcmp(p->name,a))==0) {printf("请重新输入姓名:\n")

scanf("%s",p->name)

printf("请重新输入电话号码:\n")

scanf("%s",p->number)return}

else p=p->next

}

printf("没有此人\n")

return

}

sc(struct tongxun *head) /*删除成员*/

{char name1[20],*a

struct tongxun *p,*q

p=q=heada=name1

system("CLS")

printf("请输入要删除的姓名:\n")

scanf("%s",a)

while(p!=NULL)

{

if((strcmp(p->name,a))==0) {all--q->next=p->nextreturn}

else {q=pp=p->next}

}

printf("没有此人\n")

return

}

void main()

{struct tongxun *headint i

while(1)

{printf("请选择:\n")

printf("1.添加 2.查找 3.修改 4.删除5.插入 6.输出\n")scanf("%d",&i)

switch(i)

{case 1:head=tj()break

case 2:cz(head)break

case 3:xg(head)break

case 4:sc(head)break

case 5:insert(head)break

case 6:shuchu(head)break

default:printf("输入有误,请重新输入:\n")break

}

}

}

/*编程建立一通讯簿,存放有姓名、电话号码、住址,然后对通信簿进行查找、添加、修改及删除。*/

#include<stdio.h>

struct person

{

char name[8]

char tel[15]

char addr[50]

}

char filename[20]

FILE *fp

void creat()

void output()

void search()

void append()

void modify()

void delete()

main()

{

int m

creat()

while(1)

{

printf("\n\n添加,请按1")

printf("\n查找,请按2")

printf("\n修改,请按3")

printf("\n删除,请按4")

printf("\n输出,请按5")

printf("\n退出,请按0\n")

scanf("%d",&m)

if(m>=0&&m<=5)

{

switch(m)

{

case 1: append()

break

case 2: search()

break

case 3: modify()

break

case 4: delete()

break

case 5: output()

break

case 0: exit()

}

printf("\n\n *** 作完毕,请再次选择!")

}

else

printf("\n\n选择错误,请再次选择!")

}

}

void creat()

{

struct person one

long s1

printf("\n请输入通讯簿名:")

scanf("%s",filename)

if((fp=fopen(filename,"w"))==NULL)

{

printf("\n不能建立通讯簿!")

exit()

}

fprintf(fp,"%-10s%-20s%-50s\n","姓名","电话号码","住址")

printf("\n请输入姓名、电话号码及住址(以0结束)\n")

scanf("%s",one.name)

while(strcmp(one.name,"0"))

{

scanf("%s%s",one.tel,one.addr)

fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr)

scanf("%s",one.name)

}

fclose(fp)

}

void output()

{

struct person one

if((fp=fopen(filename,"r"))==NULL)

{

printf("\n不能打开通讯簿!")

exit()

}

printf("\n\n%20s\n","通 讯 簿")

while(!feof(fp))

{

fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr)

printf("%-10s%-20s%-50s",one.name,one.tel,one.addr)

}

fclose(fp)

}

void append()

{

struct person one

if((fp=fopen(filename,"a"))==NULL)

{

printf("\n不能打开通讯簿!")

exit()

}

printf("\n请输入添加的姓名、电话号码及住址\n")

scanf("%s%s%s",one.name,one.tel,one.addr)

fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr)

fclose(fp)

}

void search()

{

int k=0

char namekey[8]

struct person one

printf("\n请输入姓名:")

scanf("%s",namekey)

if((fp=fopen(filename,"rb"))==NULL)

{

printf("\n不能打开通讯簿!")

exit()

}

while(!feof(fp))

{

fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr)

if(!strcmp(namekey,one.name))

{

printf("\n\n已查到,记录为:")

printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr)

k=1

}

}

if(!k)

printf("\n\n对不起,通讯簿中没有此人的记录。")

fclose(fp)

}

void modify()

{

int m,k=0

long offset

char namekey[8]

struct person one

printf("\n请输入姓名:")

scanf("%s",namekey)

if((fp=fopen(filename,"r+"))==NULL)

{

printf("\n不能打开通讯簿!")

exit()

}

while(!feof(fp))

{

offset=ftell(fp)

fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr)

if(!strcmp(namekey,one.name))

{

k=1

break

}

}

if(k)

{

printf("\n已查到,记录为:")

printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr)

printf("\n请输入新姓名、电话号码及住址:")

scanf("%s%s%s",one.name,one.tel,one.addr)

fseek(fp,offset,SEEK_SET)

printf("%ld",ftell(fp))

fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr)

}

else

printf("\n对不起,通讯簿中没有此人的记录。")

fclose(fp)

}

void delete()

{

int m,k=0,flag

long offset1,offset2

char namekey[8], valid[4]

struct person one

printf("\n请输入姓名:")

scanf("%s",namekey)

if((fp=fopen(filename,"r+"))==NULL)

{

printf("\n不能打开通讯簿!")

exit()

}

while(!feof(fp))

{

offset1=ftell(fp)

fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr)

if(!strcmp(namekey,one.name))

{

k=1

break

}

}

if(k)

{

printf("\n已查到,记录为")

printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr)

printf("\n确实要删除,按1;不删除,按0:")

scanf("%d",&m)

if(m)

{

fseek(fp,offset1,SEEK_SET)

fprintf(fp,"%-10s%-20s%-50s\n","","","")

}

}

else

printf("\n对不起,通讯簿中没有此人的记录。")

fclose(fp)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11605048.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存