#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1024
struct node
{
int num ;
char name[N];
char telephone[N];
char address[N];
struct node * next;
};
typedef struct node Node;
typedef struct node * Link;
void creat_empty_link(Link *head)
{
*head = (Link)malloc(sizeof(Node));
(*head)->next = NULL;
}
void insert_node(Link *head,Link new_node)
{
Link temp = *head;
new_node = (Link)malloc(sizeof(Node));
if(new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
printf("请输入联系人id:");
scanf("%d",&new_node->num);
printf("请输入联系人姓名:");
scanf("%s",new_node->name);
printf("请输入联系人号码:");
scanf("%s",new_node->telephone);
printf("请输入联系人住址:");
scanf("%s",new_node->address);
if((*head)->next == NULL)
{
(*head)->next = new_node;
new_node->next = NULL;
return;
}
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = new_node;
new_node->next = NULL;
}
void del_node(Link *head)
{
char name[N];
Link temp;
Link p;
temp = *head;
p = temp;
temp = temp->next;
scanf("%s",name);
while(temp != NULL)
{
if((strcmp((temp->name),name)) == 0)
{
p->next = temp->next;
free(temp);
printf("删除成功!\n");
break;
}
else
{
p = temp;
temp = temp->next;
}
}
if(temp == NULL)
{
printf("无该联系人!\n");
return;
}
}
void display_node(Link * head)
{
Link temp;
temp = (*head)->next;
if((*head)->next == NULL)
{
printf("还未添加联系人!\n");
return ;
}
printf("序号 姓名 联系方式 家庭住址\n");
while(temp != NULL)
{
printf("%4d %5s %15s %15s\n",temp->num,temp->name,temp->telephone,temp->address);
temp = temp->next;
}
printf("\n");
}
void search(Link *head,char name[])
{
Link temp;
temp = *head;
while(temp != NULL)
{
if((strcmp((temp->name),name)) == 0)
{ printf("序号 姓名 联系方式 家庭住址\n");
printf("%4d %5s %15s %15s\n",temp->num,temp->name,temp->telephone,temp->address);
break;
}
else
{
temp = temp->next;
}
}
if(temp == NULL)
{
printf("查无此人!\n");
return;
}
}
void menu()
{
printf("*******欢迎使用通讯录*******\n");
printf("* *\n");
printf("*1. 添加联系人信息 *\n");
printf("*2. 显示联系人信息 *\n");
printf("*3. 删除联系人信息 *\n");
printf("*4. 修改联系人信息 *\n");
printf("*5. 查找联系人信息 *\n");
printf("*6. 联系人读入文件 *\n");
printf("*7. 显示文件联系人 *\n");
printf("*8. 退出通讯录 *\n");
printf("*********************\n");
printf("请选择您需要的操作(1~7):");
}
void save(Link *head)
{
FILE *fp = NULL;
Link temp = (*head)->next;
// int count = 0;
if((fp = fopen("book.txt","w+")) == NULL)
{
printf("cannot open file!\n");
exit(1);
}
while(temp != NULL)
{
fwrite(temp,sizeof(Node),1,fp);
temp = temp->next;
// count++;
}
// fwrite(&count,4,1,fp);
// fseek(fp,-4,SEEK_END);
fclose(fp);
}
void read(Link *head)
{
Link new_node = NULL;
FILE *fp;
Link temp = (*head);
if((fp = fopen("book.txt","r")) == NULL)
{
printf("open error!\n");
exit(1);
}
// fseek(fp,-4,SEEK_END);
// fread(&count,4,1,fp);
fseek(fp,0,SEEK_SET);
printf("No 姓名 联系方式 家庭住址\n");
while(!feof(fp))
{
new_node = (Link)malloc(sizeof(Node));
if(fread(new_node,sizeof(Node),1,fp) == 0)
{
break;
}
printf("%d %10s %10s %10s\n",new_node->num,new_node->name,new_node->telephone,new_node->address);
temp->next = new_node;
new_node = NULL;
temp = temp->next;
}
if(feof(fp))
{
free(new_node);
new_node = NULL;
}
fclose(fp);
}
void change(Link *head,Link new_node)
{
Link temp;
temp = *head;
int i;
char name[N];
char telephone[N];
char address[N];
printf("请输入您需要修改的联系人姓名:");
scanf("%s",name);
while(temp != NULL)
{
if((strcmp((temp->name),name)) == 0)
{
printf("请选择要修改的选项:\n");
printf("1.姓名\n");
printf("2.联系方式\n");
printf("3.家庭住址\n");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("请输入新的姓名:");
scanf("%s",name);
strcpy((temp->name),name);
}
break;
case 2:
{
printf("请输入新的联系方式:");
scanf("%s",telephone);
strcpy((temp->telephone),telephone);
}
break;
case 3:
{
printf("请输入新的家庭住址:");
scanf("%s",address);
strcpy((temp->address),address);
}
printf("修改成功!\n");
break;
default:
printf("输入有误!\n");
}
break;
}
else
temp = temp->next;
}
if(temp == NULL)
{
printf("无该联系人!\n");
}
}
int choose(Link *head,Link new_node)
{
Link temp;
int n;
int data;
int choice;
char name[N];
temp = *head;
scanf("%d",&n);
switch(n)
{
case 1:
{
insert_node(head,new_node);
printf("添加成功!\n");
for(data = 0;;data++)
{
printf("是否继续添加?(1/0):");
scanf("%d",&choice);
if(choice == 1)
{
insert_node(head ,new_node);
printf("添加成功!\n");
}
else
{
system("clear");
menu();
choose(head,new_node);
break;
}
}
}
break;
case 2:
{
display_node(head);
printf("\n");
printf("请选择您接下来的操作:");
choose(head,new_node);
}
break;
case 3:
{
if((*head)->next == NULL)
{
printf("无联系人可删除,请先添加!\n");
system("clear");
menu();
choose(head,new_node);
}
else
{
printf("请输入需要删除的联系人姓名:");
del_node(head);
printf("\n");
printf("请选择您接下来的操作:");
choose(head,new_node);
}
}
break;
case 4:
{
if((*head)->next == NULL)
{
printf("无联系人可修改,请先添加!\n");
system("clear");
menu();
}
else
{
change(head,new_node);
printf("\n");
printf("请选择接下来的操作:");
choose(head,new_node);
}
}
break;
case 5:
{
if((*head)->next == NULL)
{
printf("无联系人可查找,请先添加!\n");
printf("\n");
printf("请选择您接下来的操作:");
choose(head,new_node);
return;
}
printf("请输入要查找的联系人姓名:");
scanf("%s",name);
search(head,name);
printf("\n");
printf("请选择接下来的操作:");
choose(head,new_node);
}
case 6:
{
save(head);
printf("请选择接下来的操作:");
choose(head,new_node);
}
case 7:
{
read(head);
printf("请选择接下来的操作:");
choose(head,new_node);
}
case 8:
{
system("clear");
printf("*********************\n");
printf("* *\n");
printf("* 谢谢使用通讯录 *\n");
printf("* *\n");
printf("*********************\n");
}
break;
default:
{
printf("输入有误,请重新输入!\n");
choose(head,new_node);
}
break;
}
}
int main()
{
Link head = NULL;
Link new_node = NULL;
system("clear");
menu();
creat_empty_link(&head);
choose(&head,new_node);
return 0;
}
通讯录(文件版)
最新推荐文章于 2024-08-18 13:26:54 发布