address_list.h
#pragma once
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define DEFAULT_SZ 10
#define DEFAULT_INCREMENT 10
#define LENGTH
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 11
#define ADDR_MAX 30
typedef struct Info
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char tele[TELE_MAX];
char addr[ADDR_MAX];
}Info;
typedef struct contact
{
int num;
int capacity;
Info *data;
}contact;
void Initmy_contact(contact *pcon);//初始化结构体
void Addcontact(contact *pcon);//添加联系人信息
void Delcontact(contact *pcon);//删除指定联系人信息
void Searchcontact(contact *pcon);//查找指定联系人信息
void Modifycontact(contact *pcon);//修改指定联系人信息
void Showcontact(contact *pcon);//显示所有联系人信息
void Emptycontact(contact *pcon);//清空所有联系人
void Sortcontact(contact *pcon);//以名字或年龄排序所有联系人
void save_addr(contact *pcon);//保存联系人信息
void load_addr(contact *pcon);//加载联系人信息
address_list.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "address_list.h"
void Initmy_contact(contact *pcon)
{
pcon->num = 0;
pcon->capacity = DEFAULT_SZ;
pcon->data = (Info *)calloc(DEFAULT_SZ, sizeof(Info));
}
void Check_capacity(contact *pcon)
{
if(pcon->num == pcon->capacity)
{
Info *tmp = (Info *)realloc(pcon->data, (pcon->capacity + DEFAULT_INCREMENT)*sizeof(Info));
if(tmp != NULL)
{
pcon->data = tmp;
pcon->capacity+=DEFAULT_INCREMENT;
printf("成功增容\n");
}
else
{
printf("%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
}
void Addcontact(contact *pcon)
{
Check_capacity(pcon);
printf("请输入名字:");
scanf("%s", pcon->data[pcon->num].name);
printf("请输入年龄:");
scanf("%d", &(pcon->data[pcon->num].age));
printf("请输入性别:");
scanf("%s", pcon->data[pcon->num].sex);
printf("请输入电话:");
scanf("%s", pcon->data[pcon->num].tele);
printf("请输入地址:");
scanf("%s", pcon->data[pcon->num].addr);
pcon->num++;
printf("添加成功\n");
}
void Delcontact(contact *pcon)
{
int i =0;
int j =0;
char name[NAME_MAX] = {0};
if(pcon->num == 0)
{
printf("通讯录为空,无法删除!\n");
return;
}
printf("请输入要删除的人名:");
scanf("%s", name);
for(i=0; i<pcon->num; i++)
{
if(strcmp(name, pcon->data[i].name) == 0)
{
break;
}
}
if(i == pcon->num)
{
printf("要删除的人不存在\n");
return;
}
for(j=i; j<pcon->num-1; j++)
{
pcon->data[j] = pcon->data[j+1];
}
pcon->num--;
printf("删除成功\n");
}
void Searchcontact(contact *pcon)
{
int i = 0;
char name[NAME_MAX] = {0};
printf("请输入你要找的人名:");
scanf("%s", &name);
for(i=0; i<pcon->num; i++)
{
if(strcmp(name, pcon->data[i].name) == 0)
{
printf("%15s %4s %5s %11s %10s\n",
"名字", "性别", "年龄", "电话", "地址");
printf("%15s %4d %5s %11s %10s\n",
pcon->data[i].name,
pcon->data[i].age,
pcon->data[i].sex,
pcon->data[i].tele,
pcon->data[i].addr);
return;
}
}
printf("没有你要找的人\n");
}
void Modifycontact(contact *pcon)
{
int i = 0;
char name[NAME_MAX] = {0};
printf("请输入要修改的人名:");
scanf("%s", name);
for(i=0; i<pcon->num; i++)
{
if(strcmp(name, pcon->data[i].name) == 0)
{
printf("请输入名字:");
scanf("%s", pcon->data[i].name);
printf("请输入年龄:");
scanf("%d", &pcon->data[i].age);
printf("请输入性别:");
scanf("%s", pcon->data[i].sex);
printf("请输入电话:");
scanf("%s", pcon->data[i].tele);
printf("请输入地址:");
scanf("%s", pcon->data[i].addr);
printf("修改成功\n");
return;
}
}
printf("要修改的人不存在\n");
}
void Showcontact(contact *pcon)
{
int i = 0;
if(pcon->num == 0)
{
printf("通讯录为空!\n");
return;
}
printf("%15s %4s %5s %11s %10s\n",
"名字", "年龄", "性别", "电话", "地址");
for(i=0; i<pcon->num; i++)
{
printf("%15s %4d %5s %11s %10s\n",
pcon->data[i].name,
pcon->data[i].age,
pcon->data[i].sex,
pcon->data[i].tele,
pcon->data[i].addr);
}
}
void Sortcontact(contact *pcon)
{
int i = 0;
int j = 0;
Info data = {0};
int input = 0;
if(pcon->num == 0)
{
printf("通讯录为空!\n");
return;
}
printf("####1.按名字 2.按年龄####\n");
scanf("%d", &input);
switch(input)
{
case 1:
{
for(i=0; i<pcon->num-1; i++)
{
for(j=0; j<pcon->num-1-i; j++)
{
if(strcmp(pcon->data[j].name, pcon->data[j+1].name) > 0)
{
data = pcon->data[j+1];
pcon->data[j+1] = pcon->data[j];
pcon->data[j] = data;
}
}
}
printf("排序成功\n");
return;
}
case 2:
{
for(i=0; i<pcon->num-1; i++)
{
for(j=0; j<pcon->num-1-i; j++)
{
if(pcon->data[j].age > pcon->data[j+1].age)
{
data = pcon->data[j+1];
pcon->data[j+1] = pcon->data[j];
pcon->data[j] = data;
}
}
}
printf("排序成功\n");
return;
}
default:
printf("输入错误!\n");
return;
}
}
void Emptycontact(contact *pcon)
{
memset(pcon->data, 0, (pcon->capacity*sizeof(Info)));
pcon->num = 0;
printf("成功清空!\n");
}
void save_addr(contact *pcon)
{
int i = 0;
FILE * pf = fopen("通讯录.txt", "w");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
else
{
for(i=0; i<pcon->num; i++)
{
fwrite(pcon->data, sizeof(Info), 1, pf);
}
}
fclose(pf);
}
void load_addr(contact *pcon)
{
int i = 0;
FILE * pf = fopen("通讯录.txt", "r");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
else
{
while(fread(&(pcon->data[pcon->num]), sizeof(Info), 1, pf))
{
Check_capacity(pcon);
pcon->num++;
}
}
fclose(pf);
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "address_list.h"
void menu()
{
printf("*****************************\n");
printf("****1.add 2.del ****\n");
printf("****3.search 4.modify ****\n");
printf("****5.show 6.empty ****\n");
printf("****7.sort 0.exit ****\n");
printf("*****************************\n");
}
enum option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
EMPTY,
SORT
};
int main()
{
int input = 0;
contact my_contact;
Initmy_contact(&my_contact);
load_addr(&my_contact);
do
{
menu();
printf("请输入:");
scanf("%d", &input);
switch(input)
{
case ADD:
Addcontact(&my_contact);
break;
case DEL:
Delcontact(&my_contact);
break;
case SEARCH:
Searchcontact(&my_contact);
break;
case MODIFY:
Modifycontact(&my_contact);
break;
case SHOW:
Showcontact(&my_contact);
break;
case EMPTY:
Emptycontact(&my_contact);
break;
case SORT:
Sortcontact(&my_contact);
break;
case EXIT:
save_addr(&my_contact);
free(my_contact.data);
my_contact.data = NULL;
break;
default:
printf("输入错误!\n");
break;
}
}while(input);
return 0;
}