C语言康复训练-5
head.h
#pragma once
#include
#include
#include
#define DEFAULT_SZ 3
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 12
#define MAX_ADDR 30
enum option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT,
SAVE
};
typedef struct peoinfo
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
}peoinfo;
typedef struct contact
{
struct peoinfo* data;
int capacity;
int size;
}contact;
void menu();
void initcontact(contact* ps);
void addcontact(contact* ps);
void showcontact(const struct contact* ps);
void delcontact(contact* ps);
void searchcontact(const struct contact* ps);
void modifycontact(contact* ps);
void sortcontact(contact* p);
void destroycontact(contact* ps);
void savecontact(contact* ps);
void loadcontact(contact* ps);
void checkcapacity(struct contact* ps);
head.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"
int main()
{
int input = 0;
contact con;
initcontact(&con);
do
{
menu();
printf("请选择->\n");
scanf("%d", &input);
switch (input)
{
case ADD:
addcontact(&con);
break;
case DEL:
delcontact(&con);
break;
case SEARCH:
searchcontact(&con);
break;
case MODIFY:
modifycontact(&con);
break;
case SHOW:
showcontact(&con);
break;
case SORT:
sortcontact(&con);
break;
case SAVE:
savecontact(&con);
break;
case EXIT:
savecontact(&con);
destroycontact(&con);
printf("退出通讯录\n");
break;
default:
printf("输入非法请重试\n");
break;
}
} while (input);
return 0;
}
void destroycontact(struct contact* ps)
{
free(ps->data);
ps->data = NULL;
}
project.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"
void menu()
{
printf("*************************\n");
printf("*************************\n");
printf("*********0.退出**********\n");
printf("*********1.增加**********\n");
printf("*********2.删除**********\n");
printf("*********3.寻找**********\n");
printf("*********4.修改**********\n");
printf("*********5.显示**********\n");
printf("*********6.排序**********\n");
printf("*********7.保存**********\n");
printf("*************************\n");
printf("*************************\n");
}
void initcontact(struct contact* ps)
{
ps->data = (struct peoinfo*)malloc(DEFAULT_SZ * sizeof(struct peoinfo));
if (ps->data == NULL)
{
return;
}
ps->size = 0;
ps->capacity = DEFAULT_SZ;
loadcontact(ps);
}
void loadcontact(contact* ps)
{
peoinfo tmp = { 0 };
FILE* pfread = fopen("contact.dat", "rb");
if (pfread == NULL)
{
printf("loadcontact::%s\n", strerror(errno));
return;
}
while (fread(&tmp, sizeof(peoinfo), 1, pfread))
{
checkcapacity(ps);
ps->data[ps->size] = tmp;
ps->size++;
}
fclose(pfread);
pfread = NULL;
}
void checkcapacity(struct contact* ps)
{
if (ps->size == ps->capacity)
{
struct peoinfo* ptr = realloc(ps->data, (ps->capacity) * sizeof(struct peoinfo));
if (ptr != NULL)
{
ps->data = ptr;
ps->capacity += 2;
printf("增容成功\n");
}
}
}
void addcontact(struct contact* ps)
{
checkcapacity(ps);
printf("请输入名字:->");
scanf("%s", ps->data[ps->size].name);
printf("请输入年龄:->");
scanf("%d", &(ps->data[ps->size].age));
printf("请输入性别:->");
scanf("%s", ps->data[ps->size].sex);
printf("请输入电话:->");
scanf("%s", ps->data[ps->size].tele);
printf("请输入地址:->");
scanf("%s", ps->data[ps->size].addr);
ps->size++;
printf("添加成功\n");
}
void showcontact(const struct contact* ps)
{
if (ps->size == 0)
{
printf("通讯录为空\n");
}
else
{
int i = 0;
for (i = 0;i < (ps->size);i++)
{
printf("%20s\t%4s\t%5s\t%12s\t%20s\n", "名字", "年龄", "性别", "电话", "地址");
printf("%20s\t%4d\t%5s\t%12s\t%20s\n",
ps->data[i].name,
ps->data[i].age,
ps->data[i].sex,
ps->data[i].tele,
ps->data[i].addr);
}
}
}
static int findbyname(const struct contact* ps, char name[MAX_NAME])
{
int i = 0;
for (i = 0;i < ps->size;i++)
{
if (0 == strcmp(ps->data[i].name, name))
{
return i;
}
}
return -1;
}
void delcontact(struct contact* ps)
{
char name[MAX_NAME];
printf("请输入要删除人的名字:>");
scanf("%s", name);
int pos = findbyname(ps, name);
if (pos == -1)
{
printf("需要删除的名字不存在\n");
}
else
{
int j = 0;
for (j = pos;j < (ps->size - 1);j++)
{
ps->data[j] = ps->data[j + 1];
}
ps->size--;
printf("删除成功\n");
}
}
void searchcontact(const struct contact* ps)
{
char name[MAX_NAME];
printf("请输入要查找人的名字\n");
scanf("%s", name);
int pos = findbyname(ps, name);
if (pos == -1)
{
printf("要查找的人不存在\n");
}
else
{
printf("%20s\t%4s\t%5s\t%12s\t%20s\n", "名字", "年龄", "性别", "电话", "地址");
printf("%20s\t%4d\t%5s\t%12s\t%20s\n",
ps->data[pos].name,
ps->data[pos].age,
ps->data[pos].sex,
ps->data[pos].tele,
ps->data[pos].addr);
}
}
void modifycontact(struct contact* ps)
{
char name[MAX_NAME];
printf("请输入要修改人的名字:>");
scanf("%s", name);
int pos = findbyname(ps, name);
if (pos == -1)
{
printf("要修改的人不存在\n");
}
else
{
printf("请输入名字:->");
scanf("%s", ps->data[pos].name);
printf("请输入年龄:->");
scanf("%d", &(ps->data[pos].age));
printf("请输入性别:->");
scanf("%s", ps->data[pos].sex);
printf("请输入电话:->");
scanf("%s", ps->data[pos].tele);
printf("请输入地址:->");
scanf("%s", ps->data[pos].addr);
printf("修改成功\n");
}
}
void sortcontact(struct contact* p)
{
if (p->size == 0)
{
printf("通讯录为空\n");
}
int i = 0;
int j = 0;
for (i = 0; i < p->size - 1; i++)
{
for (j = 0; j < p->size - i - 1; j++)
{
if (strcmp(p->data[j].name, (p->data[j + 1]).name) > 0)
{
struct peoinfo tmp;
tmp = p->data[j];
p->data[j] = p->data[j + 1];
p->data[j + 1] = tmp;
}
}
printf("排序成功!\n");
}
}
void savecontact(struct contact* ps)
{
FILE* pfwrite = fopen("contact.dat", "wb");
if (pfwrite == NULL)
{
printf("savecontact::%s\n", strerror(errno));
return;
}
int i = 0;
for (i = 0;i < ps->size;i++)
{
fwrite(&(ps->data[i]), sizeof(peoinfo), 1, pfwrite);
}
fclose(pfwrite);
pfwrite = NULL;
printf("保存成功!\n");
}