利用C语言实现通讯录,内含增删查改功能,可自行增长容量,可实现关闭程序后信息仍旧保存
代码如下:
contact.h
#ifndef contacts_h
#define contacts_h
#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 12
#define ADDR_MAX 30
#define DEFAULT_SZ 3
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
typedef struct PeoInfo
{
char name[NAME_MAX];
char sex[SEX_MAX];
int age;
char tele[TELE_MAX];
char addr[ADDR_MAX];
}PeoInfo;
typedef struct Contact
{
PeoInfo* data;
int sz;
int capacity;
}contact;
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SORT,
PRINT,
DESTORY,
SAVE
};
void menu(void);
void InitContact(struct Contact* pc);
void AddContact(struct Contact* pc);
void PrintContact(struct Contact* pc);
void DelContact(struct Contact* pc);
int FindByName(struct Contact* pc,char name[]);
void SearchContact(struct Contact* pc);
void ModifyContact(struct Contact* pc);
void SortContact(struct Contact* pc);
void CheckCapacity(struct Contact* pc);
void DestoryContact(struct Contact* pc);
void LoadContact(struct Contact* pc);
void SaveContact(struct Contact* pc);
#endif /* contacts_h */
contact.c:
#include "contacts.h"
void menu(void)
{
printf("##############################\n");
printf("#### 1.add 2.del ####\n");
printf("#### 3.search 4.modify ####\n");
printf("#### 5.sort 6.print ####\n");
printf("#### 7.destory 8.save ####\n");
printf("#### 0.exit ####\n");
printf("##############################\n");
}
void LoadContact(struct Contact* pc)
{
FILE* pf = fopen("contact.dat","rb");
if(pf == NULL)
{
perror("LoadContact::fopen");
return;
}
PeoInfo tmp = { 0 };
while(fread(&tmp, sizeof(PeoInfo), 1, pf) != EOF)
{
CheckCapacity(pc);
pc->data[pc->sz] = tmp;
pc->sz++;
}
fclose(pf);
pf = NULL;
}
void InitContact(struct Contact* pc)
{
pc->sz = 0;
pc->capacity = DEFAULT_SZ;
pc->data = (PeoInfo*)malloc(sizeof(PeoInfo) * pc->capacity);
if(pc->data == NULL)
{
perror("InitContact::malloc");
return;
}
memset(pc->data, 0, sizeof(PeoInfo) * pc->capacity);
LoadContact(pc);
}
void DestoryContact(struct Contact* pc)
{
free(pc->data);
pc->capacity = 0;
pc->sz = 0;
pc->data = NULL;
printf("success destory\n");
}
void CheckCapacity(struct Contact* pc)
{
if(pc->capacity == pc->sz)
{
PeoInfo* tmp = (PeoInfo*)realloc(pc->data, (pc->capacity + 2) * sizeof(PeoInfo));
if(tmp == NULL)
{
perror("CheckCapacity::realloc");
return;
}
printf("success realloc\n");
pc->data = tmp;
pc->capacity += 2;
}
}
void AddContact(struct Contact* pc)
{
CheckCapacity(pc);
printf("please input name\n");
scanf("%s",pc->data[pc->sz].name);
printf("please input age\n");
scanf("%d",&(pc->data[pc->sz].age));
printf("please input sex\n");
scanf("%s",pc->data[pc->sz].sex);
printf("please input tele\n");
scanf("%s",pc->data[pc->sz].tele);
printf("please input addr\n");
scanf("%s",pc->data[pc->sz].addr);
pc->sz++;
printf("add success\n");
}
void DelContact(struct Contact* pc)
{
if(pc->sz == 0)
{
printf("the contact is empty\n");
return;
}
printf("please input the name which you want to del\n");
char name[NAME_MAX];
scanf("%s",name);
int pos = 0;
pos = FindByName(pc,name);
if(pos == -1)
{
printf("the person does not exist\n");
return;
}
else
{
int j = 0;
for(j = pos; j < pc->sz - 1; j++)
{
pc->data[j] = pc->data[j + 1];
}
pc->sz--;
printf("success del\n");
}
}
void SearchContact(struct Contact* pc)
{
printf("please input the name which you want to search\n");
char name[NAME_MAX];
scanf("%s",name);
int pos = 0;
pos = FindByName(pc,name);
if(pos == -1)
{
printf("the person does not exist\n");
return;
}
else
{
printf("the person which you want to search's information\n");
printf("%20s %5s %5s %12s %30s\n","name","age","sex","tele","addr");
printf("%20s %5d %5s %12s %30s\n",pc->data[pos].name,pc->data[pos].age
,pc->data[pos].sex,pc->data[pos].tele,pc->data[pos].addr);
}
}
int FindByName(struct Contact* pc,char name[])
{
assert(pc);
int i = 0;
for(i = 0; i < pc->sz;i++)
{
if(strcmp(pc->data[i].name,name) == 0)
{
return i;
}
}
return -1;
}
void ModifyContact(struct Contact* pc)
{
printf("please input the name which you want to modify\n");
char name[NAME_MAX];
scanf("%s",name);
int pos = 0;
pos = FindByName(pc,name);
if(pos == -1)
{
printf("the person does not exist\n");
return;
}
else
{
if(pc->sz == MAX)
{
printf("the contact is fill\n");
return;
}
printf("please input name\n");
scanf("%s",pc->data[pos].name);
printf("please input age\n");
scanf("%d",&(pc->data[pos].age));
printf("please input sex\n");
scanf("%s",pc->data[pos].sex);
printf("please input tele\n");
scanf("%s",pc->data[pos].tele);
printf("please input addr\n");
scanf("%s",pc->data[pos].addr);
pc->sz++;
printf("add success\n");
printf("the person which you want to modify's information\n");
printf("%20s %5s %5s %12s %30s\n","name","age","sex","tele","addr");
printf("%20s %5d %5s %12s %30s\n",pc->data[pos].name,pc->data[pos].age
,pc->data[pos].sex,pc->data[pos].tele,pc->data[pos].addr);
}
}
int cmp(const void *a,const void *b)
{
struct PeoInfo *aa = (struct PeoInfo *)a;
struct PeoInfo *bb = (struct PeoInfo *)b;
return (((aa->age) > (bb->age)) ? 1 : -1);
}
void SortContact(struct Contact* pc)
{
qsort(pc->data,pc->sz,sizeof(pc->data[0]),cmp);
printf("sort success\n");
}
void PrintContact(struct Contact* pc)
{
assert(pc);
int i = 0;
printf("%20s %5s %5s %12s %30s\n","name","age","sex","tele","addr");
for(i = 0; i < pc->sz;i++)
{
printf("%20s %5d %5s %12s %30s\n",pc->data[i].name,pc->data[i].age
,pc->data[i].sex,pc->data[i].tele,pc->data[i].addr);
}
}
void SaveContact(struct Contact* pc)
{
FILE* pf = NULL;
pf = fopen("contact.dat", "wb");
if(pf == NULL)
{
perror("SaveContact::fopen");
return;
}
int i = 0;
for(i = 0;i < pc->sz; i++)
{
fwrite(pc->data+i, sizeof(PeoInfo), 1, pf);
}
fclose(pf);
pf = NULL;
}
test.c:
#include "contacts.h"
int main(int argc, const char * argv[]) {
contact con;
InitContact(&con);
int input = 0;
menu();
do
{
printf("please input your chose\n");
scanf("%d",&input);
switch (input) {
case EXIT:
printf("exit the contact\n");
break;
case ADD:
AddContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case SORT:
SortContact(&con);
break;
case PRINT:
PrintContact(&con);
break;
case DESTORY:
DestoryContact(&con);
break;
case SAVE:
SaveContact(&con);
break;
default:
printf("error input\n");
break;
}
}while(input);
return 0;
}