#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)
}
剽窃代码即可。。
已经按照你的要求做了一个,VC6上运行确认了:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
typedef struct PersonalInfo
{
char name[50]
char address[30]
char telno[30]
char postcode[30]
struct PersonalInfo *next
}INFO
INFO *head
void Initiate()
void Menu()
void Create()//的功能是:创建新的通讯录。
void Add()// 在通讯录的末尾,写入新的信息,并返回选单
void Find()//查找记录
void Alter()//修改记录如果未找到要修改的人,则提示通讯录中没有此人的信息,并返回选单。
void Delete()//删除某人的信息,如果未找到要删的人,提示通讯录中没有此人的信息,并返回选单。
void List()//的功能是:显示通讯录中的所有记录。
//初始化
void Initiate()
{
if((head=(INFO *)malloc(sizeof(INFO)))==NULL) exit(1)
head->next=NULL
}
//显示菜单
void Menu()
{
printf("**************欢迎使用通讯录系统**************")
printf("\n")
printf("\n")
printf(" 1.创建通讯录。\n")
printf(" 2.插入信息。\n")
printf(" 3.查询记录\n")
printf(" 4.修改记录\n")
printf(" 5.删除记录!\n")
printf(" 6.显示所有记录\n")
printf(" 0.退出通讯录\n")
printf(" 请输入0~6 \n")
}
//创建通讯录
void Create()
{
INFO *p1[100],*p2
int m,i
printf("请输入创建个数:")
scanf("%d",&m)
for(i=1i<=mi++)
{
p1[i]=(INFO *)malloc(sizeof(INFO))
printf("请输入第%d条信息!\n",i)
printf("姓名:\n")
scanf("%s",&p1[i]->name)
printf("地址:\n")
scanf("%s",&p1[i]->address)
printf("电话:\n")
scanf("%s",&p1[i]->telno)
printf("邮编:\n")
scanf("%s",&p1[i]->postcode)
p1[i]->next=NULL
if(head->next==NULL)
head->next=p1[i]
else
{
for(p2=headp2->next!=NULLp2=p2->next) //找到结点尾
p2->next=p1[i]
}
}
printf("信息已添加!\n")
return //保存到链表
}
//添加通讯录信息
void Add()
{
INFO *p,*q
if((q=(INFO *)malloc(sizeof(INFO)))==NULL) exit(1)
printf("请输入要添加的信息!\n")
printf("姓名:\n") //添加信息
scanf("%s",&q->name)
printf("性别:\n")
scanf("%s",q->address)
printf("电话:\n")
scanf("%s",q->telno)
printf("城市:\n")
scanf("%s",q->postcode)
for(p=headp->next!=NULLp=p->next)
p->next=q
q->next=NULL
printf("此信息已添加!")
return
}
//查找通讯录信息
void Find()
{
INFO *p
char name[50]
if(head->next==NULL)
{
printf("此通讯录为空!\n")
return
}
printf("请输入要查找的姓名:\n")
scanf("%s",&name)
for(p=head->nextp!=NULLp=p->next)
{
if(strcmp(p->name,name)==0)
{
printf("姓名\t地址\t邮编\t电话\n")
printf("%s\t%s\t%s\t%s\n",p->name,p->address,p->postcode,p->telno)
}
else if(p->next==NULL)
printf("无此信息!\n")
}
}
//修改通讯录信息
void Alter()
{
char name[50] //先查找 后删除
INFO *p, *p1
if (head->next == NULL)
{
printf("此通讯录为空!\n")
return
}
printf("请输入要修改的姓名:\n")
scanf("%s",name)
for(p=head->nextp!=NULLp=p->next)
{
if(strcmp(p->name,name)==0)
break
else if(p->next==NULL)
{
printf("无此信息!\n")
return
}
}
p1=(INFO *)malloc(sizeof(INFO))
printf("姓名:\n") //添加信息
scanf("%s",p1->name)
strcpy(p->name,p1->name)
printf("性别:\n")
scanf("%s",p1->address)
strcpy(p->address,p1->address)
printf("电话:\n")
scanf("%s",p1->telno)
strcpy(p->telno,p1->telno)
printf("城市:\n")
scanf("%s",p1->postcode)
strcpy(p->postcode,p1->postcode)
printf("此信息已修改!\n")
//显示修改的信息
printf("姓名\t地址\t邮编\t电话\n")
printf("%s\t%s\t%s\t%s\n",p->name,p->address,p->postcode,p->telno)
free(p1)
}
//删除通讯录信息
void Delete()
{
char name[50] //先查找 后删除
INFO *p = head->next, *p1 =head->next,*p2
if (head->next == NULL)
{
printf("此通讯录为空!\n")
return
}
printf("请输入要删除的姓名:\n")
scanf(" %s",name)
while ((strcmp(p->name,name)!=0 )&& p->next != NULL)
{
p1=p
p =p->next
}
if (strcmp(name, p->name)==0) //输出删除信息
{
if (p == head->next&&p->next!=NULL)
head->next = p->next
else if(p==head->next&&p->next==NULL)
{
head->next=p->next
printf("信息已删除,先此通讯录为空!!\n")
return
}
else
p1->next = p->next
}
else
{
printf("此信息不存在!!!\n")
return
}
printf("此信息已删除!")
printf("姓名\t地址\t邮编\t电话\n")
for(p2=head->nextp2!=NULLp2=p2->next)
printf("%s\t%s\t%s\t%s\n",p2->name,p2->address,p2->postcode,p2->telno)
}
//显示所有记录
void List()
{
INFO *p
if(head->next==NULL)
{
printf("此通讯录中无记录!\n")
return
}
printf("姓名\t地址\t邮编\t电话\n")
for(p=head->nextp!=NULLp=p->next)
printf("%s\t%s\t%s\t%s\n",p->name,p->address,p->postcode,p->telno)
}
void main()
{
int choice
char yes_no
system("color a")
Initiate()
do
{
Menu()
printf("请选择0-6的数字\n")
scanf("%d",&choice)
printf("\n")
switch(choice)
{
case 1:Create()
break
case 2:Add()
break
case 3:Find()
break
case 4:Alter()
break
case 5:Delete()
break
case 6:List()
break
case 0:
printf("************感谢您的使用************\n")
exit(0)
break
default:
printf("输入有误!请重新输入\n")
break
}
printf("是否继续 Y or N? \n")
do
{
scanf("%c",&yes_no)
}while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n')
}while(yes_no=='Y'||yes_no=='y')
}
# 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
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)