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

来源:互联网 发布:广电网络整合最新消息 编辑:程序博客网 时间:2024/06/04 21:15

实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人

ps:通讯录的容量大小可变,修改的通讯录可以保存到文件中

代码1:function.h

#ifndef   __FUNCTION_H__#define   __FUNCTION_H__#include <stdio.h>#include <string.h>#include <windows.h>#include <assert.h>typedef struct content{    char name[10];    char sex[4];    int age;    char telephone[12];    char address[20];}content;typedef struct AddressBook{    struct content* contact;    int size;//通讯录中联系人个数    int capacity;//通讯录容量}Addbook, *Abook;int find(Abook book, char name[]);void check_capacity(Abook book);void init_contact(Abook book);void add_contact(Abook book);void delete_contact(Abook book);void search_contact(Abook book);void modify_contact(Abook book);void show_all_contacts(Abook book);void empty_all_contacts(Abook book);void sort_all_contacts(Abook book);void save_data(Abook book);#endif

代码2:function.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");    }}

代码3:test.c

#include "function.h"void menu(){    printf("**************AddressBook***************\n");    printf("*     1.add              2.delete      *\n");    printf("*     3.search           4.modify      *\n");    printf("*     5.show_all         6.empty_all   *\n");    printf("*     7.sort_all         0.exit        *\n");    printf("****************************************\n\n");}void test(){    content my_friends;    init_contact(&my_friends);    do    {        menu();        printf("please select:");        int input = 0;        scanf_s("%d", &input);        printf("\n");        if (input > 0 && input < 8)        {            switch (input)            {            case 1:                add_contact(&my_friends);                break;            case 2:                delete_contact(&my_friends);                break;            case 3:                search_contact(&my_friends);                break;            case 4:                modify_contact(&my_friends);                break;            case 5:                show_all_contacts(&my_friends);                break;            case 6:                empty_all_contacts(&my_friends);                break;            case 7:                sort_all_contacts(&my_friends);                break;            default:                break;            }        }        else if (0 == input)        {            save_data(&my_friends);            exit(0);        }           else        {            printf("Select error, try again\n");            system("pause");            system("cls");        }    } while (1);}int main(){    test();    return 0;}

运行截图:
1.添加联系人成功:
添加成功

添加联系人失败(通讯录已满):
这里写图片描述

2.删除联系人成功:
这里写图片描述

删除失败(无该联系人):
这里写图片描述

3.查找联系人成功:
这里写图片描述

查找失败(无该联系人):
这里写图片描述

4.修改联系人成功:
这里写图片描述

修改失败(无该联系人):
这里写图片描述

5.显示所有联系人:
这里写图片描述

6.清空所有联系人:
这里写图片描述

清空后结果:
这里写图片描述

7.排序所有联系人:
这里写图片描述

排序结果:
这里写图片描述

原创粉丝点击