test.c
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include "contact.h"
#define _CRT_SECURE_NO_WARNINGS 1
void menu();
int main()
{
int input = 0;
struct Contact con;
InitContact(&con);
do
{
menu();
printf("chose");
scanf_s("%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 EXIT:
SaveContact(&con);
DestoryContact(&con);
printf("exit");
break;
case SAVE:
SaveContact(&con);
break;
default:
printf("error");
break;
}
} while (input);
return 0;
}
void menu()
{
printf("1. add 2.del 3.search 4.motify 5.show 6.sor 7.save 0.exit");
}
contact.h
#define MAX 1000
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 12
#define MAX_ADDR 30
#define _CRT_SECURE_NO_WARNINGS 1
#define DEFAULT_SIZE 3
#include <stdio.h>
#include<stdlib.h>
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT,
SAVE
};
struct PeoInfo
{
char name[20];
int age;
char sex[5];
char tele[12];
char addr[30];
};
struct Contact
{
//struct PeoInfo data[MAX];
struct PeoInfo *data;
int size;
int capacity;
};
void InitContact(struct Contact* ps);
void AddContact(struct Contact* ps);
void ShowContact(const struct Contact* ps);
void DelContact(struct Contact* ps);
void SearchContact(struct Contact* ps);
void ModifyContact(const struct Contact* ps);
void SortContact(struct Contact* ps);
void CheckCapacity(struct Contact* ps);
void DestoryContact(struct Contact* ps);
void SaveContact(struct Contact* ps);
contact.c
#include "contact.h"
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include<errno.h>
void LoadContact(struct Contact* ps);
void InitContact(struct Contact* ps) {
//memset(ps->data, 0, sizeof(ps->data));
///ps->size = 0;
ps->data = (struct ProInfo*)malloc(3 * sizeof(struct PeoInfo));
if (ps->data==NULL)
{
return;
}
ps->size = 0;
ps->capacity = DEFAULT_SIZE;
LoadContact(ps);
}
void CheckCapacity(struct Contact* ps)
{
if (ps->size==ps->capacity)
{
struct PeoInfo* ptr=(struct PeoInfo*)realloc(ps->data, (ps->capacity + 2) * sizeof(struct PeoInfo));
if (ptr!=NULL)
{
ps->data;
ps->capacity += 2;
printf("ok");
}
else
{
printf("no");
}
}
}
void LoadContact(struct Contact* ps) {
struct PeoInfo tmp = {0};
FILE* pfRead = fopen("contact.bat", "rb");
if (pfRead == NULL)
{
printf("%s", strerror(errno));
return;
}
while (fread(&tmp, sizeof(struct PeoInfo), 1, pfRead))
{
CheckCapacity(ps);
ps->data;
ps->data[ps->size] = tmp;
ps->size++;
}
//fread(&tmp, sizeof(struct PeoInfo), 1, pfRead);
fclose(pfRead);
pfRead = NULL;
}
void AddContact(struct Contact* ps) {
CheckCapacity(ps);
/*if (ps->size==MAX)
{
printf("已经满了");
}
else
{*/
printf("请输入名字");
scanf_s("%s", ps->data[ps->size].name);
printf("请输入年龄");
scanf_s("%d", &(ps->data[ps->size].age));
printf("请输入性别");
scanf_s("%s", ps->data[ps->size].sex);
printf("请输入电话");
scanf_s("%s", ps->data[ps->size].tele);
printf("请输入地址");
scanf_s("%s", ps->data[ps->size].addr);
ps->size++;
}
void ShowContact(const struct Contact* ps)
{
if (ps->size==0)
{
printf("no ");
}
else
{
int i = 0;
printf("%20s\t%4s\t%5s\t%12s\t%20s\n", "名字", "年龄", "性别", "电话", "地址");
for ( i = 0; i < ps->size; i++)
{
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(struct Contact* ps,char name[MAX_NAME]);
static int FindByName(struct Contact* ps, char name[MAX_NAME])
{
for (i = 0; i < ps->size; i++)
{
if (0 == strcmp(ps->data[i].name, name))
{
return 1;
}
}
return -1;
}
void DelContact(struct Contact* ps)
{
char name[20];
printf("名字");
scanf("%s", name);
int pos=FindByName(ps, name);
int i = 0;
if (pos==-1)
{
printf("nonono");
}
else
{
int j = 0;
for ( j = 0; j < ps->size-1; j++)
{
ps->data[j]=ps->data[j + 1];
}
ps->size--;
printf("yes");
}
}
void SearchContact(const struct Contact* ps) {
char name[MAX_NAME];
printf("信息多少");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos==-1)
{
printf("no exist");
}
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(const struct Contact* ps)
{
char name[MAX_NAME];
int pos = 0;
printf("修改信息多少");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos == -1)
{
printf("no exist");
}
else
{
printf("请输入名字");
scanf_s("%s", ps->data[pos].name);
printf("请输入年龄");
scanf_s("%d", &(ps->data[pos].age));
printf("请输入性别");
scanf_s("%s", ps->data[pos].sex);
printf("请输入电话");
scanf_s("%s", ps->data[pos].tele);
printf("请输入地址");
scanf_s("%s", ps->data[pos].addr);
printf("ok");
}
}
void DestoryContact(struct Contact* ps)
{
free(ps->data);
ps->data = NULL;
}
void SaveContact(struct Contact* ps)
{
FILE* ptWrite = fopen("contact.bat", "wb");
if (ptWrite==NULL)
{
printf("%s", strerror(errno));
return ;
}
int i = 0;
for ( i = 0; i < ps->size; i++)
{
fwrite(&(ps->data[i]), sizeof(struct PeoInfo),1,ptWrite);
}
fclose(ptWrite);
ptWrite = NULL;
}