#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#define FILENAME "ADDRESS_LIST"
enum INPUT
{
EXIT,
ADD,
SHOW,
SEARCH,
DELETE,
REVISE,
SORT,
INIT
};
typedef struct PEOPLE
{
char name[20];
int old;
char sex[5];
char adress[20];
char phone_code[15];
} PEOPLE;
typedef struct NODE
{
PEOPLE people;
struct NODE*next;
}NODE;
void Add_people(NODE*);
void Show_people(NODE*);
void Search_people(NODE*);
void Delete_people(NODE*);
void Revise_people(NODE*);
void Sort_people(NODE*);
void Init_people(NODE*);
void Save_people(NODE*);
void Read_people(NODE**);
#include"head.h"
void Add_people(NODE* tail)
{
static NODE *temp_head = NULL;
temp_head = tail;
int in;
do
{
temp_head->next= malloc(sizeof(NODE));
if (temp_head->next == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
temp_head->next->next = NULL;
printf("输入联系人姓名:\n");
scanf("%s", temp_head->next->people.name);
printf("输入联系人性别:\n");
scanf("%s", temp_head->next->people.sex);
printf("输入联系人年龄:\n");
scanf("%d", &temp_head->next->people.old);
printf("输入联系人住址:\n");
scanf("%s", temp_head->next->people.adress);
printf("输入联系人电话:\n");
scanf("%s", temp_head->next->people.phone_code);
printf("添加成功\n");
temp_head = temp_head->next;
while (1)
{
printf("1:继续添加 0:返回主菜单\n");
scanf("%d", &in);
if (in == 1 || in == 0)
{
break;
}
else
{
printf("选择错误,请重新选择:\n");
}
}
} while (in);
}
void Show_people(NODE* head)
{
head = head->next;
if (head== NULL)
{
printf("目前没有联系人\n");
return;
}
printf("姓名 性别 年龄 住址 电话 \n");
while (head!=NULL)
{
printf("%-10s%-5s%-6d%-15s%-15s\n", head->people.name, head->people.sex,
head->people.old, head->people.adress, head->people.phone_code);
head = head->next;
}
}
NODE* search(NODE*head)
{
head = head->next;
char name[20] = { 0 };
scanf("%s", name);
while(head!=NULL)
{
if (!strcmp(name, head->people.name))
{
return head;
}
head = head->next;
}
return NULL;
}
void Search_people(NODE*head)
{
printf("输入查找的联系人的姓名:\n");
NODE*ret = search(head);
if (ret != NULL)
{
printf("姓名 性别 年龄 住址 电话 \n");
printf("%-10s%-5s%-6d%-15s%-15s\n", ret->people.name, ret->people.sex,
ret->people.old, ret->people.adress, ret->people.phone_code);
return;
}
printf("查找不到联系人\n");
}
void Delete_people(NODE*head)
{
NODE* const head_temp = head;
NODE*temp = head;
head = head->next;
char name[20] = { 0 };
printf("输入删除的联系人的姓名:\n");
scanf("%s", name);
while (head != NULL)
{
if (!strcmp(name, head->people.name))
{
printf("删除成功\n");
if (head_temp->next == head)
{
if (head->next == NULL)
head_temp->next = NULL;
else
head_temp->next = head->next;
}
else if (head->next == NULL)
{
temp->next = NULL;
}
else
{
temp->next = head->next;
}
free(head);
head = NULL;
return;
}
temp = head;
head = head->next;
}
printf("没有此联系人\n");
}
void Revise_people(NODE*head)
{
printf("输入修改的联系人的姓名:\n");
NODE*ret = search(head);
if (ret != NULL)
{
printf("输入修改后联系人姓名:\n");
scanf("%s", ret->people.name);
printf("输入修改后联系人性别:\n");
scanf("%s", ret->people.sex);
printf("输入修改后联系人年龄:\n");
scanf("%d", &ret->people.old);
printf("输入修改后联系人住址:\n");
scanf("%s", ret->people.adress);
printf("输入修改后联系人电话:\n");
scanf("%s", ret->people.phone_code);
printf("修改成功\n");
return;
}
printf("没有找到你要修改的联系人的姓名\n");
}
int comper1(const NODE*p1, const NODE*p2)
{
return strcmp(p1->people.name, p2->people.name);
}
int comper2(const NODE*p1, const NODE*p2)
{
return p1->people.old - p2->people.old;
}
void my_qsort(NODE*next, NODE*next1, int comper(NODE*, NODE*))
{
char tmp[sizeof(next->people)] = { 0 };
if (comper(next,next1) > 0)
{
memmove(tmp, &next->people, sizeof(next->people));
memmove(&next->people, &next1->people, sizeof(next->people));
memmove(&next1->people, tmp, sizeof(next->people));
}
}
void Sort_people(NODE*head)
{
int i;
head = head->next;
printf(" 1.按名字排序 2.按年龄排序 \n");
while (1)
{
printf("请选择:\n");
scanf("%d", &i);
if (i == 1 || i == 2)
break;
else
printf("选择错误,请重新选择\n");
}
i--;
int(*comper[2])(const NODE*, const NODE*) = { comper1, comper2 };
while (head->next != NULL)
{
my_qsort(head, head->next, comper[i]);
head = head->next;
}
printf("排序成功\n");
}
void Init_people(NODE*head)
{
int i;
NODE*head_tmp = head;
NODE*temp = NULL;
head = head->next;
while (1)
{
printf("是否要清空联系人:\n********1.是 ********0.退出******** \n");
scanf("%d", &i);
if (i == 1 || i == 0)
break;
else
printf("选择错误,重新选择!!!\n");
}
if (i)
{
while (head != NULL)
{
temp = head->next;
free(head);
head = temp;
}
head_tmp->next = NULL;
}
else
return;
}
void Read_people(NODE**head)
{
FILE*in;
int i = 0;
char pc[sizeof((*head)->people)] = { 0 };
if ((in = fopen(FILENAME, "rb")) == NULL)
{
return;
}
while (fread(pc, sizeof(pc), 1, in))
{
*head = malloc(sizeof(NODE));
if (*head == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
memmove(&(*head)->people, pc, sizeof(pc));
head = &(*head)->next;
}
*head = NULL;
fclose(in);
}
void Save_people(NODE*head)
{
NODE*temp = head->next;
FILE*out;
if ((out = fopen(FILENAME, "wb")) == NULL)
{
fprintf(stdout, "Can't open");
return;
}
while (temp != NULL)
{
fwrite(&temp->people, sizeof(temp->people), 1, out);
temp = temp->next;
}
fclose(out);
}
#include"head.h"
NODE* Find_tail(NODE* head)
{
NODE*temp_tail = NULL;
if (head->next == NULL)
{
temp_tail = head;
}
else
{
head = head->next;
while (head->next != NULL)
head = head->next;
temp_tail = head;
}
return temp_tail;
}
void mean()
{
printf("*******************************************************\n");
printf("******** 1.Add people 2.Show people ******\n");
printf("******** ******\n");
printf("******** 3.Search people 4.Delete people ******\n");
printf("******** ******\n");
printf("******** 5.Revise people 6.Sort people ******\n");
printf("******** ******\n");
printf("******** 7.Init people 0.EXIT ******\n");
printf("*******************************************************\n");
}
void test()
{
NODE*head = malloc(sizeof(NODE));
NODE*tail = NULL;
if (head == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
head->next = NULL;
Read_people(&head->next);
tail = Find_tail(head);
int input;
do
{
mean();
printf("请选择:\n");
scanf("%d", &input);
switch (input)
{
case EXIT:
Save_people(head);
exit(0);
break;
case ADD:
Add_people(tail);
break;
case SHOW:
Show_people(head);
break;
case SEARCH:
Search_people(head);
break;
case DELETE:
Delete_people(head);
break;
case REVISE:
Revise_people(head);
break;
case SORT:
Sort_people(head);
break;
case INIT:
Init_people(head);
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
free(head);
}
int main()
{
test();
return 0;
}