通讯录-动态内存开辟-文件储存

头文件maillist.h

#ifndef __MAILLIST_H__
#define __MAILLIST_H__

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

#define M 2 //默认通讯录大小


enum ER
{
    Expand = 1,
    Reduce

};
struct Message
{
    char name[20];//姓名
    char sex[10]; //性别
    int age;// 年龄
    char telephone[20]; //电话
    char address[100]; //地址

};
struct Volume  //通讯轮的容量
{
    int Modern; //当前最大的储存空间
    int Exist;   //现在存储的空间
    struct Message *M_people;
};

void InitVolume(struct Volume  *vl); //初始化函数
void ExpandReduce(struct Volume  *vl);//扩大或缩小通讯录空间
void Free(struct Volume  *vl);//释放动态内存大小

void AddPerson(struct Volume  *vl);//添加联系人信息 
void DeletePerson(struct Volume  *vl);//删除指定联系人信息
void QueryPerson(struct Volume  *vl);//查找指定联系人信息
void ModifyPerson(struct Volume  *vl);//修改指定联系人信息
void LookPerson(struct Volume  *vl);//显示所有联系人信息
void EmptyPerson(struct Volume  *vl);//清空所有联系人
void SortPerson(struct Volume  *vl);//以名字排序所有联系人

void SavePerson(struct Volume  *vl);//以二进制文件形式保存
void ReadPerson(struct Volume  *vl);//以二进制文件形式读取



#endif //__MAILLIST_H__

主程序maillist.c

#define  _CRT_SECURE_NO_WARNINGS 1

#include "maillist.h"

void InitVolume(struct Volume  *vl)         
{
    vl->Exist = 0;
    vl->Modern = M;
    vl->M_people =(struct Message *)malloc(sizeof(struct Message)*M);//出现错误的地方,
    if(vl->M_people == NULL) //申请内存申请失败
    {
        printf("        Out of memory!\n");
        exit(EXIT_FAILURE);
    }
    memset(vl->M_people,0,M*sizeof(struct Message));//通讯录初始化为0
}
void menu2()
{
    printf("        *****************************\n");
    printf("        ****        1.扩大       ****\n");
    printf("        ****        2.减小       ****\n");
    printf("        *****************************\n");


}
//void  go_menu()
//{
//  char c = 0;
//  printf("返回菜单请按:Y\n");
//  fflush(stdin);
//  while(1)
//  {
//      scanf("%c",&c);
//      if('Y' == c)
//      {
//          return;
//      }
//      
//  }
//}
void ExpandReduce(struct Volume  *vl)
{
    int input2 = 0;
    int num = 0;
    struct Message *str = NULL;
    assert(vl);
    menu2();
    printf("请选择->");
    fflush(stdin);
    scanf("%d",&input2);
    if(Expand == input2)
    {
        printf("请输入你想要扩大的数量\n");
        fflush(stdin);
        scanf("%d",&num);

        str = (struct Message *)realloc(vl->M_people,sizeof(struct Message)*(vl->Modern + num));

        if(NULL == str)
        {
            printf("Out of memory!\n");
            exit(EXIT_FAILURE);
        }
        else
        {
             vl->M_people = str;
             vl->Modern = vl->Modern + num;
        }
    }
    if(Reduce == input2)
    {
        printf("请输入你想要缩小的数量\n");
        fflush(stdin);
        scanf("%d",&num);
        if(vl->Exist > (vl->Modern - num))
        {
            printf("缩小空间过小,不足以容纳当前人数\n");
            return ;
        }
        str = (struct Message *)realloc(vl->M_people,sizeof(struct Message)*(vl->Modern - num));
        if(NULL == str)
        {
            printf("Out of memory!\n");
            exit(EXIT_FAILURE);
        }
        else
        {
             vl->M_people = str;
             vl->Modern = vl->Modern - num;
        }
    }
}
void Free(struct Volume  *vl)
{
    if(vl->M_people != NULL)
    {
        free(vl->M_people);
    }
    vl->M_people = NULL;
}
void AddPerson(struct Volume  *vl)
{
    int i = 0;
    assert(vl);
    if(vl->Modern == vl->Exist)
    {
        printf("通讯录已满,请扩大空间\n");
        return;
    }
    else
    {
        printf("请输入姓名->\n");
        fflush(stdin);
        scanf("%s",vl->M_people[vl->Exist].name);
        printf("请输入性别->\n");
        fflush(stdin);
        scanf("%s",vl->M_people[vl->Exist].sex);
        printf("请输入年龄->\n");
        fflush(stdin);
        scanf("%d",&(vl->M_people[vl->Exist].age));
        printf("请输入电话->\n");
        fflush(stdin);
        scanf("%s",vl->M_people[vl->Exist].telephone);
        printf("请输入地址->\n");
        fflush(stdin);
        scanf("%s",vl->M_people[vl->Exist].address);
    }
    vl->Exist++;//储存人数加1
}

static int find_people(struct Volume  *vl,char name[])
{
    int i = 0;
    assert(vl);
    printf("请输入联系人的姓名\n");
    fflush(stdin);
    scanf("%s",name);
    for(i = 0;i < vl->Exist;i++)
    {
        if(strcmp(vl->M_people[i].name,name) == 0)
        {
            return i;
        }

    }
    return -1;
}
void DeletePerson(struct Volume  *vl)
{

    int ret = 0;
    char name[20] = {0};
    assert(vl);
    ret = find_people(vl,name); //查找是否有这个人
    if(ret == -1)
    {
        printf("没有该联系人\n");
    }
    else
    {
        vl->M_people[ret] = vl->M_people[vl->Exist-1];
        vl->Exist--;
        printf("已删除\n");
    }
}
void QueryPerson(struct Volume  *vl)
{

    int ret = 0;
    char name[20] = {0};
    assert(vl);
    ret = find_people(vl,name); //查找是否有这个人
    if(ret == -1)
    {
        printf("没有该联系人\n");
        return ;
    }
    else
    {
        printf("%10s%10s%10s%10s%10s\n", "名字","性别", "年龄", "电话", "地址");
        printf("%9s",vl->M_people[ret].name);
        printf("%9s",vl->M_people[ret].sex);
        printf("%9d",vl->M_people[ret].age);
        printf("%9s",vl->M_people[ret].telephone);
        printf("%9s\n",vl->M_people[ret].address);
    }
}
void menu1()
{
    printf("****************************\n");
    printf("**  请选择你要修改的信息  **\n");
    printf("**        1.姓名          **\n");
    printf("**        2.性别          **\n");
    printf("**        3.年龄          **\n");
    printf("**        4.电话          **\n");
    printf("**        5.地址          **\n");
    printf("****************************\n");


}
void ModifyPerson(struct Volume  *vl)
{

    int input1 = 0;
    int ret = 0;
    char name[20] = {0};
    assert(vl);
    ret = find_people(vl,name); //查找是否有这个人
    if(ret == -1)
    {
        printf("没有该联系人\n");
        return ;
    }
    else
    {
        menu1();
        fflush(stdin);
        scanf("%d",&input1);
        switch(input1)
        {
        case 1:
            printf("请输入姓名->\n");
            fflush(stdin);
            scanf("%s",vl->M_people[ret].name);
            break;
        case 2:
            printf("请输入性别->\n");
            fflush(stdin);
            scanf("%s",vl->M_people[ret].sex);
            break;
        case 3:
            printf("请输入年龄->\n");
            fflush(stdin);
            scanf("%d",&(vl->M_people[ret].age));
            break;
        case 4:
            printf("请输入电话->\n");
            fflush(stdin);
            scanf("%s",vl->M_people[ret].telephone);
            break;
        case 5:
            printf("请输入地址->\n");
            scanf("%s",vl->M_people[ret].address);
        default:
            printf("输入错误,请重新输入\n");
            Sleep(2000);
            break;
        }
    }
}
void LookPerson(struct Volume  *vl)
{
    int i = 0;
    assert(vl);
    if (0 == vl->Exist)
    {
        printf("通讯录为空!\n");
        return;
    }
    printf("%12s%12s%12s%12s%12s\n", "名字","性别", "年龄", "电话", "地址");

    {
        printf("%11s",vl->M_people[i].name);
        printf("%11s",vl->M_people[i].sex);
        printf("%11d",vl->M_people[i].age);
        printf("%11s",vl->M_people[i].telephone);
        printf("%11s\n",vl->M_people[i].address);
    }
}

void EmptyPerson(struct Volume  *vl)
{
    vl->Exist = 0;
    printf("清空完毕\n");

}
void SortPerson(struct Volume  *vl)//以名字形式排序
{
    int i = 0;
    int j = 0;
    struct Message tmp;
    assert(vl);
    for(i = 0;i < vl->Exist - 1;i++)
    {
        for(j = 0;j < vl->Exist - 1 - i;j++)
        {
            if(strcmp(vl->M_people[j].name,vl->M_people[j+1].name) > 0)
            {
                tmp=vl->M_people[j];
                vl->M_people[j]=vl->M_people[j+1];
                vl->M_people[j+1]=tmp;
            }

        }

    }
    printf("排序成功\n");
}
void SavePerson(struct Volume  *vl)
{
    int i = 0;
    FILE *pFile=fopen("myconnect.txt","w");
    if(NULL == pFile)
    {
        perror("Error opening File");
        exit(1);
    }
    for(i = 0;i < vl->Exist;i++)
    {
        fwrite(&(vl->M_people[i]),sizeof(struct Message),1,pFile);
    }
    fclose(pFile);                                                                                                                                                                                                          
}
void ReadPerson(struct Volume  *vl)
{
    int i = 0;
    struct Message tmp={0};
    FILE *pRead =fopen("myconnect.txt","r");

    if(NULL == pRead)
    {
        perror("Error opening File");
        exit(1);
    }
    //fread((&tmp),sizeof(struct Message),1,pRead);
     while(fread((&tmp),sizeof(struct Message),1,pRead)!= 0)      
    { 
        if(vl->Exist == vl->Modern)
        {
            ExpandReduce(vl);
        }
        vl->M_people[i]=tmp;      
        i++;      
        vl->Exist++; 
    //  fread((&tmp),sizeof(struct Message),1,pRead);
    }
    fclose(pRead);
}

调用程序main.c

#define  _CRT_SECURE_NO_WARNINGS 1
//2.实现一个通讯录; 
//通讯录可以用来存储1000个人的信息,每个人的信息包括: 
//姓名、性别、年龄、电话、住址 
//1.将通讯录改成动态的版本。
#include "maillist.h"

void menu()
{

    printf("        ********************************************\n");
    printf("        *****          简易通讯录              *****\n");
    printf("        *****    1.添加联系人   2.删除联系人   *****\n");
    printf("        *****    3.查找联系人   4.修改联系人   *****\n");
    printf("        *****    5.显示所有     6.清空所有     *****\n");
    printf("        *****    7.以名字排序   8.扩大或缩小   *****\n");
    printf("        *****    9.读取         0.退出并保存   *****\n");
    printf("        ********************************************\n");
}
void test()
{
    struct Volume  VL;
    int input = 0;
    InitVolume(&VL);
    do
    {
        menu();
        printf("请输入你选择的功能-> ");
        scanf("%d",&input);
        switch(input)
        {
        case 1:
            AddPerson(&VL);
            break;
        case 2:
            DeletePerson(&VL);
            break;
        case 3:
            QueryPerson(&VL);
            break;
        case 4:
            ModifyPerson(&VL);
            break;
        case 5:
            LookPerson(&VL);
            break;
        case 6:
            EmptyPerson(&VL);
            break;
        case 7:
            SortPerson(&VL);
            break;
        case 8:
            ExpandReduce(&VL);
            break;
        case 9:
            ReadPerson(&VL);
            break;
        case 0:
            {
                SavePerson(&VL);
                Free(&VL);
                return;
            }
            break;
        default:
            printf("输入错误,请重新输入\n");
            Sleep(2000);
            break;
        }

    }while(1);
}
int main()
{
    test();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值