编号 | 姓 名 | 住址 | 电话 | 住宅电话 |
---|---|---|---|---|
201701 | ** | xxxx | 183****5668 | ** |
要求:使用结构体形式对数据存储
功能:使用链表实现增加(在增加人员的过程中有一个自动排序功能,比如按姓名排序)、删除、修改、查找(比如:工号查找、电话查找)的功能;
(1)添加用户信息(号码长度 号码是否重复)
(2)列出好友信息(按姓名排序)
(3)查找好友信息(按姓名查找)
(4)删除好友
(5)退出
(6)退出后自动保存记录,下次使用时自动加载
#ifndef _ADDRESSBOOK_H_
#define _ADDRESSBOOK_H_
struct Addressbook
{
char name[10];
char id[20];
char address[20];
char tele[20];
char mobile[20];
struct Addressbook *next;
};
typedef struct Addressbook addressbook;
typedef addressbook *LinkList;
void ListInit(LinkList *l);
int InsertInfo(LinkList *l);
int ShowInfo(LinkList l);
int SearchInfo(LinkList l);
int DeleteInfo(LinkList l);
int ChangeInfo(LinkList *l);
int SaveInfo(LinkList l);
int LoadInfo(LinkList *l);
#endif
#include<stdio.h>
#include<stdlib.h>
#include"addressbook.h"
void show()
{
system("clear");
printf("************************************\n");
printf("************************************\n");
printf("********Welcome AddressBook!********\n");
printf("************************************\n");
printf("************************************\n");
sleep(1);
system("clear");
}
void PrintInfo()
{
printf("************************************\n");
printf("***1.添加好友信息 2.显示好友信息***\n");
printf("***3.查找好友信息 4.删除好友信息****\n");
printf("***5.修改好友信息 6.退出************\n");
printf("************************************\n\n");
printf("***********请输入你的选择***********\n");
}
int main()
{
LinkList addbook;
int choice;
show();
ListInit(&addbook);
LoadInfo(&addbook);
while(1)
{
PrintInfo();
scanf("%d",&choice);
switch(choice)
{
case 1:
InsertInfo(&addbook);
break;
case 2:
ShowInfo(addbook);
break;
case 3:
SearchInfo(addbook);
break;
case 4:
DeleteInfo(addbook);
break;
case 5:
ChangeInfo(&addbook);
break;
case 6:
SaveInfo(addbook);
break;
default:
printf("无效操作!\n");
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"addressbook.h"
void ListInit(LinkList *l)
{
(*l) = (LinkList)malloc(sizeof(addressbook));
(*l)->next = NULL;
}
int InsertInfo(LinkList *l)
{
LinkList p = *l;
LinkList s;
printf("请输入好友信息:\n");
s = (LinkList)malloc(sizeof(addressbook));
scanf("%s%s%s%s%s",s->name,s->id,s->mobile,s->tele,s->address);
if(strlen(s->mobile) != 11)
{
printf("无效手机号码!\n");
return 0;
}
if(strlen(s->tele) != 8)
{
printf("无效家庭电话号码!\n");
return 0;
}
while(p->next)
{
if(strcmp((p->next)->mobile,s->mobile) == 0)
{
printf("已存在相同手机号码!\n");
return 0;
}
p = p->next;
}
p=*l;
if(p->next ==NULL)
{
s->next = p->next;
p->next = s;
}
else
{
while(p->next)
{
if(strcmp((p->next)->name,s->name)>0)
{
s->next = p->next;
p->next = s;
break;
}
p = p->next;
}
if(p->next ==NULL)
{
s->next = p->next;
p->next = s;
}
}
}
int ShowInfo(LinkList l)
{
if(l->next == NULL)
{
printf("通讯录为空!\n");
}
else
{
LinkList p = l->next;
while(p)
{
printf("Name: %s Id: %s Mobile: %s Tele: %s Address: %s\n",p->name,p->id,p->mobile,p->tele,p->address);
p=p->next;
}
}
}
int SearchInfo(LinkList l)
{
LinkList p = l->next;
char n[10];
printf("请输入您想要查找的好友名字:\n");
scanf("%s",n);
while(p)
{
if(strcmp(p->name,n) == 0)
{
printf("ID: %s Tele: %s Mobile: %s Address: %s\n",p->id,p->tele,p->mobile,p->address);
break;
}
else
{
p =p->next;
}
}
if(p == NULL)
{
printf("查无此人!\n");
}
}
int DeleteInfo(LinkList l)
{
LinkList p = l;
LinkList tmp;
char n[10];
printf("请输入你想删除的好友名字:\n");
scanf("%s",n);
while(p->next)
{
if(strcmp((p->next)->name,n) == 0)
{
tmp = p->next;
p->next = tmp->next;
free(tmp);
printf("删除好友信息成功!\n");
return 0;
}
else
{
p = p->next;
}
}
if(p->next == NULL)
{
printf("查无此人!\n");
}
}
int ChangeInfo(LinkList *l)
{
LinkList p = (*l)->next;
LinkList t = *l;
LinkList s ;
char n[10];
printf("请输入您想修改的好友姓名:\n");
scanf("%s",n);
while(p)
{
s = (LinkList)malloc(sizeof(addressbook));
if(strcmp(p->name,n) == 0)
{
printf("请输入您要修改的信息:姓名 编号 电话 家庭电话 住址\n");
scanf("%s%s%s%s%s",s->name,s->id,s->mobile,s->tele,s->address);
if(strlen(s->mobile) != 11)
{
printf("无效手机号码!\n");
return 0;
}
if(strlen(s->tele) != 8)
{
printf("无效家庭电话号码!\n");
return 0;
}
while(t->next)
{
if(strcmp((t->next)->mobile,s->mobile) == 0)
{
printf("已存在相同手机号码!\n");
return 0;
}
t = t->next;
}
strcpy(p->name , s->name);
strcpy(p->id , s->id);
strcpy(p->mobile , s->mobile);
strcpy(p->tele , s->tele);
strcpy(p->address , s->address);
free(s);
printf("修改成功!\n");
break;
}
else
{
p = p->next;
}
}
if(p == NULL)
{
printf("查无此人!\n");
}
}
int SaveInfo(LinkList l)
{
int ret;
LinkList p = l->next;
LinkList tmp;
tmp = (LinkList)malloc(sizeof(addressbook));
FILE *fp = fopen("addbook.txt", "w");
if(fp == NULL)
{
perror("fopen");
exit(1);
}
while(p)
{
strcpy(tmp->name, p->name);
strcpy(tmp->id, p->id);
strcpy(tmp->address, p->address);
strcpy(tmp->tele, p->tele);
strcpy(tmp->mobile, p->mobile);
ret = fwrite(tmp, 1, sizeof(addressbook), fp);
if(ret == 0)
{
perror("fwrite");
exit(1);
}
p = p->next;
}
free(tmp);
fclose(fp);
exit(1);
}
int LoadInfo(LinkList *l)
{
int ret;
LinkList p = *l;
LinkList tmp;
FILE *fp = fopen("addbook.txt", "a+b");
if(fp == NULL)
{
perror("fopen");
return 0;
}
tmp = (LinkList)malloc(sizeof(addressbook));
while(ret = fread(tmp, 1, sizeof(addressbook), fp))
{
tmp->next = p->next;
p->next = tmp;
p = p->next;
tmp = (LinkList)malloc(sizeof(addressbook));
}
fclose(fp);
return 0;
}