一、函数调用
目录
二、宏常量、结构体、枚举的定义以及函数声明
#define MAX_NAME 10
#define MAX_GENDER 5//注意汉字储存,两字节
#define MAX_TELE 13
#define MAX_ADDRESS 20
#define MAX_RELV 5
#define MAX_AGE 3
#define DEFAULT_CY 50
#define DEFAULTADD 2
//定义每种信息的最大存储量
#include<stdio.h>
#include<Windows.h>
#include<string.h>
#include<errno.h>
struct P_infm
{
char name[MAX_NAME];
char age[MAX_AGE];
char gender[MAX_GENDER];
char tele[MAX_TELE];
char address[MAX_ADDRESS];
char relv[MAX_RELV];
};
struct Contact
{
int size;
int capacity;
struct P_infm*date;
}con;
enum//枚举便于switch循环
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SORT,
DISPLAY,
SAVE
};
void InitContact(struct Contact*p);
void ADDContact(struct Contact*p);
void DELContact(struct Contact*p);
void SEARCHContact(struct Contact*p);
void MODIFYContact(struct Contact*p);
void SortContact(struct Contact*p);
void DISPLAYContact(struct Contact*p);
void FREE(struct Contact*p);
void SAVEContact(struct Contact*p);
void LoadContact(struct Contact*p);
三、函数实现
#include"Contact.h"
void InitContact(struct Contact*p)//初始化
{
p->date = malloc(DEFAULT_CY * sizeof(struct P_infm));
if (p != NULL)
{
p->capacity = DEFAULT_CY;
p->size = 0;
memset(p->date, 0, p->capacity * sizeof(struct P_infm));
}
else
printf("初始化失败\n");
}
void checkcapacity(struct Contact*p);
void LoadContact(struct Contact*p)//载入文本数据
{
FILE*pf = fopen("Contact.hhh", "rb");
if (!pf)
{
printf("LoadContact::%s\n", strerror(errno));
return;
}
while (fread(&(p->date[p->size]), sizeof(struct P_infm), 1, pf))//fread返回值为读取有效字符的个数
{
checkcapacity(p);
p->size++;
}
printf("加载成功\n");
fclose(pf);
pf = NULL;
}
void checkcapacity(struct Contact*p)//检查容量并判断是否扩增
{
if (p->size == p->capacity)
{
struct P_infm*str = realloc(p->date,(p->capacity+DEFAULTADD)*sizeof(struct P_infm));
if (p != NULL)
{
p->date = str;
p->capacity += DEFAULTADD;
}
else
printf("增容失败\n");
}
}
void ADDContact(struct Contact*p)//增加
{
checkcapacity(p);
printf("请输入姓名>:\n");
scanf("%s", &p->date[p->size].name);
printf("请输入年龄>:\n");
scanf("%s", &p->date[p->size].age);
printf("请输入性别>:\n");
scanf("%s", &p->date[p->size].gender);
printf("请输入电话号码>:\n");
scanf("%s", &p->date[p->size].tele);
printf("请输入地址>:\n");
scanf("%s", &p->date[p->size].address);
printf("请备注关系>:\n");
scanf("%s", &p->date[p->size].relv);
p->size++;
printf("添加成功\n");
}
void DELContact(struct Contact*p)//删除
{
char tmp[MAX_NAME] = { 0 };
int i = 0;
printf("请输入查找的人的姓名>:\n");
scanf("%s", &tmp);
while(1)
{
int ret = p->size;
if (!strcmp(tmp, p->date[i].name))
{
while (--ret - i)
{
p->date[i] = p->date[i + 1];
}
memset(&(p->date[p->size-1]),0, sizeof(p->date[0]));
p->size--;
printf("删除成功\n");
break;
}
else if (i >= p->size)
{
printf("通讯录中无此人信息\n");
break;
}
else
i++;
}
}
void SEARCHContact(struct Contact*p)//查找
{
char tmp[MAX_NAME] = { 0 };
int i = 0;
printf("请输入查找的人的姓名>:\n");
scanf("%s", &tmp);
if (!p->size)
printf("通讯录为空\n");
while(1)
{
if (!strcmp(tmp, p->date[i].name))
{
printf("姓名 年龄 性别 电话 地址 关系 \n");
printf("%-9s%-7s%-6s%-13s%-12s%-3s\n",
p->date[i].name,
p->date[i].age,
p->date[i].gender,
p->date[i].tele,
p->date[i].address,
p->date[i].relv);
break;
}
else if (i >= p->size)
{
printf("通讯录中无此人信息\n");
break;
}
else
i++;
}
}
void MODIFYContact(struct Contact*p)//修改
{
int i = 0;
char tmp[MAX_NAME] = { 0 };
printf("请输入查找的人的姓名>:\n");
scanf("%s", &tmp);
while (1)
{
if (!strcmp(tmp, p->date[i].name))
{
printf("请输入姓名\n");
scanf("%s", &p->date[i].name);
printf("请输入年龄\n");
scanf("%s", &p->date[i].age);
printf("请输入性别\n");
scanf("%s", &p->date[i].gender);
printf("请输入电话\n");
scanf("%s", &p->date[i].tele);
printf("请输入地址\n");
scanf("%s", &p->date[i].address);
printf("请输入关系\n");
scanf("%s",&p->date[i].relv);
break;
}
else if (i >= p->size)
{
printf("通讯录中无此人信息\n");
break;
}
else
i++;
}
}
int str_cmp(const void *e1,const void* e2)//定义库函数比较方式
{
return *(int*)e1 - *(int*)e2;
}
void SortContact(struct Contact*p)//按名字用库排序
{
qsort(p->date, p->size,sizeof(p->date[0]), str_cmp);
}
void DISPLAYContact(struct Contact*p)
{
printf("姓名 年龄 性别 电话 地址 关系 \n");
for (int i = 0; i < p->size; i++)
{
printf("%-9s%-7s%-6s%-13s%-12s%-3s\n",
p->date[i].name,
p->date[i].age,
p->date[i].gender,
p->date[i].tele,
p->date[i].address,
p->date[i].relv);
}
}
void FREE(struct Contact*p)//释放内存
{
free(p->date);
p->date = NULL;
}
void SAVEContact(struct Contact*p)//保存文本数据
{
FILE*pf = fopen("Contact.hhh", "wb");
if (!pf)
{
printf("SAVEContect::%s\n", strerror(errno));
return;
}
for (int i = 0; i < p->size; i++)
{
fwrite(&(p->date[i]), sizeof(struct P_infm), 1, pf);
}
fclose(pf);
pf = NULL;
}
四、我后来又写的
#define _CRT_SECURE_NO_WARNINGS
#define MAX_NAME 10
#define MAX_AGE 3
#define MAX_GENDER 3
#define MAX_TEL 15
#define MAX_ADRESS 15
#define MAX_RELATION 10
#define INIT_CAPACITY 3
#define ADD_CAPACITY 2
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<string.h>
struct Data
{
char name[MAX_NAME];
char age[MAX_AGE];
char gender[MAX_GENDER];
char tel[MAX_TEL];
char adress[MAX_ADRESS];
char relation[MAX_RELATION];
};
typedef struct Contact
{
int capacity;
int size;
struct Data*data;
}C;
enum
{
EXIT,
ADD,
DEL,
SERCH,
MODIFY,
DISPLAY,
SORT,
SAVE,
};
void InitC(C*c);
void LoadC(C*c);
void AddC(C*c);
void DelC(C*c);
void SearchC(C*c);
void ModifyC(C*c);
void Display(C*c);
void SortC(C*c);
void SaveC(C*c);
void menu()
{
printf("*************************************\n");
printf("******* 通讯录 *******\n");
printf("******* 1.增加联系人 *******\n");
printf("******* 2.删除联系人 *******\n");
printf("******* 3.查找联系人 *******\n");
printf("******* 4.修改联系人 *******\n");
printf("******* 5.展示联系人 *******\n");
printf("******* 6.联系人排序 *******\n");
printf("******* 7.保存联系人 *******\n");
printf("******* 0.退出通讯录 *******\n");
printf("*************************************\n");
}
void startwork(C*c)
{
InitC(c);
LoadC(c);
int choice;
while(1)
{
system("cls");
menu();
printf("请选择:>\n");
scanf("%d", &choice);
switch (choice)
{
case ADD:
AddC(c);
system("pause");
break;
case DEL:
DelC(c);
system("pause");
break;
case SERCH:
SearchC(c);
system("pause");
break;
case MODIFY:
ModifyC(c);
system("pause");
break;
case SORT:
SortC(c);
system("pause");
break;
case SAVE:
SaveC(c);
system("pause");
break;
case DISPLAY:
Display(c);
system("pause");
break;
case EXIT:
SaveC(c);
exit(0);
break;
default:
printf("选择错误\n");
break;
}
}
}
int main()
{
C c;
startwork(&c);
return 0;
}
void InitC(C*c)
{
c->capacity = INIT_CAPACITY;
c->data = (struct Data*)calloc(c->capacity,sizeof(struct Data));
c->size = 0;
}
void checkcapacity(C*c)
{
if (c->size == c->capacity)
{
c->capacity += ADD_CAPACITY;
c->data = (struct Data*)realloc(c->data, sizeof(struct Data)*(c->capacity));
}
}
void LoadC(C*c)
{
FILE*p = fopen("data.contact", "rb");
while (fread(&(c->data[c->size]), sizeof(c->data[0]), 1, p))
{
c->size++;
checkcapacity(c);
}
fclose(p);
printf("载入成功\n");
}
int Getindex(C*c)
{
int index;
char temp[MAX_NAME];
printf("请输入姓名:\n");
scanf("%s", &temp);
for (int i = 0; i < c->size; i++)
{
if (!strcmp(c->data[i].name, temp))
{
index = i;
return index;
}
}
printf("无此人信息\n");
return -1;
}
void Receive(C*c,int n)
{
printf("请输入姓名:\n");
scanf("%s", &c->data[n].name);
printf("请输入年龄:\n");
scanf("%s", &c->data[n].age);
printf("请输入性别:\n");
scanf("%s", &c->data[n].gender);
printf("请输入电话:\n");
scanf("%s", &c->data[n].tel);
printf("请输入地址:\n");
scanf("%s", &c->data[n].adress);
printf("请备注关系:\n");
scanf("%s", &c->data[n].relation);
}
void AddC(C*c)
{
checkcapacity(c);
Receive(c,c->size);
c->size++;
printf("添加成功\n");
}
void DelC(C*c)
{
int n = Getindex(c);
if(n!=-1)
{
for (int i = n; i < c->size; i++)
c->data[i] = c->data[i + 1];
c->size--;
printf("删除成功!\n");
}
}
void SearchC(C*c)
{
int n = Getindex(c);
if (n != -1)
{
printf("姓名 年龄 性别 电话 地址 关系 \n");
printf("%-9s%-7s%-6s%-13s%-12s%-3s\n",
c->data[n].name,
c->data[n].age,
c->data[n].gender,
c->data[n].tel,
c->data[n].adress,
c->data[n].relation);
};
}
void ModifyC(C*c)
{
int n = Getindex(c);
if (n)
Receive(c, n);
printf("修改成功\n");
}
int s_cmp(void*e1, void*e2)
{
return (int)*((char*)e1) - ((int)*((char*)e2));
}
void SortC(C*c)
{
qsort(c->data, c->size, sizeof(c->data[0]), s_cmp);
}
void SaveC(C*c)
{
FILE*p = fopen("data.contact", "wb");
int n = c->size;
for (int i = 0; i < c->size; i++)
fwrite(&(c->data[i]), sizeof(c->data[0]), 1, p);
fclose(p);
}
void Display(C*c)
{
printf("姓名 年龄 性别 电话 地址 关系 \n");
for (int i = 0; i <c->size; i++)
{
printf("%-9s%-7s%-6s%-13s%-12s%-3s\n",
c->data[i].name,
c->data[i].age,
c->data[i].gender,
c->data[i].tel,
c->data[i].adress,
c->data[i].relation);
}
}