c语言编写动态图片,用C语言编写通讯录(动态版+文件版)

#define _CRT_SECURE_NO_WARNINGS -1

#include "function.h"

int find(Abook book, char name[10])

{

int i = 0;

for (i = 0; i < book->size; i++)

{

if (strcmp((const char *)book->contact[i].name , name) == 0)

return i;

}

return -1;

}

void check_capacity(Abook book)//是否需要扩容

{

if (book->size == book->capacity)

{

int len = book->capacity + 10;

content * tmp = (content *)realloc(book->contact, len*sizeof(content));

//realloc扩容失败时会返回 NULL,不能直接让pcon->contact接受

book->capacity += 10;

if (tmp == NULL)

{

printf("extend fail");

return;

}

book->contact = tmp;

}

}

void load(Abook book, content *p)//把一个联系人的信息加载到内存中

{

assert(book);

check_capacity(book);

book->contact[book->size] = *p;

book->size++;

}

void read_data(Abook book)//读取文件中的信息

{

content content = {0};

FILE *pFread = fopen("content.txt", "r");

if (pFread == NULL)

{

perror("open file contact.dat for read error");

exit(1);

}

while (fread(&content, sizeof(content), 1, pFread))

{

load(book, &content);

}

}

void save_data(Abook book)//把数据保存到文件中

{

int i = 0;

FILE *pFWrite = fopen("content.txt", "w");

if (pFWrite == NULL)

{

perror("open file contact.txt for write error");

exit(1);

}

for (i = 0; i< book->size; i++)

{

fwrite(&(book->contact[i]), sizeof(content), 1, pFWrite);

}

free(book->contact);

fclose(pFWrite);

}

void init_contact(Abook book)//初始化通讯录

{

book->size = 0;

book->contact = (content *)malloc(sizeof(content)*10);

memset(book->contact, 0, 10 * sizeof(content));

book->capacity = 10;

read_data(book);

}

void add_contact(Abook book) //添加新联系人

{

assert(book);

check_capacity(book);

char name[10] = { 0 };

printf("Please enter the name: ");

scanf("%s", name);

int ret = find(book, name);

if (-1 == ret)

{

strcpy(book->contact[book->size].name, name);

printf("Please enter the sex: ");

scanf("%s", book->contact[book->size].sex);

printf("Please enter the age: ");

scanf("%d", &(book->contact[book->size].age));

printf("Please enter the tele: ");

scanf("%s", book->contact[book->size].telephone);

printf("Please enter the addr: ");

scanf("%s", book->contact[book->size].address);

book->size++; //通讯录内联系人数量

printf("Add successfully!\n");

system("pause");

system("cls");

}

else

{

printf("The contact already exists!\n");

system("pause");

system("cls");

}

}

void delete_contact(Abook book) //删除联系人

{

assert(book);

printf("Please enter the name of the contact you want to delete: ");

char name[10];

scanf("%s", name);

int ret = find(book, name);

if (ret == -1)

{

printf("Delete failed! The contact was not found! \n");

system("pause");

system("cls");

}

else

{

int start = 0;

for (start = ret; start < book->size - 1; start++)//这种方法不会打乱通讯录排序,方法二是把要删除的联系人和最后一个联系人交换位置,然后size--。

{

book->contact[start] = book->contact[start + 1];

}

book->size--;

printf("Delete successfully!\n");

system("pause");

system("cls");

}

}

void search_contact(Abook book) //查找联系人

{

assert(book);

printf("Please enter the name of the contact you want to search: ");

char name[10];

scanf("%s", &name);

int ret = find(book, name);

if (ret >= 0 && ret <= book->capacity)

{

printf("%-7s\t%-6s\t%-6s\t%-10s\t%-10s\n", "name", "age", "sex", "tele", "addr");

printf("%-7s\t%-6d\t%-6s\t%-10s\t%-10s\n",

book->contact[ret].name,

book->contact[ret].age,

book->contact[ret].sex,

book->contact[ret].telephone,

book->contact[ret].address);

system("pause");

system("cls");

}

else

{

printf("Search failed! The contact was not found! \n");

system("pause");

system("cls");

}

}

void modify_contact(Abook book) //修改联系人信息

{

assert(book);

printf("Please enter the name of the contact you want to modify: ");

char name[20];

scanf("%s", name);

int ret = find(book, name);

if (-1 == ret)

{

printf("Modify failed! The contact was not found!\n");

system("pause");

system("cls");

}

else

{

printf("Please enter the revised name: ");

scanf("%s", book->contact[ret].name);

printf("Please enter the revised sex: ");

scanf("%s", book->contact[ret].sex);

printf("Please enter the revised age: ");

scanf("%d", &(book->contact[ret].age));

printf("Please enter the revised tele: ");

scanf("%s", book->contact[ret].telephone);

printf("Please enter the revised addr: ");

scanf("%s", book->contact[ret].address);

printf("\n");

printf("Modify successfully!\n");

system("pause");

system("cls");

}

}

void show_all_contacts(Abook book) //打印所有联系人信息

{

assert(book);

int i = 0;

if (book->size > 0)

{

printf("%-7s\t%-6s\t%-6s\t%-10s\t%-10s\n", "name", "age", "sex", "tele", "addr");

for (i = 0; i < book->size; i++)

{

printf("%-7s\t%-6d\t%-6s\t%-10s\t%-10s\n",

book->contact[i].name,

book->contact[i].age,

book->contact[i].sex,

book->contact[i].telephone,

book->contact[i].address);

}

system("pause");

system("cls");

}

else

{

printf("The address book is empty!\n");

system("pause");

system("cls");

}

}

void empty_all_contacts(Abook book) //清空通讯录所有信息

{

assert(book);

book->size = 0;

printf("The address book has been emptied!\n");

system("pause");

system("cls");

}

void sort_all_contacts(Abook book) //根据名字从小到大排序联系人

{

assert(book);

if (0 == book->size)

{

printf("Sort failed! The address book is empty!\n");

system("pause");

system("cls");

}

else

{

int i = 0;

int j = 0;

for (i = 0; i < book->size-1; i++)

for (j = 0; j < book->size-1 - i; j++)

{

if (strcmp(book->contact[j].name, book->contact[j+1].name) == 1)

{

content tmp = book->contact[j];

book->contact[j] = book->contact[j+1];

book->contact[j+1] = tmp;

}

}

printf("The address book has been sorted! \n");

system("pause");

system("cls");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值