通讯录要求:
通讯录说明文档(一)
语言:c语言
格式: 编号 姓 名 住址 电话
201701 *** xxxx 183****5668
要求:使用结构体形式对数据存储
功能:使用链表实现增加(在增加人员的过程中有一个自动排序功能,比如按姓名排序)、删除、修改、查找(比如:工号查找、电话查找)的功能;
(1)添加用户信息(号码长度 号码是否重复)
(2)列出好友信息(按姓名排序)
(3)查找好友信息(按姓名查找)
(4)删除好友
(5)退出
注意事项:在增、删、改、查过程中,如果姓名相同怎么进行选择操作。
头文件
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
struct node
{
char number[10];
char name[10];
char address[10];
char telephone[11];
struct node *next;
};
typedef struct node Node;
typedef Node *LinkList;
void ListInit(LinkList *L);
void ListInsert(LinkList *L);
void ListTraverse(LinkList L);
void ListDelete(LinkList L);
#endif
主函数
#include
#include
#include
#include"LinkList.h"
#include
#include
#include
#include
int main()
{
FILE *fp;
int t=0;
int ret;
LinkList list;
ListInit(&list);
LinkList p;
p = (LinkList)malloc(sizeof(Node));
list = p;
p->next = NULL;
fp = fopen("record.txt","a+"); //增加了用文件流的方式保存信息的功能
while(!feof(fp))
{
if(fread(p,sizeof(Node),1,fp)!=1)
break;
p->next = (LinkList)malloc(sizeof(Node));
p = p->next;
}
fclose(fp);
do
{
printf("-----------------------------------\n");
printf("| 1. add user information |\n");
printf("-----------------------------------\n");
printf("| 2. list friend information |\n ");
printf("-----------------------------------\n");
printf("| 3. find friend information |\n");
printf("-----------------------------------\n");
printf("| 4. delete friend information |\n");
printf("-----------------------------------\n");
printf("| 5. exit |\n");
printf("-----------------------------------\n");
printf("what you want to do:");
int a;
scanf("%d",&a);
switch(a)
{
case 1:
ListInsert(&list);
break;
case 2:
ListTraverse(list);
break;
case 3:
GetElem(list);
break;
case 4:
ListDelete(list);
break;
case 5:
fp = fopen("record.txt","w+");
while(list)
{
fwrite(list,sizeof(Node),1,fp);
list=list->next;
}
fclose(fp);
t=1;
break;
default:
return;
}
}while(!t);
return 0;
}
调用的功能函数
#include
#include
#include"LinkList.h"
#include
void ListInit(LinkList *L) //初始化
{
(*L)=(LinkList)malloc(sizeof(Node));
if(NULL==(*L))
{
printf("Init fail\n");
}
(*L)->next=NULL;
}
void ListInsert(LinkList *L) //添加用户信息
{
char nu[10],na[10],ad[20],tel[11];
LinkList p=*L;
LinkList k=*L;
LinkList n=(LinkList)malloc(sizeof(Node));
printf("Please input ID:\n");
scanf("%s",nu);
LinkList temp = p->next;
while(k) //查看编号是否已被注册
{
if((strcmp(k->number,nu)!=0))
{
k=k->next;
}
else
{
printf("this number has been registered\n");
return;
}
}
strcpy(n->number,nu);
printf("Please input name:\n");
scanf("%s",&na);
if(p->next==NULL) //再插入名字的时候顺便进行排序
{
n->next=p->next;
p->next=n;
}
else
{
if((strcmp(p->next->name,na)>=0))
{
n->next=p->next;
p->next=n;
}
else
{
while(strcmp(p->next->name,na)<0)
{
p=p->next;
if(p->next == NULL)
break;
}
if(p->next!=NULL)
{
n->next=p->next;
p->next=n;
}
if(p->next==NULL)
{
n->next=p->next;
p->next=n;
}
}
}
strcpy(n->name,na);
printf("Please input address:\n");
scanf("%s",&ad);
strcpy(n->address,ad);
printf("Please input telephone:\n");
scanf("%s",&tel);
int len;
while((len=strlen(tel)) != 11) //判断电话号码是否为11位
{
printf("Please input right telephone!\n");
scanf("%s",tel);
}
strcpy(n->telephone,tel);
printf("add user success\n");
}
void ListTraverse(LinkList L) //查看用户信息
{
LinkList p;
if(L->next == NULL)
{
printf("this no information\n");
}
p=L->next;
printf("number name address telephone\n");
while(p)
{
printf("%2s %5s %5s %10s\n",p->number,p->name,p->address,p->telephone);
p=p->next;
}
}
void GetElem(LinkList L) //查找你想要找的用户姓名,显示所有信息
{
LinkList p = L->next;
char findname[10];
printf("who you what to find:\n");
scanf("%s",findname);
printf(" number name address telephone\n");
while(p!=NULL)
{
if(strcmp(p->name,findname)==0)
{
printf(" %s %s %s %s\n",p->number,p->name,p->address,p->telephone);
break;
}
p=p->next;
}
if(p==NULL)
printf("can not find \n");
}
void ListDelete(LinkList L) //删除用户信息
{
char findnumber[10];
LinkList p = L;
LinkList k;
printf("which one do you want to delete:\n");
scanf("%s",findnumber);
while((strcmp(p->next->number,findnumber)!=0))
{
p=p->next;
if(p->next==NULL)
{
printf("can not find\n");
break;
}
}
if(strcmp(p->next->number,findnumber)==0)
{
k=p->next;
p->next=k->next;
free(k);
}
printf("The user has been delete!\n");
}