first commit

master
1927461836@qq.com 6 years ago
commit 77fec02ccc

@ -0,0 +1,218 @@
#define _CRT_SECURE_NO_WARNINGS 1
#include "Contact.h"
void* check_capacity(pContact pc)//增容
{
assert(pc);
if (pc->sz == pc->capacity)//判断是否增容
{
PeoInfo *tmp = realloc(pc->data, (pc->capacity + 2)*sizeof(PeoInfo)); //data改变内存 新的大小
if (tmp != NULL) //如果需要改变内存则执行
{
pc->data = tmp;
pc->capacity += 2;
return pc->data;
}
else
{
return NULL;
}
}
}
void LoadContact(pContact pc) //读取--加载内存
{
PeoInfo tmp = { 0 };
FILE* pf = fopen("contact.dat", "rb");
int i = 0;
if (pf == NULL)
{
perror("open file for read");
exit(EXIT_FAILURE);
}
while (fread(&tmp, sizeof(PeoInfo), 1, pf)) //从一个文件流中读数据
{
if (check_capacity(pc) != NULL)
{
pc->data[pc->sz] = tmp;
pc->sz++;
}
}
fclose(pf);
pf = NULL;
}
void InitContact(pContact pc) //初始化
{
assert(pc);
pc->sz = 0;
pc->data = (PeoInfo*)malloc(3 * sizeof(PeoInfo));//分配空间
pc->capacity = DEFAULT_SZ;
//加载文件信息
LoadContact(pc);
}
void DestroyContact(pContact pc)//释放动态内存开辟的空间
{
assert(pc);
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->sz = 0;
}
void AddContact(pContact pc) //添加信息
{
assert(pc);
if (check_capacity(pc) == NULL)
{
return;
}
printf("请输入名字:>");
scanf("%s", pc->data[pc->sz].name);
printf("请输入年龄:>");
scanf("%d", &(pc->data[pc->sz].age));
printf("请输入性别:>");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入电话:>");
scanf("%s", pc->data[pc->sz].tele);
printf("请输入地址:>");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
}
void ShowContact(pContact pc) //显示联系人信息
{
assert(pc);
int i = 0;
printf("%10s\t%4s\t%5s\t%12s\t%20s\n", "name","age","sex","tele","addr");
for (i = 0; i < pc->sz; i++)
{
printf("%10s\t%4d\t%5s\t%12s\t%20s\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
int FindEntry(pContact pc, char name[]) //建立函数搜素联系人 判断是否存在
{
int i = 0;
assert(pc);
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0) //比较字符是否相同确定联系人是否存在
{
return i;
}
}
return -1;//代表没找到联系人
}
void DelContact(pContact pc) //删除联系人
{
assert(pc);
char name[NAME_MAX] = { 0 };
int i = 0;
int j = 0;
int pos = 0;
if (pc->sz == 0) //判断是否为空通讯录
{
printf("通讯录已空,无法删除\n");
return;
}
printf("请输入要删除人的名字>:"); //1.查找
scanf("%s", name);
pos=FindEntry(pc, name); //判断要找的人是否存在
if (pos!= -1) //2.删除
{
for (j = pos; j < pc->sz - 1; j++) //找到位置后 覆盖前一个人的数据即移位
{
pc->data[j] = pc->data[j + 1];
}
pc->sz--;
printf("删除成功\n");
}
else
{
printf("要删除的人不存在\n");
}
}
void SearchContact(pContact pc) //查找联系人
{
assert(pc);
char name[NAME_MAX] = { 0 };
int pos = 0;
printf("请输入要查找的名字>:");
scanf("%s", name);
pos=FindEntry(pc, name); //判断是否存在
if (pos == -1)
{
printf("要查找的人不存在\n");
}
else
{
printf("%10s\t%4s\t%5s\t%12s\t%20s\n", "name", "age", "sex", "tele", "addr");
printf("%10s\t%4d\t%5s\t%12s\t%20s\n",
pc->data[pos].name,
pc->data[pos].age,
pc->data[pos].sex,
pc->data[pos].tele,
pc->data[pos].addr);
}
}
void ModifyContact(pContact pc) //修改信息
{
assert(pc);
char name[NAME_MAX] = { 0 };
int pos = 0;
printf("请输入要修改人的名字>:");
scanf("%s", name);
pos = FindEntry(pc, name);
if (pos == -1)
{
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].tele);
printf("请输入地址:>");
scanf("%s", pc->data[pos].addr);
}
}
void EmptyContact(pContact pc) //清空联系人
{
pc->sz = 0;
printf("通讯录已清空\n");
}
void SaveContact(pContact pc) //存储信息 //写文件
{
FILE* pf = fopen("contact.dat", "wb");
int i = 0;
if (pf == NULL)
{
perror("open file for write");
exit(EXIT_FAILURE);
}
for (i = 0; i < pc->sz; i++) //写文件
{
fwrite(pc->data + i, sizeof(PeoInfo), 1, pf);
}
fclose(pf);
pf = NULL;
}

@ -0,0 +1,44 @@
#ifndef __CONTACT_H__
#define __CONTACT_H__
#include <stdio.h>
#include <stdlib.h> //系统函数 实现某些函数调用等
#include <string.h>
#include <assert.h> //诊断程序是否有误
#define DEFAULT_SZ 3
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 12
#define ADDR_MAX 30
#define MAX 1000
typedef struct PeoInfo //结构体
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX ];
char tele[TELE_MAX];
char addr[ADDR_MAX];
}PeoInfo;
typedef struct Contact
{
PeoInfo* data;//存放数据
int sz;//当前已有信息个数
int capacity;//当前最大的容量
}Contact, *pContact;
void InitContact(pContact pc); //初始化
void DestroyContact(pContact pc);//释放动态内存开辟的空间
void AddContact(pContact pc); //添加联系人
void ShowContact(pContact pc);//显示联系人
void DelContact(pContact pc);//删除联系人
void SearchContact(pContact pc);//查找联系人
void ModifyContact(pContact pc);//修改联系热
void EmptyContact(pContact pc);//清空联系人
void SaveContact(pContact pc);//保存信息到文件
void LoadContact(pContact pc);//加载文件
#endif

Binary file not shown.

@ -0,0 +1,31 @@
# Project: ÏîÄ¿1
# Makefile created by Dev-C++ 5.11
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = Contact.o test.o
LINKOBJ = Contact.o test.o
LIBS = -L"C:/Program Files/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib32" -static-libgcc -m32
INCS = -I"C:/Program Files/Dev-Cpp/MinGW64/include" -I"C:/Program Files/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS = -I"C:/Program Files/Dev-Cpp/MinGW64/include" -I"C:/Program Files/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Program Files/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
BIN = ÏîÄ¿1.exe
CXXFLAGS = $(CXXINCS) -m32
CFLAGS = $(INCS) -m32
RM = rm.exe -f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CC) $(LINKOBJ) -o $(BIN) $(LIBS)
Contact.o: Contact.c
$(CC) -c Contact.c -o Contact.o $(CFLAGS)
test.o: test.c
$(CC) -c test.c -o test.o $(CFLAGS)

Binary file not shown.

@ -0,0 +1,64 @@
#define _CRT_SECURE_NO_WARNINGS 1
#include "Contact.h"
void menu()
{
printf(" 菜单\n");
printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃ 1.添加联系人信息 ┃\n");
printf("┃ 2.删除指定联系人信息 ┃\n");
printf("┃ 3.查找指定联系人信息 ┃\n");
printf("┃ 4.修改指定联系人信息 ┃\n");
printf("┃ 5.显示所有联系人信息 ┃\n");
printf("┃ 6.清空所有联系人 ┃\n");
printf("┃ 0.退出 ┃\n");
printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf(" 选择代码012345, 6 ,7 \n ");
}
void test()
{
Contact con;//通讯录
InitContact(&con);//初始化通讯录
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1://增加联系人
AddContact(&con);
break;
case 2://删除联系人
DelContact(&con);
break;
case 3://查找联系人
SearchContact(&con);
break;
case 4://修改联系人
ModifyContact(&con);
break;
case 5://显示联系人
ShowContact(&con);
break;
case 0:
SaveContact(&con); //保存
DestroyContact(&con);
printf("退出通讯录\n");
break;
case 6://清空联系人
EmptyContact(&con);
break;
default:
printf("选择错误\n");
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}

BIN
test.o

Binary file not shown.

@ -0,0 +1,82 @@
[Project]
FileName=ÏîÄ¿1.dev
Name=ÏîÄ¿1
Type=1
Ver=2
ObjFiles=
Includes=
Libs=
PrivateResource=
ResourceIncludes=
MakeIncludes=
Compiler=
CppCompiler=
Linker=
IsCpp=0
Icon=
ExeOutput=
ObjectOutput=
LogOutput=
LogOutputEnabled=0
OverrideOutput=0
OverrideOutputName=
HostApplication=
UseCustomMakefile=0
CustomMakefile=
CommandLine=
Folders=
IncludeVersionInfo=0
SupportXPThemes=0
CompilerSet=3
CompilerSettings=0000000100000000000000000
UnitCount=3
[VersionInfo]
Major=1
Minor=0
Release=0
Build=0
LanguageID=1033
CharsetID=1252
CompanyName=
FileVersion=
FileDescription=Developed using the Dev-C++ IDE
InternalName=
LegalCopyright=
LegalTrademarks=
OriginalFilename=
ProductName=
ProductVersion=
AutoIncBuildNr=0
SyncProduct=1
[Unit3]
FileName=Contact.h
CompileCpp=0
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
[Unit1]
FileName=Contact.c
CompileCpp=0
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
[Unit2]
FileName=test.c
CompileCpp=0
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

Binary file not shown.

@ -0,0 +1,18 @@
[Editors]
Order=0,2,1
Focused=-1
[Editor_0]
CursorCol=4
CursorRow=52
TopLine=4
LeftChar=1
[Editor_1]
CursorCol=28
CursorRow=22
TopLine=14
LeftChar=1
[Editor_2]
CursorCol=48
CursorRow=41
TopLine=9
LeftChar=1
Loading…
Cancel
Save