You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
4.1 KiB

6 years ago
#define _CRT_SECURE_NO_WARNINGS 1
#include "Contact.h"
void* check_capacity(pContact pc)//<2F><><EFBFBD><EFBFBD>
{
assert(pc);
if (pc->sz == pc->capacity)//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
{
PeoInfo *tmp = realloc(pc->data, (pc->capacity + 2)*sizeof(PeoInfo)); //data<74>ı<EFBFBD><C4B1>ڴ棬 <20>µĴ<C2B5>С
if (tmp != NULL) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ı<EFBFBD><C4B1>ڴ<EFBFBD><DAB4><EFBFBD>ִ<EFBFBD><D6B4>
{
pc->data = tmp;
pc->capacity += 2;
return pc->data;
}
else
{
return NULL;
}
}
}
void LoadContact(pContact pc) //<2F><>ȡ--<2D><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
{
PeoInfo tmp = { 0 };
FILE* pf = fopen("contact.dat", "rb");
int i = 0;
if (pf == NULL)
{
perror("open file for read");
exit(EXIT_FAILURE);
}
while (fread(&tmp, sizeof(PeoInfo), 1, pf)) //<2F><>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
{
if (check_capacity(pc) != NULL)
{
pc->data[pc->sz] = tmp;
pc->sz++;
}
}
fclose(pf);
pf = NULL;
}
void InitContact(pContact pc) //<2F><>ʼ<EFBFBD><CABC>
{
assert(pc);
pc->sz = 0;
pc->data = (PeoInfo*)malloc(3 * sizeof(PeoInfo));//<2F><><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD>
pc->capacity = DEFAULT_SZ;
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Ϣ
LoadContact(pc);
}
void DestroyContact(pContact pc)//<2F>ͷŶ<CDB7>̬<EFBFBD>ڴ濪<DAB4>ٵĿռ<C4BF>
{
assert(pc);
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->sz = 0;
}
void AddContact(pContact pc) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
{
assert(pc);
if (check_capacity(pc) == NULL)
{
return;
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:>");
scanf("%s", pc->data[pc->sz].name);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:>");
scanf("%d", &(pc->data[pc->sz].age));
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD>:>");
scanf("%s", pc->data[pc->sz].sex);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>绰:>");
scanf("%s", pc->data[pc->sz].tele);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ:>");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
}
void ShowContact(pContact pc) //<2F><>ʾ<EFBFBD><CABE>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD>Ϣ
{
assert(pc);
int i = 0;
printf("%10s\t%4s\t%5s\t%12s\t%20s\n", "name","age","sex","tele","addr");
for (i = 0; i < pc->sz; i++)
{
printf("%10s\t%4d\t%5s\t%12s\t%20s\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
int FindEntry(pContact pc, char name[]) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5> <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
{
int i = 0;
assert(pc);
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0) //<2F>Ƚ<EFBFBD><C8BD>ַ<EFBFBD><D6B7>Ƿ<EFBFBD><C7B7><EFBFBD>ͬȷ<CDAC><C8B7><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
{
return i;
}
}
return -1;//<2F><><EFBFBD><EFBFBD>û<EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>ϵ<EFBFBD><CFB5>
}
void DelContact(pContact pc) //ɾ<><C9BE><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
{
assert(pc);
char name[NAME_MAX] = { 0 };
int i = 0;
int j = 0;
int pos = 0;
if (pc->sz == 0) //<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>ͨѶ¼
{
printf("ͨѶ¼<EFBFBD>ѿգ<EFBFBD><EFBFBD>޷<EFBFBD>ɾ<EFBFBD><EFBFBD>\n");
return;
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫɾ<EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>>:"); //1.<2E><><EFBFBD><EFBFBD>
scanf("%s", name);
pos=FindEntry(pc, name); //<2F>ж<EFBFBD>Ҫ<EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if (pos!= -1) //2.ɾ<><C9BE>
{
for (j = pos; j < pc->sz - 1; j++) //<2F>ҵ<EFBFBD>λ<EFBFBD>ú<EFBFBD> <20><><EFBFBD><EFBFBD>ǰһ<C7B0><D2BB><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD>λ
{
pc->data[j] = pc->data[j + 1];
}
pc->sz--;
printf("ɾ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>\n");
}
else
{
printf("Ҫɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
}
void SearchContact(pContact pc) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
{
assert(pc);
char name[NAME_MAX] = { 0 };
int pos = 0;
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>>:");
scanf("%s", name);
pos=FindEntry(pc, name); //<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if (pos == -1)
{
printf("Ҫ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD>˲<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
else
{
printf("%10s\t%4s\t%5s\t%12s\t%20s\n", "name", "age", "sex", "tele", "addr");
printf("%10s\t%4d\t%5s\t%12s\t%20s\n",
pc->data[pos].name,
pc->data[pos].age,
pc->data[pos].sex,
pc->data[pos].tele,
pc->data[pos].addr);
}
}
void ModifyContact(pContact pc) //<2F>޸<EFBFBD><DEB8><EFBFBD>Ϣ
{
assert(pc);
char name[NAME_MAX] = { 0 };
int pos = 0;
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>޸<EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>>:");
scanf("%s", name);
pos = FindEntry(pc, name);
if (pos == -1)
{
printf("Ҫ<EFBFBD>޸ĵ<EFBFBD><EFBFBD>˲<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
else
{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:>");
scanf("%s", pc->data[pos].name);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:>");
scanf("%d", &(pc->data[pos].age));
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD>:>");
scanf("%s", pc->data[pos].sex);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>绰:>");
scanf("%s", pc->data[pos].tele);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ:>");
scanf("%s", pc->data[pos].addr);
}
}
void EmptyContact(pContact pc) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
{
pc->sz = 0;
printf("ͨѶ¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
void SaveContact(pContact pc) //<2F><EFBFBD><E6B4A2>Ϣ //д<>ļ<EFBFBD>
{
FILE* pf = fopen("contact.dat", "wb");
int i = 0;
if (pf == NULL)
{
perror("open file for write");
exit(EXIT_FAILURE);
}
for (i = 0; i < pc->sz; i++) //д<>ļ<EFBFBD>
{
fwrite(pc->data + i, sizeof(PeoInfo), 1, pf);
}
fclose(pf);
pf = NULL;
}