contact—通讯录

Contact_通讯录

contact—通讯录_第1张图片

这里是代码

contact.h
#pragma once
#include
#include
#include

//数字
#define CON_NAME 10
#define CON_SEX 6
#define CON_TELE 15
#define CON_ADDR 10
#define capa 3

//定义通讯录方法
enum num
{
	EXIT,
	ADD,
	DEL,
	MODIFY,
	CHECK,
	SHOW,
	CLEAR,
	SORT
};

//定义通讯录信息
struct Contact
{
	char name[CON_NAME];
	int age;
	char sex[CON_SEX];
	char tele[CON_TELE];
	char addr[CON_ADDR];
};

//定义通讯录大纲
struct Mycontact
{
	struct Contact *data;
	int size;
	int capacity;//开辟空间
};

//声明函数
//初始化通讯录
void Initcontact(struct Mycontact *ps);
//增加通讯录联系人
void ADDcontact(struct Mycontact *ps);
//删除通讯录联系人
void DELcontact(struct Mycontact *ps);
//显示通讯录联系人
void SHOWcontact(struct Mycontact *ps);
//修改通讯录联系人
void MODIFYcontact(struct Mycontact *ps);
//查找通讯录联系人
void CHECKcontact(struct Mycontact *ps);
//排序通讯录联系人
void SORTcontact(struct Mycontact *ps);
//清空通讯录联系人
void Clearcontact(struct Mycontact *ps);
//释放通讯录联系人
void Freecontact(struct Mycontact *ps);
contact.c
#define _CRT_SECURE_NO_WARNINGS 

#include"contact.h"
//初始化通讯录
void Initcontact(struct Mycontact* ps)
{
	ps->data = (struct Contact*)malloc(capa* sizeof(struct Contact));
	if (ps->data == NULL)
	{
		printf("初始化失败\n");//报错信息
		return;
	}
	ps->capacity = capa;
	memset(ps->data, 0, sizeof(ps->data));
	ps->size = 0;
}
//增加通讯录信息

void ADDcontact(struct Mycontact *ps)
{
	if (ps->size == ps->capacity)
	{
		struct Contact* ptr=(struct Contact*)realloc(ps->data,(ps->capacity+capa) * sizeof(struct Contact));
		if (ptr != NULL)
		{
			printf("扩容成功\n");
			ps->data = ptr;
			ps->capacity += capa;
			ptr = NULL;
		}
		else
		{
			printf("内存满了\n");
			return;
		}
	}	
	printf("请输入姓名:");
	scanf("%s", ps->data[ps->size].name);
	printf("请输入年龄:");
	scanf("%d", &ps->data[ps->size].age);
	printf("请输入性别:");
	scanf("%s", ps->data[ps->size].sex);
	printf("请输入号码:");
	scanf("%s", ps->data[ps->size].tele);
	printf("请输入地址:");
	scanf("%s", ps->data[ps->size].addr);
	ps->size++;
	printf("增加成功\n");
}

//查找联系人
int Findcontact(struct Mycontact *ps)
{
	if (ps->size == 0)
		return -1;
	char name[CON_NAME] = { 0 };
	printf("请输入联系人的姓名:");
	scanf("%s", name);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (strcmp(name, ps->data[i].name) == 0)
		{
			return i;
		}
	}
	return -1;
}
//删除通讯录联系人
void DELcontact(struct Mycontact *ps)
{	
	int ret = Findcontact(ps);
	if (ret != -1)
	{
		ps->data[ret] = ps->data[ret + 1];
		ps->size--;
		printf("删除成功\n");
	}
	else
	{
		printf("删除失败,没有找到该联系人\n");
	}
}

//显示通讯录联系人
void SHOWcontact(struct Mycontact *ps)
{
	printf("%-10s\t%-6s\t%-4s\t%-15s\t%-10s\t\n", "姓名", "性别", "年龄", "电话", "地址");
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%-10s\t%-6s\t%-4d\t%-15s\t%-10s\t\n",
			ps->data[i].name,
			ps->data[i].sex,
			ps->data[i].age,
			ps->data[i].tele,
			ps->data[i].addr);
	}
}

//修改通讯录联系人
void MODIFYcontact(struct Mycontact *ps)
{
	int ret = Findcontact(ps);
	if (-1==ret)
	{
		printf("修改失败,未找到该联系人\n");
	}
	else
	{
		printf("请输入姓名:");
		scanf("%s", ps->data[ret].name);
		printf("请输入年龄:");
		scanf("%d", &ps->data[ret].age);
		printf("请输入性别:");
		scanf("%s", ps->data[ret].sex);
		printf("请输入号码:");
		scanf("%s", ps->data[ret].tele);
		printf("请输入地址:");
		scanf("%s", ps->data[ret].addr);
		printf("修改成功\n");
	}
}

//查找通讯录联系人
void CHECKcontact(struct Mycontact *ps)
{
	int ret=Findcontact(ps);
	if (-1 == ret)
	{
		printf("没有该联系人\n");
	}
	else
	{
		printf("%-10s\t%-6s\t%-4s\t%-15s\t%-10s\t\n", "姓名", "性别", "年龄", "电话", "地址");
		printf("%-10s\t%-6s\t%-4d\t%-15s\t%-10s\t\n",
			ps->data[ret].name,
			ps->data[ret].sex,
			ps->data[ret].age,
			ps->data[ret].tele,
			ps->data[ret].addr);
	}
}

int cmp_struct(const void*e1,const void* e2)
{
	return strcmp(((struct Contact*)e1)->name, ((struct Contact*)e2)->name);
}
//排序通讯录联系人
void SORTcontact(struct Mycontact *ps)
{
	if (ps->size == 0)
	{
		printf("通讯录为空\n");
	}
	else
	{
		int i = 0;
		for (i = 0; i < ps->size; i++)
		{
			int j = 0;
			for (j = 0; j < ps->size - i - 1; i++)
			{
				qsort(ps->data[j].name, ps->size, sizeof(ps->data[0]), cmp_struct);
			}
		}
		printf("排序成功\n");
	}
}
//清空通讯录联系人
void Clearcontact(struct Mycontact *ps)
{
	memset(ps->data, 0, sizeof(ps->data));
	ps->size = 0;
}

//释放通讯录联系人内存
void Freecontact(struct Mycontact *ps)
{
	free(ps->data);
	ps->data = NULL;
	ps->size = 0;
}
test.c测试文档
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"

void mnue()
{
	printf("******************************\n");
	printf("****  1.ADD       2.DEL   ****\n");
	printf("****  3.MODIFY    4.CHECK ****\n");
	printf("****  5.SHOW      6.CLEAR ****\n");
	printf("****  7.SORT      0.EXIT  ****\n");
	printf("******************************\n");
}

int main()
{
	//创建通讯录
	struct Mycontact con;
	//初始化通讯录
	Initcontact(&con);

	int input = 0;
	do
	{
		mnue();
		printf("请输入你的操作:");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			ADDcontact(&con);
			break;
		case DEL:
			DELcontact(&con);
			break;
		case MODIFY:
			MODIFYcontact(&con);
			break;
		case CHECK:
			CHECKcontact(&con);
			break;
		case SHOW:
			SHOWcontact(&con);
			break;
		case CLEAR:
			//Initcontact(&con);//清空相当于初始化
			Clearcontact(&con);
			printf("清空成功\n");
			break;
		case SORT:
			SORTcontact(&con);
			break;
		case EXIT:
			Freecontact(&con);
			printf("退出通讯录\n");
			break;
		default:
			printf("命令错误\n");
			break;
		}

	} while (input);
	return 0;
}

这是上一篇文章的代码,要思考,不能照搬噢!

你可能感兴趣的:(笔记,c#,开发语言,c语言)