目录
test.c:
?contact.h:
contact.c:
?
我们先创建三个不同的文件,分别是主体函数test.c,函数实现contact.c,和头文件contact.h
test.c:
制作一个简单的目录即可,记得包含头文件
#include"contact.h"//头文件
void menu()//目录
{
printf("**********************************\n");
printf("****** 0.退出 1.增加 2.删除 ******\n");
printf("****** 3.查找 4.修改 5.排序 ******\n");
printf("****** 6.显示通讯录 ******\n");
printf("**********************************\n");
}
int main()
{
int input = 0;
contact con;
initcontact(&con);
do
{
menu();
printf("请输入\n");
scanf("%d", &input);
switch (input)
{
case 1:
addcontact(&con);
break;
case 2:
deletecontact(&con);
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
showcontact(&con);
break;
case 0:
destory(&con);
printf("退出通讯录\n");
break;
default:
printf("请输入0-5\n");
}
} while (input);
}
将所有功能的头文件和函数都填入
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<errno.h>
#include<stdlib.h>
#define NAME_MAX 20
#define SEX_MAX 5
#define ADDR_MAX 30
#define TELE_MAX 12
#define DATA_MAX 1000
#define default_sz 3
typedef struct peoinfo//通讯录包含的内容
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char addr[ADDR_MAX];
char tele[TELE_MAX];
}peoinfo;
typedef struct contact//通讯录的实现
{
peoinfo *data;
int sz;
int capacity;
}contact;
void initcontact(contact* a);//初始化通讯录
void addcontact(contact* a);//增加通讯录
void showcontact(contact* a);//显示通讯录
void deletecontact(contact* a);//删除通讯录
void destory(contact* a);//销毁通讯录
完善每个函数
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void destory(contact* a)
{
assert(a);
printf("销毁成功");
free(a->data);
a->data = NULL;
a->capacity = 0;
a->sz = 0;
}
void initcontact(contact* a)
{
assert(a);
a->sz = 0;
peoinfo* tmp = (peoinfo*)malloc(3 * sizeof(peoinfo));
if (tmp != NULL)
{
a->data = tmp;
}
else
{
printf("initcontact()::%s\n", strerror(errno));
return;
}
a->capacity = default_sz;
}
void check_capacity(contact*a)
{
if (a->sz == a->capacity)
{
peoinfo* tmp = (peoinfo*)realloc(a->data, (a->capacity + 2) * sizeof(peoinfo));
if (tmp != NULL)
{
a->data = tmp;
a->capacity += 2;
printf("增容成功\n");
}
else
{
printf("%s\n", strerror(errno));
}
}
}
void addcontact(contact* a)
{
assert(a);
check_capacity(a);
{
printf("请输入联系人的名字\n");
scanf("%s", a->data[a->sz].name);
printf("请输入联系人的年龄\n");
scanf("%d", &(a->data[a->sz].age));
printf("请输入联系人的性别\n");
scanf("%s", a->data[a->sz].sex);
printf("请输入联系人的地址\n");
scanf("%s", a->data[a->sz].addr);
printf("请输入联系人的电话\n");
scanf("%s", a->data[a->sz].tele);
a->sz++;
printf("增加完毕\n");
}
}
void showcontact(const contact* a)
{
assert(a);
printf("%10s\t%5s\t%5s\t%20s\t%20s\t\n", "名字", "年龄", "性别", "地址", "电话");
for (int i = 0; i < a->sz; i++)
{
printf("%10s\t%3d\t%5s\t%20s\t%20s\t\n", a->data[i].name, a->data[i].age, a->data[i].sex, a->data[i].addr, a->data[i].tele);
}
}
int findbyname(const contact* a, char name[])
{
for (int i = 0; i < a->sz; i++)
{
if (strcmp(a->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void deletecontact(contact* a)
{
char name[NAME_MAX] = { 0 };
if (a->sz == 0);
{
printf("通讯录为空,无法删除\n");
return;
}
printf("请输入删除人的名字\n");
scanf("%s", name);
int ch = findbyname(a, name);
if (ch == -1)
{
printf("不存在这个人\n");
}
else
{
for (int j = ch; j < a->sz-1; j++)
{
a->data[j] = a->data[j + 1];
}
a->sz--;
printf("删除成功");
}
}
|