C语言小项目--通讯录的实现

今天给大家带来的是通讯录的一个小项目。其中包括通讯录的初始化、增加、显示、删除、查找、修改、排序功能!

主要功能

初始化通讯录

void InitContact(struct Contact* pc);

增加联系人

void AddContact(struct Contact* pc);

显示所有联系人

void ShowContact(struct Contact* pc);

删除指定联系人

void ShowContact(struct Contact* pc);

查找指定联系人

void SearchContact(const struct Contact* pc);

修改指定联系人

void ModifyContact(struct Contact* pc);

排序所有联系人

void SortContact(struct Contact* pc);

代码实现

/* contact.h */
#pragma once

#define NAME_MAX 30
#define TELE_MAX 12
#define SEX_MAX 5
#define ADDR_MAX 30
#define MAX 10000

#include <stdio.h>
#include <string.h>

struct PeoInfo
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
};

struct Contact
{
	struct PeoInfo data[MAX];
	int sz;
};
// 初始化通讯录
void InitContact(struct Contact* pc);
// 增加联系人
void AddContact(struct Contact* pc);
// 显示所有联系人
void ShowContact(struct Contact* pc);
// 删除指定联系人
void DelContact(struct Contact* pc);
// 查找指定联系人
void SearchContact(const struct Contact* pc);
// 修改指定联系人
void ModifyContact(struct Contact* pc);
// 排序所有联系人
void SortContact(struct Contact* pc);
/* contact.c */
#define _CRT_SECURE_NO_WARNINGS 1

#include "contact.h"

void InitContact(struct Contact* pc)
{
	pc->sz = 0;
	memset(pc->data, 0, sizeof(pc->data));
}

void AddContact(struct Contact* pc)
{
	struct PeoInfo tmp = { 0 }; // 新建联系人
	if (pc->sz == MAX)
	{
		printf("通讯录满了!\n");
	}
	else
	{
		printf("请输入名字:>");
		scanf("%s", tmp.name);
		printf("请输入年龄:>");
		scanf("%d", &tmp.age);
		printf("请输入性别:>");
		scanf("%s", tmp.sex);
		printf("请输入电话:>");
		scanf("%s", tmp.tele);
		printf("请输入地址:>");
		scanf("%s", tmp.addr);
		
		pc->data[pc->sz] = tmp;
		pc->sz++;
	}
}

void ShowContact(struct Contact* pc)
{
	int i = 0;
	printf("%10s\t%5s\t%8s\t%15s\t%30s\n\n", "name", "age", "sex", "tele", "addr");
	for (i = 0; i < pc->sz; i++)
	{
		printf("%10s\t%5d\t%8s\t%15s\t%30s\n",
			pc->data[i].name,
			pc->data[i].age,
			pc->data[i].sex,
			pc->data[i].tele,
			pc->data[i].addr);
	}
}


int FindContactByName(const struct Contact* pc, const char name[])
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;
		}
	}
	// 找不到
	return -1;
}
void DelContact(struct Contact* pc)
{
	if (pc->sz == 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->sz - 1; j++)
		{
			pc->data[j] = pc->data[j + 1];
		}
		pc->sz--;
		printf("删除成功\n");
	}
}

void SearchContact(const struct Contact* pc)
{
	char name[NAME_MAX] = { 0 };
	printf("输入要查找的名字:>");
	scanf("%s", name);
	int pos = FindContactByName(pc, name);
	if (-1 == pos)
	{
		printf("查无此人!\n");
	}
	else
	{
		printf("%10s\t%5s\t%8s\t%15s\t%30s\n\n", "name", "age", "sex", "tele", "addr");
		printf("%10s\t%5d\t%8s\t%15s\t%30s\n",
			pc->data[pos].name,
			pc->data[pos].age,
			pc->data[pos].sex,
			pc->data[pos].tele,
			pc->data[pos].addr);
	}
}

void ModifyContact(struct Contact* 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].tele);
		printf("请输入新的地址:>");
		scanf("%s", pc->data[pos].addr);
	}
}

void SortContact(struct Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("通讯录为空\n");
		return;
	}
	int i, j;
	struct PeoInfo tmp = { 0 };
	for(i = 0; i < pc->sz - 1; i++) // 采用的是冒泡排序
		for(j = 0; j < pc->sz - i - 1; j++)
			if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
			{
				tmp = pc->data[j];
				pc->data[j] = pc->data[j + 1];
				pc->data[j + 1] = tmp;
			}
	ShowContact(pc);
}
/* contactTest.c */
#define _CRT_SECURE_NO_WARNINGS 1

#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.eixt          ****\n");
	printf("******************************\n");
}
enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SHOW,
	SORT
};

int main()
{
	int input = 0;

	struct Contact 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 SORT:
			SortContact(&con);
			break;
		case EXIT:
			printf("退出通讯录\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
	return 0;
}

总结

上述功能已经实现,如果你觉得功能不够,可以自己添加,增加通讯录的完整性,可靠性!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT自习小空间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值