学C语言快一个月了,第一次做项目,中间出现了许多BUG,终于勉勉强强实现了预期的所以功能!
其中还存在一些BUG(如:输入字符程序崩溃等等),后期完成阶段学习会回来完善代码。
完善后的通讯录请转到文件系统编程的应用
AddressList.c 文件:
#include <stdio.h>
#include <stdlib.h>
#include "AddressList.h"
#include <string.h>
//主菜单
int Interface_Display()
{
system ("clear");
printf ("\t*****************************************\n");
printf ("\t~ 欢迎使用通讯录 ~\n");
printf ("\t~ ~\n");
printf ("\t~ 1 >>>>>>>>> 添加好友信息 ~\n");
printf ("\t~ 2 >>>>>>>>> 列表好友信息 ~\n");
printf ("\t~ 3 >>>>>>>>> 搜索好友 ~\n");
printf ("\t~ 4 >>>>>>>>> 删除好友 ~\n");
printf ("\t~ 5 >>>>>>>>> 退出 ~\n");
printf ("\t~ ~\n");
printf ("\t~ ~\n");
printf ("\t~ ~\n");
printf ("\t*****************************************\n");
printf (" \n");
printf (" \n");
printf ("\t请输入对应数字选择相应功能:");
}
//创建链表
Node * Create_List()
{
Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));
if (list == NULL)
return NULL;
list->next = NULL; //空表
return list;
}
// 添加好友
int Add_Friend(Node *h)
{
if(h == NULL)
return FALSE;
Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
if(node == NULL)
{
return FALSE;
}
system("clear");
printf ("\t请输入好友的ID:");
scanf ("%d", &(node->data.id));
printf ("\n");
printf ("\t请输入好友的姓名:");
scanf ("%s", node->data.name);
printf ("\n");
printf ("\t请输入好友的手机号码:");
scanf ("%s", node->data.tel);
printf ("\n");
printf ("\t请输入好友的家庭地址:");
scanf ("%s", node->data.address);
printf ("\n");
printf ("\t请输入好友的公司电话:");
scanf ("%s", node->data.officetel);
printf ("\n");
node->next = NULL;
printf("\t添加成功!\n");
Node *tmp = h;
while(tmp->next)
{
tmp = tmp->next;
}
tmp->next = node;
return TRUE;
}
//通讯录排序
int Order_Len(Node *h, int *len)
{
if (h == NULL)
{
return FALSE;
}
if (h->next == NULL)
{
printf ("通讯录为空\n");
return TRUE;
}
Node *tmp = (Node*)malloc(sizeof(Node)/sizeof(char));
if (tmp == NULL)
{
return FALSE;
}
int i,j;
for(i = 0; i < (*len)-1; i++)
{
Node *min = h->next;
Node *max = h->next->next;
for(j = 0; j < (*len)-i-1; j++)
{
if(min->data.id > max->data.id)
{
tmp->data = min->data;
min->data = max->data;
max->data = tmp->data;
}
min = min->next;
max = max->next;
}
}
free(tmp);
return TRUE;
}
//输出列表好友
int Display(Node *h)
{
if(h == NULL)
return;
Node *tmp = h->next;
while(tmp)
{
printf ("\t%d", tmp->data.id);
printf ("\t%s", tmp->data.name);
printf ("\t%s", tmp->data.tel);
printf ("\t%s", tmp->data.address);
printf ("\t%s", tmp->data.officetel);
printf("\n");
tmp = tmp->next;
}
printf("\n");
}
//查找好友
int Find_Friend(Node *h, char *name)
{
if (h == NULL)
return FALSE;
Node *tmp;
int k = 0;
for(tmp = h->next; tmp != NULL; tmp = tmp->next)
{
if (strcmp(tmp->data.name, name) == 0)
{
k++;
printf ("\t查询的好友信息:\n\tID: %d\n \t姓名: %s\n \t手机号码: %s\n \t家庭地址: %s\n \t公司电话: %s\n", tmp->data.id, tmp->data.name, tmp->data.tel, tmp->data.address, tmp->data.officetel);
}
printf ("\n");
}
if (k == 0)
{
printf ("\t您的通讯录中没有该好友!\n");
}
return k;
}
//删除指定数据
int Delete_Frined(Node *h, char *name)
{
if (h == NULL)
return FALSE;
Node *tmp = h;
while (tmp->next)
{
if (strcmp(tmp->next->data.name, name) == 0)
{
break;
}
tmp = tmp->next;
}
if (tmp->next == NULL)
return FALSE;
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;
}
//通过 id 删除指定数据
int Delete_Id(Node *h, int id, char *name)
{
if (h == NULL)
return FALSE;
Node *tmp = h;
while (tmp->next)
{
if (strcmp(tmp->next->data.name, name) == 0 && tmp->next->data.id == id)
{
break;
}
tmp = tmp->next;
}
if (tmp->next == NULL)
return FALSE;
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;
}
//获取链表长度
int Get_Len(Node *h)
{
if (h == NULL)
return 0;
Node *tmp = h;
int count = 0;
while (tmp->next)
{
count++;
tmp = tmp->next;
}
return count;
}