#include
#include
#define FALSE 0
#define TRUE 1
typedef struct node_data
{
int id;
char name[20];
char phone [20];
char address[20];
char tel[20];
}NodeData;
typedef struct _node
{
NodeData bug;
struct _node * next;
}Node;
void menu(Node *s)
{
printf ("添加好友\n");
printf ("显示好友\n");
}
//添加好友
void newfriend (Node *s)
{
Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
return ;
system("clear");
node->next = NULL;
printf ("请输入好友id\n");
scanf ("%d" ,&(node->bug.id));
printf ("请输入好友姓名\n");
scanf ("%s" ,node->bug.name);
printf ("请输入好友电话号码\n");
scanf ("%s" ,node->bug.phone);
printf ("请输入好友家庭住址\n");
scanf ("%s" ,node->bug.address);
printf ("请输入好友公司号码\n");
scanf ("%s" ,node->bug.tel);
Node *tmp = s;
while (tmp->next)
{
tmp = tmp->next;
}
tmp->next = node;
}
//显示好友
//先求长度
void LookFriend (Node*s)
{
system("clear");
int count = 0;
Node *tmp = s;
while (tmp->next)
{
tmp = tmp->next;
count ++;
}
//排序
int i = 0;
int j = 0;
NodeData tmp1 ;
Node *p = s->next;
for(i = 0;i
{
p=s->next;
for(j =0;j
{
if(p->bug.id > p->next->bug.id)
{
tmp1 = p->bug;
p->bug = p->next->bug;
p->next->bug = tmp1;
}
p=p->next;
}
}
//输出
p=s->next;
while(p)
{
printf("好友id: %d\n",p->bug.id);
printf("好友姓名:%s\n",p->bug.name);
printf("好友电话号码:%s\n",p->bug.phone);
printf("好友家庭住址:%s\n",p->bug.address);
printf("好友公司号码:%s\n\n",p->bug.tel);
p=p->next;
}
}
//搜索好友
void FindFriend (Node* s)
{
char name[20];
printf("请输入你要搜索的好友名字\n");
scanf ("%s",name);
if (s->next = NULL)
{
printf ("对不起你的通讯录里没有好友\n");
return;
}
Node* tmp = s->next;
int flag = 0;
while (tmp)
{
if (strcmp(name,tmp->bug.name) ==0)
{
printf("好友id: %d\n",tmp->bug.id);
printf("好友姓名:%s\n",tmp->bug.name);
printf("好友电话号码:%s\n",tmp->bug.phone);
printf("好友家庭住址:%s\n",tmp->bug.address);
printf("好友公司号码:%s\n\n",tmp->bug.tel);
flag = 1;
}
tmp = tmp->next;
}
if (flag = 0)
printf ("没有找到此好友\n");
}
//声明
int Search (Node* s, char* name);
//删除只有一个姓名是相同的好友
void ShanChu (Node* s, char* name)
{
Node* tmp = s;
while (tmp->next)
{
if (strcmp(name,tmp->next->bug.name)==0)
{
Node* p = tmp->next;
tmp->next = tmp->next->next;
free (p);
break;
}
tmp = tmp->next;
}
}
//删除有多个姓名相同的好友的id
void ShanChu1 (Node* s,int id)
{
Node* tmp = s;
while (tmp->next)
{
if (id == tmp->next->bug.id )
{
Node* p = tmp->next;
tmp->next = p->next;
free (p);
break;
}
tmp = tmp->next;
}
}
int Search1(Node* s, char* name)
{
Node* tmp = s->next;
while (tmp)
{
if (strcmp(name,tmp->bug.name)==0)
{
printf("好友id: %d\n",tmp->bug.id);
printf("好友姓名:%s\n",tmp->bug.name);
printf("好友电话号码:%s\n",tmp->bug.phone);
printf("好友家庭住址:%s\n",tmp->bug.address);
printf("好友公司号码:%s\n\n",tmp->bug.tel);
}
tmp = tmp->next;
}
return;
}
//删除好友
void DeleteFriend (Node* s)
{
char name[20];
printf ("请输入你要删除的好友的姓名\n");
scanf ("%s",name);
int count = Search(s,name);
int id;
if (count >= 2)
{
Search1(s,name);
printf("请输入好友的id\n");
scanf ("%d",&id);
ShanChu1(s,id);
printf("删除成功\n");
}
else if(count = 1)
{
ShanChu (s,name);
printf("删除成功\n");
}
else if (count = 0)
{
printf("对不起没有找到此好友\n");
}
return;
}
//查找相同好友名字的个数
int Search (Node* s, char* name)
{
Node* tmp = s->next;
int flag = 0;
while(tmp)
{
if (strcmp(name,tmp->bug.name)==0)
{
flag += 1;
}
tmp = tmp->next;
}
return flag;
}
int main()
{
Node *s = (Node*)malloc(sizeof(Node)/sizeof(char));
if (s == NULL)
return FALSE;
s->next = NULL;
char b;
while(1)
{
system ("clear");
printf(" 学生通讯录管理系统\n");
printf(" 1、添加好友\n");
printf(" 2、显示好友\n");
printf(" 3、查找好友\n");
printf(" 4、删除好友\n");
printf(" 5、退出程序\n");
scanf ("%c",&b);
switch(b)
{
case '1':
{
newfriend(s);
getchar();
printf("请按任意键返回\n");
getchar();
break;
}
case '2':
{
LookFriend(s);
getchar();
printf("请按任意键返回\n");
getchar();
break;
}
case '3':
{
FindFriend(s);
getchar();
printf("请按任意键返回\n");
getchar();
break;
}
case '4':
{
DeleteFriend(s);
getchar();
printf("请按任意键返回\n");
getchar();
break;
}
case '5':
{
exit(0);
}
}
}
return 0;
}