通讯录文件版,通过动态内存分配完成,包括信息的增、删、查、改
头文件:
#ifndef __ADDRESSLIST_H__
#define __ADDRESSLIST_H__
#define _CRT_SECURE_NO_WARNINGS
#define MAX 2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct man
{
char name[20];
int age;
char sex[5];
char tele[20];
char addr[50];
}Man,*pMan;
typedef struct adresslist
{
pMan pcon;
int sz;//当前成员数
int capaity;//容量
}List,*pList;
void Init(pList list);//初始化通讯录
void Add(pList list);//添加信息
void Dele(pList list);//删除信息
void Serc(pList list);//查找
void Show(const pList list);//显示所有人
void Chage(pList list);//修改信息
void SaveCon(pList list);//保存信息到文件
void LodCon(pList list);//加载文件里的信息
void Destroy(pList list);//退出时销毁通讯录
#endif //__ADDRESSLIST_H__
函数文件:
#include"addresslist.h"
static void dy_add(pList list);//动态增加
static int Find(pList list,char name[]);//按姓名查找,找到返回1否则返回-1
void Init(pList list)
{
list->sz = 0;
list->pcon = (pMan)malloc(MAX*sizeof(Man));
list->capaity = MAX;
}
static void dy_add(pList list)//动态增加
{
if(list->sz == list->capaity)
{
pMan temp = (pMan)realloc(list->pcon,(list->capaity+2)*sizeof(Man));
list->pcon = temp;
list->capaity += 2;
}
}
void Add(pList list)
{
dy_add(list);
printf("输入姓名:");
scanf("%s",list->pcon[list->sz].name);
printf("输入年龄:");
scanf("%d",&(list->pcon[list->sz].age));
printf("输入性别:");
scanf("%s",list->pcon[list->sz].sex);
printf("输入电话:");
scanf("%s",list->pcon[list->sz].tele);
printf("输入地址:");
scanf("%s",list->pcon[list->sz].addr);
list->sz++;
printf("添加成功\n");
}
void Destroy(pList list)
{
free(list->pcon);
list->capaity = 0;
list->sz = 0;
list->pcon = NULL;
}
static int Find(pList list,char name[])
{
int i;
for(i=0; i<list->sz; i++)
{
if(strcmp(list->pcon[i].name,name)==0)
return i;
}
return -1;
}
void Serc(pList list)
{
char name[20];
int pos;
int i = 0;
printf("输入要查找的姓名:");
scanf("%s",name);
pos = Find(list,name);
if(pos == -1)
printf("没有要找的人\n");
else
{
printf("姓名 年龄 性别 电话 地址\n");
printf("%3s%5d%6s%7s%7.5s\n",list->pcon[pos].name,list->pcon[pos].age,
list->pcon[pos].sex,list->pcon[pos].tele,list->pcon[pos].addr);
}
}
void Dele(pList list)
{
char name[20];
int pos;
int i = 0;
printf("输入要删的姓名:");
scanf("%s",name);
pos = Find(list,name);
if(pos == -1)
printf("没有要找的人\n");
else
{
for(i=pos; i<list->sz-1; i++)
{
list->pcon[i] = list->pcon[1+i];
}
list->sz--;
printf("删除成功\n");
}
}
void Show(const pList list)
{
int i = 0;
printf("姓名 年龄 性别 电话 地址\n");
for(i=0; i<list->sz; i++)
{
printf("%3s%5d%6s%7s%7.5s\n",list->pcon[i].name,list->pcon[i].age,
list->pcon[i].sex,list->pcon[i].tele,list->pcon[i].addr);
}
}
void Chage(pList list)
{
char name[20];
int pos;
int i = 0;
printf("输入要修改的姓名:");
scanf("%s",name);
pos = Find(list,name);
if(pos == -1)
printf("没有要找的人\n");
else
{
printf("输入姓名:");
scanf("%s",list->pcon[i].name);
printf("输入年龄:");
scanf("%d",&(list->pcon[i].age));
printf("输入性别:");
scanf("%s",list->pcon[i].sex);
printf("输入电话:");
scanf("%s",list->pcon[i].tele);
printf("输入地址:");
scanf("%s",list->pcon[i].addr);
printf("修改成功\n");
}
}
void SaveCon(pList list)
{
int i;
FILE* pfWrite = fopen("hehe.txt","w");
if(pfWrite == NULL)
{
perror("open file for write");
exit(1);
}
for(i=0; i<list->sz; i++)
fwrite(list->pcon+i,sizeof(Man),1,pfWrite);
fclose(pfWrite);
}
void LodCon(pList list)
{
Man tmp;
FILE* pfRead = fopen("hehe.txt","r");
if(pfRead == NULL)
{
perror("open file for read");
exit(1);
}
while(fread(&tmp,sizeof(Man),1,pfRead))
{
dy_add(list);
list->pcon[list->sz++] = tmp;
}
fclose(pfRead);
}
主体逻辑:
#include"addresslist.h"
void menu()
{
printf("***********************\n");
printf("****1.添加 2.删除*****\n");
printf("****3.查找 4.修改*****\n");
printf("****5.显示 0.退出*****\n");
printf("***********************\n");
}
void test()
{
int input = 0;
List a_list;
pList lt = &a_list;
Init(lt);
LodCon(lt);
do
{
menu();
printf("请选择:");
scanf("%d",&input);
switch(input)
{
case 0:
SaveCon(lt);
Destroy(lt);
exit(1);
case 1:
Add(lt);
break;
case 2:
Dele(lt);
break;
case 3:
Serc(lt);
break;
case 4:
Chage(lt);
break;
case 5:
Show(lt);
break;
default:
printf("输入错误!\n");
break;
}
}while(input);
}
int main()
{
test();
return 0;
}