contact.h头文件
#pragma once
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#define DEFAULT_SZ 3
#define NAME_MAX 20
#define SEX_MAX 2
#define TEL_MAX 12
#define ADDR_MAX 50
#define MAX 1000
typedef struct PersonInfo {
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char tel[TEL_MAX];
char addr[ADDR_MAX];
}Per;
typedef struct Contatc {
Per *data;
int size;
int capacity;
}Con;
void CheckCapacity(Con* pc);
void InitContact(Con *pc);
void DestoryContact(Con *pc);
void AddContact(Con *pc);
void SubContact(Con *pc);
void ShowContact(Con *pc);
void DelContact(Con* pc);
void SearchContact(const Con *pc);
void ModifyContact(Con* pc);
void SaveContact(Con* pc);
void LoadContact(Con* pc);
contact.c 源文件
#include "contact.h"
void CheckCapacity(Con *pc) {
if (pc->size == pc->capacity)
{
Per* ptr = (Per*)realloc(pc->data, (pc->capacity + 2) * sizeof(Per));
if (ptr != NULL)
{
pc->data = ptr;
pc->capacity += 2;
printf("增容成功\n");
}
else
{
perror("通讯录增容失败");
exit(-1);
}
}
}
void LoadContact(Con* pc)
{
FILE* pf = fopen("E:\contact.txt", "rb");
if (NULL == pf)
{
perror("LoadContact::fopen");
return;
}
Per tmp = { 0 };
while (fread(&tmp, sizeof(Per), 1, pf))
{
CheckCapacity(pc);
pc->data[pc->size] = tmp;
pc->size++;
}
fclose(pf);
pf = NULL;
}
void InitContact(Con* pc)
{
pc->size = 0;
pc->data = (Per*)malloc(DEFAULT_SZ * sizeof(Per));
pc->capacity = DEFAULT_SZ;
LoadContact(pc);
}
void AddContact(Con* pc)
{
CheckCapacity(pc);
printf("请输入名字:>");
scanf("%s", pc->data[pc->size].name);
printf("请输入年龄:>");
scanf("%d", &(pc->data[pc->size].age));
printf("请输入性别:>");
scanf("%s", pc->data[pc->size].sex);
printf("请输入电话:>");
scanf("%s", pc->data[pc->size].tel);
printf("请输入地址:>");
scanf("%s", pc->data[pc->size].addr);
pc->size++;
printf("增加成功\n");
}
void DestroyContact(Con* pc)
{
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->size = 0;
}
void ShowContact(Con* pc)
{
int i = 0;
printf("%15s\t%5s\t%8s\t%15s\t%30s\n\n",
"name", "age", "sex", "tele", "addr");
for (i = 0; i < pc->size; i++)
{
printf("%15s\t%5d\t%8s\t%15s\t%30s\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tel,
pc->data[i].addr);
}
}
int FindContactByName(const Con* pc, const char*name)
{
int i = 0;
for (i = 0; i < pc->size; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void DelContact(Con* pc)
{
if (pc->size == 0)
{
printf("通讯录为空,无法删除\n");
return;
}
char name[NAME_MAX] = { 0 };
printf("请输入要删除人的名字:>");
scanf("%s", name);
int pos = FindContactByName(pc, name);
if (pos == -1)
{
printf("指定的联系人不存在\n");
}
else
{
int j = 0;
for (j = pos; j < pc->size - 1; j++)
{
pc->data[j] = pc->data[j + 1];
}
pc->size--;
printf("删除成功\n");
}
}
void SearchContact(const Con* pc)
{
char name[NAME_MAX] = { 0 };
printf("输入要查找人的名字:>");
scanf("%s", name);
int pos = FindContactByName(pc, name);
if (-1 == pos)
{
printf("查无此人\n");
}
else
{
printf("%15s\t%5s\t%8s\t%15s\t%30s\n\n",
"name", "age", "sex", "tele", "addr");
printf("%15s\t%5d\t%8s\t%15s\t%30s\n",
pc->data[pos].name,
pc->data[pos].age,
pc->data[pos].sex,
pc->data[pos].tel,
pc->data[pos].addr);
}
}
void ModifyContact(Con* pc)
{
char name[NAME_MAX] = { 0 };
printf("输入要修改人的名字:>");
scanf("%s", name);
int pos = FindContactByName(pc, name);
if (-1 == pos)
{
printf("要修改的人不存在\n");
}
else
{
printf("请输入新的名字:>");
scanf("%s", pc->data[pos].name);
printf("请输入新的年龄:>");
scanf("%d", &(pc->data[pos].age));
printf("请输入新的性别:>");
scanf("%s", pc->data[pos].sex);
printf("请输入新的电话:>");
scanf("%s", pc->data[pos].tel);
printf("请输入新的地址:>");
scanf("%s", pc->data[pos].addr);
}
}
void SaveContact( Con* pc)
{
FILE* pf = fopen("contact.txt", "wb");
if (NULL == pf)
{
perror("SaveContact::fopen");
return;
}
int i = 0;
for (i = 0; i < pc->size; i++)
{
fwrite(pc->data + i, sizeof(Per), 1, pf);
}
fclose(pf);
pf = NULL;
}
main.c源文件
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:4996)
#include "contact.h"
void menu()
{
printf("******************************\n");
printf("**** 1. add 2. del *****\n");
printf("**** 3. search 4. modify****\n");
printf("**** 5. show 6. sort ***\n");
printf("**** 0. exit **\n");
printf("******************************\n");
}
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
int main()
{
int input = 0;
Con con;
InitContact(&con);
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case EXIT:
SaveContact(&con);
DestroyContact(&con);
printf("退出通讯录\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
|