学习C语言阶段写的一个程序,实现了一个类似手机通讯录的东西,有添加好友,列表信息,搜索好友,删除好友等功能。添加好友或删除好友时会对配置文件进行相应操作,程序停止后,再次运行时,先从配置文件中读取信息,搭建链表,上次的信息便不会丢失。
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
#define TRUE 1
#define FALSE 0
typedef struct
{
int id;
char name[10];
char pnum[20]; //电话号码
char addr[20]; //家庭住址
char cnum[20]; //公司电话
}Linkdata;
typedef struct _node
{
Linkdata data;
struct _node *next;
}Node;
void menu(); //菜单
Node* create_list(); //创建链表
int Insert(Node *head); //添加好友
int load (Node *head); //加载配置文件
int sort (Node *head); //排序
void search(Node *head); //搜索好友
int Delete (Node *head); //删除好友
void Display(Node *head); //显示好友列表
Node* Find_element(Node* h, char name[]);
void Display_pos(Node *h);
int Get_len(Node * head);
#endif
#include <stdio.h>
#include <stdlib.h>
#include "Linklist.h"
void menu()
{
printf (" * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf (" * *\n");
printf (" * * 电子通讯录 * *\n");
printf (" * *\n");
printf (" * *\n");
printf (" * 1)添加好友信息 *\n");
printf (" * *\n");
printf (" * 2)列表好友信息 *\n");
printf (" * *\n");
printf (" * 3)搜索好友 *\n");
printf (" * *\n");
printf (" * 4)删除好友 *\n");
printf (" * *\n");
printf (" * 5)退出 *\n");
printf (" * *\n");
printf (" * *\n");
printf (" * MADE BY:张建 *\n");
printf (" * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
}
int load (Node *head)
{
FILE *fp = fopen ("peizhi", "r+");
if (fp == NULL)
{
perror ("fopen");
return -1;
}
Linkdata buf[10] = {0};
int ret;
while (ret = fread (buf, sizeof(Linkdata), 1, fp))
{
Insert_pos(head, buf);
}
if(ret == 0 && !feof(fp))
{
perror ("fread");
return -1;
}
fclose (fp);
sort (head);
return TRUE;
}
void Display_label ()
{
printf ("ID ");
printf (" 姓名 ");
printf (" 手机号码 ");
printf (" 家庭住址 ");
printf (" 公司电话 ");
printf ("\n");
}
void Display (Node *head)
{
if (head == NULL)
return;
sort (head);
Node *tmp = head->next;
while(tmp)
{
printf ("%d ", tmp->data.id);
printf ("%-12s ", tmp->data.name);
printf ("%-11s ", tmp->data.pnum);
printf ("%-20s", tmp->data.addr);
printf ("%-11s", tmp->data.cnum);
printf ("\n");
tmp = tmp->next;
}
}
Node* create_list()
{
Node *head = (Node *)malloc(sizeof(Node)/sizeof(char));
if (head == NULL)
{
printf ("创建失败\n");
return NULL;
}
head->next = NULL;
return head;
}
int sort (Node *head)
{
Node *tmp_pre = head;
Node *tmp = head->next;
while (tmp->next)
{
Node *cur = tmp->next;
while (cur)
{
if (tmp->data.id > cur->data.id)
{
Linkdata temp = tmp->data;
tmp->data = cur->data;
cur->data = temp;
}
cur = cur->next;
}
tmp = tmp->next;
}
return TRUE;
}
int refile(int id)
{
FILE *fp = fopen("peizhi", "r+");
if (fp == NULL)
{
perror ("fopen");
return -1;
}
FILE *fp1 = fopen("bak", "w+");
if (fp1 == NULL)
{
perror ("fopen");
return -1;
}
Linkdata buf[10] = {0};
// 拷贝一个备份
int ret;
while (ret = fread(buf, sizeof(Linkdata), 1, fp))
{
fwrite(buf, sizeof(Linkdata), ret, fp1);
}
if(ret == 0 && !feof(fp))
{
perror ("fread");
return -1;
}
fclose(fp);
fclose(fp1);
fp = fopen("peizhi", "w+");
fp1 = fopen("bak", "r+");
//删除信息,更新文件
while (ret = fread(buf, sizeof(Linkdata), 1, fp1))
{
if (buf[0].id == id)
continue;
fwrite(buf, sizeof(Linkdata), ret, fp);
}
if(ret == 0 && !feof(fp1))
{
perror ("fread");
return -1;
}
fclose(fp);
fclose(fp1);
return 0;
}
int Insert_pos(Node *head, Linkdata buf[])
{
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
return FALSE;
node->data = buf[0];
node->next = head->next;
head->next = node;
return TRUE;
}
int Insert(Node *head)
{
if (head == NULL)
return FALSE;
FILE *p = fopen ("peizhi", "a+");
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
return FALSE;
Linkdata data;
Linkdata buf[10] = {0};
printf ("请输入好友信息\n");
printf ("ID号 :");
scanf ("%d", &data.id);
printf ("姓名 :");
scanf ("%s", data.name);
printf ("手机号码 :");
scanf ("%s", data.pnum);
printf ("家庭住址 :");
scanf ("%s", data.addr);
printf ("公司电话 :");
scanf ("%s", data.cnum);
node->data = data;
node->next = head->next;
head->next = node;
if (node->data.id==0 || node->data.name==NULL || node->data.pnum==NULL
|| node->data.addr==NULL || node->data.cnum==NULL)
{
printf ("添加失败\n");
}
else
{
printf ("添加成功\n");
buf[0] = data;
fwrite (buf, sizeof(Linkdata), 1, p);
}
fclose (p);
sort (head);
return TRUE;
}
Node* Find_element(Node* h, char name[])
{
if (h == NULL)
return FALSE;
Node *tmp = h->next;
while (tmp)
{
if (strcmp(tmp->data.name, name) == 0)
{
break;
}
tmp = tmp->next;
}
if (tmp == NULL)
return FALSE;
return tmp;
}
int Delete_pos(Node* head, int pos)
{
if (head == NULL || pos < 1)
return FALSE;
Node *tmp = head;
int i;
for (i = 0; i < pos - 1; i++)
{
if (tmp == NULL)
break;
tmp = tmp->next;
}
if (tmp == NULL)
{
printf ("删除位置越界\n");
return FALSE;
}
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;
}
int count_num(Node *head, char *name)
{
if (head == NULL)
return FALSE;
int count = 0;
Node *tmp = head->next;
while (tmp)
{
if (strcmp((tmp->data).name, name) == 0)
{
count++;
}
tmp = tmp->next;
}
return count;
}
void search(Node *head)
{
int x = -1 ;
int count = 0;
char name[20];
Node* h = (Node *)malloc(sizeof(Node)/sizeof(char));
h = head;
printf ("请输入要搜索的好友姓名:");
scanf ("%s", name);
count = count_num(head, name);
if (count == 0)
printf ("未搜索到该好友\n");
else
printf ("有%d个同名好友:\n", count);
int i;
Display_label ();
for (i = 0; i < count; i++)
{
h = Find_element(h, name);
Display_pos(h);
}
}
int Delete (Node *head)
{
int x = -1 ;
int count = 0;
char name[20];
Node* h = head;
printf ("请输入要删除的好友姓名:");
scanf ("%s", name);
count = count_num(head, name);
if (count == 0)
printf ("该好友不存在\n");
else if (count == 1)
{
Node *tmp = head;
while (tmp)
{
if (strcmp(tmp->next->data.name, name) == 0)
{
break;
}
tmp = tmp->next;
}
if (tmp == NULL)
return FALSE;
Node *p = tmp->next;
tmp->next = p->next;
refile (p->data.id);
free(p);
printf ("删除成功\n");
return TRUE;
}
else
{
printf ("有%d个同名好友:\n", count);
int i;
Display_label ();
for (i = 0; i < count; i++)
{
h = Find_element(h, name);
Display_pos(h);
}
printf ("请输入ID号指定删除好友:");
int id;
scanf ("%d",&id);
Node *tmp = head;
while (tmp)
{
if (tmp->next->data.id == id)
{
break;
}
tmp = tmp->next;
}
if (tmp == NULL)
return FALSE;
Node *p = tmp->next;
tmp->next = p->next;
free(p);
printf ("删除成功\n");
refile (id);
return TRUE;
}
}
void Display_pos(Node *h)
{
if (h == NULL)
return;
//Node *tmp = head;
printf ("%d ", h->data.id);
printf ("%-12s ", h->data.name);
printf ("%-11s ", h->data.pnum);
printf ("%-20s", h->data.addr);
printf ("%-11s", h->data.cnum);
printf ("\n");
}
int Get_len(Node * head)
{
if (head == NULL)
return FALSE;
int len = 0;
Node *tmp = head->next;
while (tmp)
{
len++;
tmp = tmp->next;
}
return len;
}
#include <stdio.h>
#include "Linklist.h"
#include <stdlib.h>
int main()
{
Node *head = create_list();
if (head == NULL)
return FALSE;
Linkdata f1 = {5, "liyang", "13118012579", "江西九江", "123"};
Linkdata f2 = {4, "liyang", "13118016511", "江苏扬州", "456"};
Linkdata f3 = {3, "liyang", "13151589715", "江苏连云港", "789"};
Linkdata f4 = {2, "liyang", "13151589715", "江苏苏州", "749"};
Linkdata f5 = {1, "liyang", "13151589715", "江苏无锡", "759"};
Linkdata buf[10];
buf[0] = f1;
buf[1] = f2;
buf[2] = f3;
buf[3] = f4;
buf[4] = f5;
//往配置文件内写入内置联系人,方便测试功能,最好写入后将此部分注释,避免重复写入
FILE *fd = fopen ("peizhi", "w+");
fwrite (buf, sizeof(Linkdata), 1, fd);
fwrite (buf+1, sizeof(Linkdata), 1, fd);
fwrite (buf+2, sizeof(Linkdata), 1, fd);
fwrite (buf+3, sizeof(Linkdata), 1, fd);
fwrite (buf+4, sizeof(Linkdata), 1, fd);
fclose (fd);
load (head); //加载配置文件
char str[20];
while(1)
{
system ("clear");
menu();
printf ("请输入操作:");
scanf ("%s", str);
switch (str[0])
{
case '1':
Insert(head);
break;
case '2':
Display_label ();
Display(head);
break;
case '3':
search(head);
break;
case '4':
Delete(head);
break;
case '5':
return 0;
}
printf ("请按任意键返回菜单:");
getchar();
getchar();
printf ("\n");
continue;
}
return 0;
}