文件版本的通讯录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lu_1079776757/article/details/79963966

之前我们实现了静态通讯录和动态通讯录,今天我们来实现文件版本的通讯录。直接上代码:

test.c

#include"address_list.h"

void address_list(PADDRESS plist)//通讯录主函数
{
    int input = 0;
    do
    {
        menu2();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case EXIT:
            break;
        case ADD:
            add(plist);
            break;
        case DELE:
            dele(plist);
            break;
        case SEARCH:
            search(plist);
            break;
        case MODIFY:
            modify(plist);
            break;
        case SHOW:
            show(plist);
            break;
        case CLEAR:
            clear(plist);
            break;
        case SORT:
            sort(plist);
            break;
        default:
            printf("输入有误,请重新输入!\n");
            break;
        }
    } while (input);
}

void menu1()
{
    printf("************************\n");
    printf("**** 0.EXIT  1.OPEN ****\n");
    printf("************************\n");
}

void test()
{
    int input = 0;
    ADDRESS list;
    FILE *pfile = fopen("address_list.txt", "r");//以读的方式打开文件
    loading(pfile,&list);//加载函数,加载文件中的信息进入程序
    fclose(pfile);//关闭文件
    do
    {
        menu1();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case 0:
            break;
        case 1:
            address_list(&list);
            break;
        default:
            printf("输入有误,请重新输入!\n");
            break;
        }
    } while (input);
    pfile = fopen("address_list.txt", "w");//以写的方式打开文件
    unload(pfile,&list);//卸载函数
    fclose(pfile);//关闭文件
    free(list.pcon);//释放动态分配的内存空间
}

int main()
{
    test();
    return 0;
}

address_list.h

#ifndef __ADDRESS_LIST__
#define __ADDRESS_LIST__

#define _CRT_SECURE_NO_WARNINGS 1

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


//数据结构

#define NAME_MAX 12//最大姓名长度
#define SEX_MAX 6//最大性别长度
#define PHONE_MAX 13//最大电话长度
#define ADDRESS_MAX 20//最大地址长度
#define INCREASE 10//每次增加元素个数


typedef struct list//联系人数据结构
{
    char name[NAME_MAX];
    char sex[SEX_MAX];
    int age;
    char phone[PHONE_MAX];
    char address[ADDRESS_MAX];
}CON,*PCON;

typedef struct//通讯录数据结构
{
    PCON pcon;
    int stock;//库存
    int depot;//仓库
}ADDRESS,*PADDRESS;

enum
{
    EXIT,
    ADD,
    DELE,
    SEARCH,
    MODIFY,
    SHOW,
    CLEAR,
    SORT
};


//功能函数实现

void menu2();

void add(PADDRESS pl);//添加函数
void dele(PADDRESS pl);//删除函数
void search(PADDRESS pl);//查找函数
void modify(PADDRESS pl);//修改函数
void show(PADDRESS pl);//显示函数
void clear(PADDRESS pl);//清空函数
void sort(PADDRESS pl);//排序函数


void loading(FILE *pf, PADDRESS pl);
void unload(FILE *pf, PADDRESS pl);


#endif//__ADDRESS_LIST__

address_list.c

#include"address_list.h"

void loading(FILE *pf, PADDRESS pl)//加载文件内容函数
{
    fscanf(pf, "%d", &(pl->stock));
    if (pl->stock == 0)
    {
        pl->pcon = NULL;
        pl->depot = 0;
        printf("Loading success.\n");
    }
    else
    {
        int i = 0;
        PCON tmp = NULL;
        tmp = (PCON)malloc((pl->stock)*sizeof(CON));
        if (tmp == NULL)
        {
            printf("Loading file flunk\n");
        }
        else
        {
            pl->pcon = tmp;
            pl->depot = pl->stock;
            for (i = 0; i < pl->stock; i++)
            {
                fscanf(pf, "%s%s%d%s%s\n", (pl->pcon + i)->name, (pl->pcon + i)->sex, &((pl->pcon + i)->age), (pl->pcon + i)->phone, (pl->pcon + i)->address);
            }
            printf("Loading success.\n");
        }
    }
}
void unload(FILE *pf, PADDRESS pl)//卸载函数,将通讯录中的信息存入文件中。
{
    fprintf(pf, "%d ", pl->stock);
    if (pl->stock == 0)
    {
        ;
    }
    else
    {
        int i = 0;
        for (i = 0; i < pl->stock; i++)
        {
            fprintf(pf, "%s %s %d %s %s\n", (pl->pcon + i)->name, (pl->pcon + i)->sex, (pl->pcon + i)->age, (pl->pcon + i)->phone, (pl->pcon + i)->address);
        }
        printf("Unload success.\n");
    }

}




void menu2()//功能菜单函数实现
{
    printf("*********************************\n");
    printf("****  0.EXIT      1.ADD     *****\n");
    printf("****  2.DELETE    3.SEARCH  *****\n");
    printf("****  4.MODIFY    5.SHOW    *****\n");
    printf("****  6.CLEAR     7.SORT    *****\n");
    printf("*********************************\n");
}

static int hunt(PADDRESS pl, const char *sour)//公共模块——搜寻函数
{
    int i = 0;
    for (i = 0; i < pl->stock; i++)
    {
        if (strcmp((pl->pcon + i)->name, sour) == 0)
        {
            return i;
        }
    }
    return -1;
}

void add(PADDRESS pl)//添加函数
{
    if (pl->stock == pl->depot)
    {
        PCON tmp = (PCON)realloc(pl->pcon, (pl->depot + INCREASE)*sizeof(CON));
        if (tmp == NULL)
        {
            printf("Realloc flunk!\n");
        }
        else
        {
            pl->pcon = tmp;
            pl->depot += INCREASE;
            goto flag;
        }
    }
    else
    {
    flag:
        printf("请输入联系人姓名:>");
        scanf("%s", (pl->pcon + pl->stock)->name);
        printf("请输入联系人性别:>");
        scanf("%s", (pl->pcon + pl->stock)->sex);
        printf("请输入联系人年龄:>");
        scanf("%d", &((pl->pcon + pl->stock)->age));
        printf("请输入联系人电话:>");
        scanf("%s", (pl->pcon + pl->stock)->phone);
        printf("请输入联系人地址:>");
        scanf("%s", (pl->pcon + pl->stock)->address);
        pl->stock++;
        printf("添加联系人成功!\n");
    }
}


void dele(PADDRESS pl)//删除函数
{
    char tmp[NAME_MAX] = { 0 };
    int ret = 0;
    printf("请输入要删除联系人姓名:>");
    scanf("%s", tmp);
    ret = hunt(pl, tmp);
    if (ret == -1)
    {
        printf("未找到所要删除的联系人!\n");
    }
    else
    {
        for (; ret < pl->stock-1; ret++)
        {
            *(pl->pcon + ret) = *(pl->pcon + ret + 1);
        }
        pl->stock--;
        printf("删除联系人成功!\n");
    }
}


void search(PADDRESS pl)//查找函数
{
    char tmp[NAME_MAX] = { 0 };
    int ret = 0;
    printf("请输入要查找联系人姓名:>");
    scanf("%s", tmp);
    ret = hunt(pl, tmp);
    if (ret == -1)
    {
        printf("未找到所要查找的联系人!\n");
    }
    else
    {
        printf("所要查找的联系人下标为:%d\n", ret + 1);
    }
}
void modify(PADDRESS pl)//修改函数
{
    char tmp[NAME_MAX] = { 0 };
    int ret = 0;
    printf("请输入要查找联系人姓名:>");
    scanf("%s", tmp);
    ret = hunt(pl, tmp);
    if (ret == -1)
    {
        printf("未找到所要修改的联系人!\n");
    }
    else
    {
        printf("请输入联系人姓名:>");
        scanf("%s", (pl->pcon + ret)->name);
        printf("请输入联系人性别:>");
        scanf("%s", (pl->pcon + ret)->sex);
        printf("请输入联系人年龄:>");
        scanf("%d", &((pl->pcon + ret)->age));
        printf("请输入联系人电话:>");
        scanf("%s", (pl->pcon + ret)->phone);
        printf("请输入联系人地址:>");
        scanf("%s", (pl->pcon + ret)->address);
        printf("修改联系人成功!\n");
    }
}

void show(PADDRESS pl)//显示函数
{
    int i = 0;
    printf("%-13s %-6s %-3s %-12s %-12s\n", "NAME", "SEX", "AGE", "PHONE", "ADDRESS");
    for (i = 0; i < pl->stock; i++)
    {
        printf("%-13s %-6s %-3d %-12s %-12s\n", (pl->pcon + i)->name, (pl->pcon + i)->sex, (pl->pcon + i)->age, (pl->pcon + i)->phone, (pl->pcon + i)->address);
    }
}


void clear(PADDRESS pl)
{
    free(pl->pcon);
    pl->pcon = NULL;
    pl->depot = 0;
    pl->stock = 0;
}


void sort(PADDRESS pl)//排序函数
{
    struct list tmp = { 0 };
    int i = 0;
    for (i = 0; i < pl->stock - 1; i++)
    {
        int j = 0;
        for (j = 0; j < pl->stock - 1 - i; j++)
        {
            if (strcmp((pl->pcon + j)->name, (pl->pcon + j + 1)->name) > 0)
            {
                tmp = *(pl->pcon + j);
                *(pl->pcon + j) = *(pl->pcon + j + 1);
                *(pl->pcon + j + 1) = tmp;
            }
        }
    }
    printf("排序成功!\n");
}


阅读更多

没有更多推荐了,返回首页