基础版-固定内存大小版本

main.c文件

#include "mail.h"

void menu()
{
printf("************************************\n");
printf("******* WELCOME TO MAIL LIST *******\n");
printf("******* 1.add 2.delete *******\n");
printf("******* 3.search 4.modify *******\n");
printf("******* 5.show 6.sort *******\n");
printf("******* 0.exit maile list *******\n");
printf("************************************\n");
}


int main()
{
int input = 0;
//create mail list
struct Contact con;
//init mail list
init_mail(&con);
do
{
menu();
printf("Please choose operate for mail list>>:");
scanf("%d", &input);
int ret = 0;
switch(input)
{
case ADD:
ret = add_mail(&con);
if(1 == ret)
{
printf("add mail list success.\n");
}
else
{
printf("add mail list failed.\n");
}
break;

case DELETE:
ret = del_mail(&con);
if(1 == ret)
{
printf("delete mail list success.\n");
}
else
{
printf("delete mail list failed.\n");
}
break;

case SEARCH:
search_mail(&con);
break;

case MODIFY:
modify_mail(&con);
break;

case SHOW:
show_mail(&con);
break;

case SORT:
sort_mail(&con);
break;

case EXIT:
printf("exit mail list.\n");
break;

default:
printf("choose error, please input choose operate again.\n");
break;
}
}
while(input);
return 0;
}

mail.h文件

#define MAX 1000

#define NAME_MAX 20
#define SEX_MAX 10
#define TELE_MAX 13
#define EMAIL_MAX 50
#define ADDR_MAX 200

#include <stdio.h>
#include <string.h>

enum Operate
{
EXIT,
ADD,
DELETE,
SEARCH,
MODIFY,
SHOW,
SORT
};

struct PersonInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char tele[TELE_MAX];
char email[EMAIL_MAX];
char addr[ADDR_MAX];
};

struct Contact
{
struct PersonInfo data[MAX];
int size;
};

//Declarative function
void init_mail(struct Contact *ps);

int add_mail(struct Contact *ps);

void show_mail(struct Contact *ps);

int del_mail(struct Contact *ps);

void search_mail(const struct Contact *ps);

void modify_mail(struct Contact *ps);

void sort_mail(struct Contact *ps);

mail.c文件

#include "mail.h"


static int find_by_name_s(const struct Contact *ps, const char *name)
{
for(int i = 0; i < ps->size; i++)
{
if(strcmp(name, ps->data[i].name) == 0)
{
return i;
}
}
return -1;
}


static int find_by_name(const struct Contact *ps)
{
char name[NAME_MAX];
printf("Please input name for mail list>>:");
scanf("%s", name);
int i = 0;
for(i = 0; i < ps->size; i++)
{
if(strcmp(name, ps->data[i].name) == 0)
{
return i;
}
}
return -1;
}


void init_mail(struct Contact *ps)
{
memset(ps->data, 0, sizeof(ps->data));
ps->size = 0;
}


int add_mail(struct Contact *ps)
{
if(ps->size > MAX)
{
printf("Mail list is full, not add.\n");
}
else
{
printf("Please input name>>:");
scanf("%s", ps->data[ps->size].name);
printf("Please input age>>:");
scanf("%d", &(ps->data[ps->size].age));
printf("Please input sex>>:");
scanf("%s", ps->data[ps->size].sex);
printf("Please input tele>>:");
scanf("%s", ps->data[ps->size].tele);
printf("Please input email>>:");
scanf("%s", ps->data[ps->size].email);
printf("Please input addr>>:");
scanf("%s", ps->data[ps->size].addr);
ps->size++;
return 1;
}
return 0;
}


void show_mail(struct Contact *ps)
{
if(ps->size == 0){
printf("Mail list not data.\n");
}
else
{
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
for(int i = 0; i < ps->size; i++)
{
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[i].name, ps->data[i].age, ps->data[i].sex, ps->data[i].tele, ps->data[i].email, ps->data[i].addr);
}
}
}


int del_mail(struct Contact *ps)
{
char name[NAME_MAX];
printf("Please input name for mail list>>:");
scanf("%s", name);

int ret = find_by_name_s(ps, name);

if(-1 == ret)
{
printf("Not find mail info.\n");
return 0;
}

for(int j = ret; j < ps->size - 1; j++)
{
ps->data[j] = ps->data[j+1];
}

ps->size--;
return 1;
}


void search_mail(const struct Contact *ps)
{
int ret = find_by_name(ps);
if(-1 == ret)
{
printf("Not find mail info.\n");

}
else
{
printf("------------------------------------- mail info -------------------------------------\n");
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[ret].name, ps->data[ret].age, ps->data[ret].sex, ps->data[ret].tele, ps->data[ret].email, ps->data[ret].addr);
printf("------------------------------------------ end --------------------------------------\n");
}
}


void modify_mail(struct Contact *ps)
{
int ret = find_by_name(ps);
if(-1 == ret)
{
printf("Not find mail info.\n");
}
else
{
printf("Please input name>>:");
scanf("%s", ps->data[ret].name);
printf("Please input age>>:");
scanf("%d", &(ps->data[ret].age));
printf("Please input sex>>:");
scanf("%s", ps->data[ret].sex);
printf("Please input tele>>:");
scanf("%s", ps->data[ret].tele);
printf("Please input email>>:");
scanf("%s", ps->data[ret].email);
printf("Please input addr>>:");
scanf("%s", ps->data[ret].addr);
printf("************** modify success. **************\n");
printf("------------------------------------- mail info -------------------------------------\n");
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[ret].name, ps->data[ret].age, ps->data[ret].sex, ps->data[ret].tele, ps->data[ret].email, ps->data[ret].addr);
printf("------------------------------------------ end --------------------------------------\n");
}
}


void sort_mail(struct Contact *ps)
{
if(ps->size == 0)
{
printf("Mail list not data.\n");
}
else
{
for(int i = 0; i < ps->size - 1; i++)
{
for(int j = 0; j < ps->size - 1 - i; j++)
{
if(strcmp(ps->data[j].name, ps->data[j+1].name) > 0)
{
struct PersonInfo peio = ps->data[j];
ps->data[j] = ps->data[j+1];
ps->data[j+1] = peio;
}
}

}
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
for(int i = 0; i < ps->size; i++)
{
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[i].name, ps->data[i].age, ps->data[i].sex, ps->data[i].tele, ps->data[i].email, ps->data[i].addr);
}
}
}

中级版-动态开辟内存版本

main.c文件

#include "mail.h"

void menu()
{
printf("************************************\n");
printf("******* WELCOME TO MAIL LIST *******\n");
printf("******* 1.add 2.delete *******\n");
printf("******* 3.search 4.modify *******\n");
printf("******* 5.show 6.sort *******\n");
printf("******* 0.exit maile list *******\n");
printf("************************************\n");
}


int main()
{
int input = 0;
//create mail list
struct Contact con;
//init mail list
init_mail(&con);
do
{
menu();
printf("Please choose operate for mail list>>:");
scanf("%d", &input);
int ret = 0;
switch(input)
{
case ADD:
ret = add_mail(&con);
if(1 == ret)
{
printf("add mail list success.\n");
}
else
{
printf("add mail list failed.\n");
}
break;

case DELETE:
ret = del_mail(&con);
if(1 == ret)
{
printf("delete mail list success.\n");
}
else
{
printf("delete mail list failed.\n");
}
break;

case SEARCH:
search_mail(&con);
break;

case MODIFY:
modify_mail(&con);
break;

case SHOW:
show_mail(&con);
break;

case SORT:
sort_mail(&con);
break;

case EXIT:
exit_mail(&con);
printf("exit mail list.\n");
break;

default:
printf("choose error, please input choose operate again.\n");
break;
}
}
while(input);
return 0;
}

mail.h文件

#define INIT_MAX 3
#define NAME_MAX 20
#define SEX_MAX 10
#define TELE_MAX 13
#define EMAIL_MAX 50
#define ADDR_MAX 200

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

enum Operate
{
EXIT,
ADD,
DELETE,
SEARCH,
MODIFY,
SHOW,
SORT
};

struct PersonInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char tele[TELE_MAX];
char email[EMAIL_MAX];
char addr[ADDR_MAX];
};

struct Contact
{
struct PersonInfo *data;
int size;
int capacity;
};

//Declarative function
void init_mail(struct Contact *ps);

int add_mail(struct Contact *ps);

void show_mail(struct Contact *ps);

int del_mail(struct Contact *ps);

void search_mail(const struct Contact *ps);

void modify_mail(struct Contact *ps);

void sort_mail(struct Contact *ps);

void exit_mail(struct Contact *ps);

mail.c文件

#include "mail.h"


static int find_by_name_s(const struct Contact *ps, const char *name)
{
for(int i = 0; i < ps->size; i++)
{
if(strcmp(name, ps->data[i].name) == 0)
{
return i;
}
}
return -1;
}


static int find_by_name(const struct Contact *ps)
{
char name[NAME_MAX];
printf("Please input name for mail list>>:");
scanf("%s", name);
int i = 0;
for(i = 0; i < ps->size; i++)
{
if(strcmp(name, ps->data[i].name) == 0)
{
return i;
}
}
return -1;
}

void check_capacity(struct Contact *ps)
{
if(ps->size == ps->capacity)
{
// need to add two element memory
struct PersonInfo *ptd = (struct PersonInfo*)realloc(ps->data, (ps->capacity + 2) * sizeof(struct PersonInfo));
if(ptd == NULL)
{
printf("Add person info memory failed [%s]\n", strerror(errno));
return;
}
ps->data = ptd;
ps->capacity += 2;
printf("Add person info memory success.\n");
return;
}
return;
}

void init_mail(struct Contact *ps)
{
// method one, easy
// memset(ps->data, 0, sizeof(ps->data));
// ps->size = 0;
// method two
ps->data = (struct PersonInfo*)malloc(INIT_MAX * sizeof(struct PersonInfo));
if(ps->data == NULL)
{
printf("Init person info memory failed [%s]\n", strerror(errno));
return;
}
ps->capacity = INIT_MAX;
ps->size = 0;
printf("Init person info memory success.\n");
return;
}


int add_mail(struct Contact *ps)
{
//check person info memory exist
check_capacity(ps);

printf("Please input name>>:");
scanf("%s", ps->data[ps->size].name);
printf("Please input age>>:");
scanf("%d", &(ps->data[ps->size].age));
printf("Please input sex>>:");
scanf("%s", ps->data[ps->size].sex);
printf("Please input tele>>:");
scanf("%s", ps->data[ps->size].tele);
printf("Please input email>>:");
scanf("%s", ps->data[ps->size].email);
printf("Please input addr>>:");
scanf("%s", ps->data[ps->size].addr);
ps->size++;
return 1;
}


void show_mail(struct Contact *ps)
{
if(ps->size == 0){
printf("Mail list not data.\n");
}
else
{
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
for(int i = 0; i < ps->size; i++)
{
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[i].name, ps->data[i].age, ps->data[i].sex, ps->data[i].tele, ps->data[i].email, ps->data[i].addr);
}
}
}


int del_mail(struct Contact *ps)
{
char name[NAME_MAX];
printf("Please input name for mail list>>:");
scanf("%s", name);

int ret = find_by_name_s(ps, name);

if(-1 == ret)
{
printf("Not find mail info.\n");
return 0;
}

for(int j = ret; j < ps->size - 1; j++)
{
ps->data[j] = ps->data[j+1];
}

ps->size--;
return 1;
}


void search_mail(const struct Contact *ps)
{
int ret = find_by_name(ps);
if(-1 == ret)
{
printf("Not find mail info.\n");

}
else
{
printf("------------------------------------- mail info -------------------------------------\n");
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[ret].name, ps->data[ret].age, ps->data[ret].sex, ps->data[ret].tele, ps->data[ret].email, ps->data[ret].addr);
printf("------------------------------------------ end --------------------------------------\n");
}
}


void modify_mail(struct Contact *ps)
{
int ret = find_by_name(ps);
if(-1 == ret)
{
printf("Not find mail info.\n");
}
else
{
printf("Please input name>>:");
scanf("%s", ps->data[ret].name);
printf("Please input age>>:");
scanf("%d", &(ps->data[ret].age));
printf("Please input sex>>:");
scanf("%s", ps->data[ret].sex);
printf("Please input tele>>:");
scanf("%s", ps->data[ret].tele);
printf("Please input email>>:");
scanf("%s", ps->data[ret].email);
printf("Please input addr>>:");
scanf("%s", ps->data[ret].addr);
printf("************** modify success. **************\n");
printf("------------------------------------- mail info -------------------------------------\n");
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[ret].name, ps->data[ret].age, ps->data[ret].sex, ps->data[ret].tele, ps->data[ret].email, ps->data[ret].addr);
printf("------------------------------------------ end --------------------------------------\n");
}
}


void sort_mail(struct Contact *ps)
{
if(ps->size == 0)
{
printf("Mail list not data.\n");
}
else
{
for(int i = 0; i < ps->size - 1; i++)
{
for(int j = 0; j < ps->size - 1 - i; j++)
{
if(strcmp(ps->data[j].name, ps->data[j+1].name) > 0)
{
struct PersonInfo peio = ps->data[j];
ps->data[j] = ps->data[j+1];
ps->data[j+1] = peio;
}
}

}
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
for(int i = 0; i < ps->size; i++)
{
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[i].name, ps->data[i].age, ps->data[i].sex, ps->data[i].tele, ps->data[i].email, ps->data[i].addr);
}
}
}

void exit_mail(struct Contact *ps)
{
free(ps->data);
ps->data = NULL;
return;
}

高级版-数据文件存储版本

main.c文件

#include "mail.h"

void menu()
{
printf("************************************\n");
printf("******* WELCOME TO MAIL LIST *******\n");
printf("******* 1.add 2.delete *******\n");
printf("******* 3.search 4.modify *******\n");
printf("******* 5.show 6.sort *******\n");
printf("******* 0.exit maile list *******\n");
printf("************************************\n");
}


int main()
{
int input = 0;
//create mail list
struct Contact con;
//init mail list
init_mail(&con);
do
{
menu();
printf("Please choose operate for mail list>>:");
scanf("%d", &input);
int ret = 0;
switch(input)
{
case ADD:
ret = add_mail(&con);
if(1 == ret)
{
printf("add mail list success.\n");
}
else
{
printf("add mail list failed.\n");
}
break;

case DELETE:
ret = del_mail(&con);
if(1 == ret)
{
printf("delete mail list success.\n");
}
else
{
printf("delete mail list failed.\n");
}
break;

case SEARCH:
search_mail(&con);
break;

case MODIFY:
modify_mail(&con);
break;

case SHOW:
show_mail(&con);
break;

case SORT:
sort_mail(&con);
break;

case EXIT:
save_mail(&con);
exit_mail(&con);
printf("exit mail list.\n");
break;

default:
printf("choose error, please input choose operate again.\n");
break;
}
}
while(input);
return 0;
}

mail.h文件

#define INIT_MAX 3
#define NAME_MAX 20
#define SEX_MAX 10
#define TELE_MAX 13
#define EMAIL_MAX 50
#define ADDR_MAX 200

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

enum Operate
{
EXIT,
ADD,
DELETE,
SEARCH,
MODIFY,
SHOW,
SORT
};

struct PersonInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char tele[TELE_MAX];
char email[EMAIL_MAX];
char addr[ADDR_MAX];
};

struct Contact
{
struct PersonInfo *data;
int size;
int capacity;
};

//Declarative function
void init_mail(struct Contact *ps);

int add_mail(struct Contact *ps);

void show_mail(struct Contact *ps);

int del_mail(struct Contact *ps);

void search_mail(const struct Contact *ps);

void modify_mail(struct Contact *ps);

void sort_mail(struct Contact *ps);

void exit_mail(struct Contact *ps);

void save_mail(struct Contact *ps);

int load_mail(struct Contact *ps);

mail.c文件

#include "mail.h"


static int find_by_name_s(const struct Contact *ps, const char *name)
{
for(int i = 0; i < ps->size; i++)
{
if(strcmp(name, ps->data[i].name) == 0)
{
return i;
}
}
return -1;
}


static int find_by_name(const struct Contact *ps)
{
char name[NAME_MAX];
printf("Please input name for mail list>>:");
scanf("%s", name);
int i = 0;
for(i = 0; i < ps->size; i++)
{
if(strcmp(name, ps->data[i].name) == 0)
{
return i;
}
}
return -1;
}

void check_capacity(struct Contact *ps)
{
if(ps->size == ps->capacity)
{
// need to add two element memory
struct PersonInfo *ptd = (struct PersonInfo*)realloc(ps->data, (ps->capacity + 2) * sizeof(struct PersonInfo));
if(ptd == NULL)
{
printf("Add person info memory failed [%s]\n", strerror(errno));
return;
}
ps->data = ptd;
ps->capacity += 2;
printf("Add person info memory success.\n");
return;
}
return;
}

int load_mail(struct Contact *ps)
{
FILE *pf = fopen("./contact.dat", "rb+");
if(pf == NULL)
{
printf("load_mail::open [contact.dat] failed, reason [%s].\n", strerror(errno));
return 1;
}
//读取数据
struct PersonInfo tem = {0};
while(fread(&tem, sizeof(struct PersonInfo), 1, pf))
{
check_capacity(ps);
ps->data[ps->size] = tem;
ps->size++;
}
fclose(pf);
pf = NULL;
return 0;
}

void init_mail(struct Contact *ps)
{
// method two
ps->data = (struct PersonInfo*)malloc(INIT_MAX * sizeof(struct PersonInfo));
if(ps->data == NULL)
{
printf("Init person info memory failed [%s]\n", strerror(errno));
return;
}
ps->capacity = INIT_MAX;
ps->size = 0;
printf("Init person info memory success.\n");
// load data from contact.dat
int ret = load_mail(ps);
if(ret == 0)
{
printf("load data from [contact.dat] success.\n");
}
else
{
printf("load data from [contact.dat] failed.\n");
}
return;
}


int add_mail(struct Contact *ps)
{
//check person info memory exist
check_capacity(ps);

printf("Please input name>>:");
scanf("%s", ps->data[ps->size].name);
printf("Please input age>>:");
scanf("%d", &(ps->data[ps->size].age));
printf("Please input sex>>:");
scanf("%s", ps->data[ps->size].sex);
printf("Please input tele>>:");
scanf("%s", ps->data[ps->size].tele);
printf("Please input email>>:");
scanf("%s", ps->data[ps->size].email);
printf("Please input addr>>:");
scanf("%s", ps->data[ps->size].addr);
ps->size++;
return 1;
}


void show_mail(struct Contact *ps)
{
if(ps->size == 0){
printf("Mail list not data.\n");
}
else
{
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
for(int i = 0; i < ps->size; i++)
{
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[i].name, ps->data[i].age, ps->data[i].sex, ps->data[i].tele, ps->data[i].email, ps->data[i].addr);
}
}
}


int del_mail(struct Contact *ps)
{
char name[NAME_MAX];
printf("Please input name for mail list>>:");
scanf("%s", name);

int ret = find_by_name_s(ps, name);

if(-1 == ret)
{
printf("Not find mail info.\n");
return 0;
}

for(int j = ret; j < ps->size - 1; j++)
{
ps->data[j] = ps->data[j+1];
}

ps->size--;
return 1;
}


void search_mail(const struct Contact *ps)
{
int ret = find_by_name(ps);
if(-1 == ret)
{
printf("Not find mail info.\n");

}
else
{
printf("------------------------------------- mail info -------------------------------------\n");
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[ret].name, ps->data[ret].age, ps->data[ret].sex, ps->data[ret].tele, ps->data[ret].email, ps->data[ret].addr);
printf("------------------------------------------ end --------------------------------------\n");
}
}


void modify_mail(struct Contact *ps)
{
int ret = find_by_name(ps);
if(-1 == ret)
{
printf("Not find mail info.\n");
}
else
{
printf("Please input name>>:");
scanf("%s", ps->data[ret].name);
printf("Please input age>>:");
scanf("%d", &(ps->data[ret].age));
printf("Please input sex>>:");
scanf("%s", ps->data[ret].sex);
printf("Please input tele>>:");
scanf("%s", ps->data[ret].tele);
printf("Please input email>>:");
scanf("%s", ps->data[ret].email);
printf("Please input addr>>:");
scanf("%s", ps->data[ret].addr);
printf("************** modify success. **************\n");
printf("------------------------------------- mail info -------------------------------------\n");
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[ret].name, ps->data[ret].age, ps->data[ret].sex, ps->data[ret].tele, ps->data[ret].email, ps->data[ret].addr);
printf("------------------------------------------ end --------------------------------------\n");
}
}


void sort_mail(struct Contact *ps)
{
if(ps->size == 0)
{
printf("Mail list not data.\n");
}
else
{
for(int i = 0; i < ps->size - 1; i++)
{
for(int j = 0; j < ps->size - 1 - i; j++)
{
if(strcmp(ps->data[j].name, ps->data[j+1].name) > 0)
{
struct PersonInfo peio = ps->data[j];
ps->data[j] = ps->data[j+1];
ps->data[j+1] = peio;
}
}

}
printf("%s\t%3s\t%3s\t%11s\t%20s\t%20s\n", "name", "age", "sex", "tele", "email", "addr");
for(int i = 0; i < ps->size; i++)
{
printf("%s\t%3d\t%3s\t%11s\t%20s\t%20s\n", ps->data[i].name, ps->data[i].age, ps->data[i].sex, ps->data[i].tele, ps->data[i].email, ps->data[i].addr);
}
}
}

void exit_mail(struct Contact *ps)
{
free(ps->data);
ps->data = NULL;
return;
}

void save_mail(struct Contact *ps)
{
FILE *pf = fopen("./contact.dat", "wb+");
if(pf == NULL)
{
printf("save_mail::open [contact.dat] failed, reason [%s].\n", strerror(errno));
return;
}
// write contact data for contact.dat
for(int i = 0; i < ps->size; i++)
{
fwrite (&(ps->data[i]), sizeof(struct PersonInfo), 1, pf);
}
fclose(pf);
pf = NULL;
printf("[contact.dat] add success.\n");
return;
}