头文件
SeqList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
#define INIT_CAPACITY 4
typedef PersonInfo SLDataType;
typedef struct SeqList {
SLDataType* a;
int size;
int capacity;
}SL;
void SLPrint(SL* ps);
void SLInit(SL* ps);
void SLDestory(SL* ps);
void SLCheckCapacity(SL* ps);
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);
void SLInsert(SL* ps, int pos, SLDataType x);
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
Contact.h
#pragma once
#define NAME_MAX 100
#define GENDER_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100
typedef struct SeqList Contact;
typedef struct PersonInfo {
char name[NAME_MAX];
char gender[GENDER_MAX];
char tel[TEL_MAX];
char addr[ADDR_MAX];
}PersonInfo;
void ContactInit(Contact* con);
void ContactDestory(Contact* con);
void ContactPushBack(Contact* con);
void ContactPrint(Contact* con);
void ContactDele(Contact* con);
void ContactModify(Contact* con);
void ContactFind(Contact* con);
void LoadContact(Contact* con);
void SaveContact(Contact* con);
实现文件
SeqList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void SLInit(SL* ps)
{
ps->size = 0;
ps->capacity = INIT_CAPACITY;
SLDataType* arr = (SLDataType*)malloc(sizeof(SLDataType) * INIT_CAPACITY);
if (arr == NULL) {
perror("malloc fail!\n");
exit(1);
}
else {
ps->a = arr;
arr = NULL;
}
}
void SLDestory(SL* ps)
{
assert(ps);
if(ps->a)
free(ps->a);
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
ps->capacity *= 2;
SLDataType* arr = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * (ps->capacity));
if (arr == NULL)
{
perror("realloc fail!\n");
exit(1);
}
ps->a = arr;
}
}
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
for (int i = ps->size - 1; i >= 0; i--)
{
ps->a[i + 1] = ps->a[i];
}
ps->a[0] = x;
ps->size++;
}
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size > 0);
ps->size--;
}
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size > 0);
for (int i = 0; i < ps->size - 1; i++) {
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos <= ps->size && ps >= 0);
SLCheckCapacity(ps);
for (int i = ps->size - 1; i >= pos; i--)
{
ps->a[i + 1] = ps->a[i];
}
ps->a[pos] = x;
ps->size++;
}
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos <= ps->size && ps >= 0);
for (int i = pos; i < ps->size - 1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
Contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
#include"Contact.h"
#include<string.h>
void ContactInit(Contact* con)
{
SLInit(con);
}
void ContactDestory(Contact* con)
{
SLDestory(con);
}
void ContactPushBack(Contact* con)
{
PersonInfo a;
printf("情输入联系人姓名:\n");
scanf("%s", &(a.name));
printf("情输入联系人性别:\n");
scanf("%s", &(a.gender));
printf("情输入联系人电话号码:\n");
scanf("%s", &(a.tel));
printf("情输入联系人地址:\n");
scanf("%s", &(a.addr));
SLPushBack(con, a);
}
void ContactPrint(Contact* con)
{
assert(con);
if (con->size == 0) {
printf("通讯录为空。\n");
return;
}
printf("姓名 性别 电话 地址\n");
for (int i = 0; i < con->size; i++)
{
printf("%s ", con->a[i].name);
printf("%s ", con->a[i].gender);
printf("%s ", con->a[i].tel);
printf("%s ", con->a[i].addr);
printf("\n");
}
}
void ContactDele(Contact* con)
{
char name[NAME_MAX] = { 0 };
printf("请输入要删除的联系人姓名:");
scanf("%s", name);
int dele = 0;
for (int i = 0; i < con->size; i++)
{
if (strcmp(con->a[i].name, name) == 0)
{
SLErase(con, i);
dele = 1;
printf("删除成功!\n");
break;
}
}
if(dele == 0)
printf("联系人信息不存在!\n");
}
void ContactModify(Contact* con)
{
assert(con);
char name[NAME_MAX];
printf("请输入要修改的联系人姓名:");
scanf("%s", name);
for (int i = 0; i < con->size; i++)
{
if (strcmp(con->a[i].name, name) == 0)
{
printf("情输入新的联系人姓名:\n");
scanf("%s", &(con->a[i].name));
printf("情输入联系人性别:\n");
scanf("%s", &(con->a[i].gender));
printf("情输入联系人电话号码:\n");
scanf("%s", &(con->a[i].tel));
printf("情输入联系人地址:\n");
scanf("%s", &(con->a[i].addr));
printf("修改成功!\n");
return;
}
}
printf("无此联系人信息!\n");
}
void ContactFind(Contact* con)
{
assert(con);
char name[NAME_MAX];
printf("请输入要查找的联系人姓名:");
scanf("%s", name);
for (int i = 0; i < con->size; i++)
{
if (strcmp(con->a[i].name, name) == 0)
{
printf("姓名 性别 电话 地址\n");
printf("%s ", con->a[i].name);
printf("%s ", con->a[i].gender);
printf("%s ", con->a[i].tel);
printf("%s ", con->a[i].addr);
printf("\n");
return;
}
}
printf("无此联系人信息!\n");
return;
}
void LoadContact(Contact* con)
{
FILE* pf = fopen("contact.txt", "rb");
if (pf == NULL)
{
perror("fopen fail!\n");
exit(1);
}
else {
PersonInfo tmp;
while (fread(&tmp, sizeof(PersonInfo), 1, pf))
{
SLPushBack(con, tmp);
}
}
fclose(pf);
pf = NULL;
printf("读取成功!\n");
}
void SaveContact(Contact* con)
{
FILE* pf = fopen("contact.txt", "wb");
if (pf == NULL)
{
perror("fopen fail!\n");
exit(1);
}
else {
for (int i = 0; i < con->size; i++)
{
fwrite(&(con->a[i]), sizeof(PersonInfo), 1, pf);
}
}
fclose(pf);
pf = NULL;
printf("保存成功!\n");
return;
}
测试文件
text.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
#include"Contact.h"
void menu()
{
printf("*************************************************\n");
printf("*************1.增加联系人 2.删除联系人**********\n");
printf("*************3.修改联系人 4.查找联系人**********\n");
printf("*************5.打印通讯录 0.退出****************\n");
printf("*************************************************\n");
printf("请选择要进行的操作:\n");
}
void Test2(Contact* con)
{
int input = 0;
do {
menu();
scanf("%d", &input);
switch (input)
{
case 1:
ContactPushBack(con);
break;
case 2:
ContactDele(con);
break;
case 3:
ContactModify(con);
break;
case 4:
ContactFind(con);
break;
case 5:
ContactPrint(con);
default:
break;
}
} while (input);
printf("正在退出...\n");
}
int main()
{
Contact a;
Contact* con = &a;
ContactInit(con);
LoadContact(con);
Test2(con);
SaveContact(con);
ContactDestory(con);
return 0;
}