c语言通讯录的增加.删除.查找.更改.显示.排序.清空

点我⊙通讯录运行视频链接

一.主文件代码

#include "prog.h"
enum a
{
	EXIT=0,
	ADD=1,
	OUT=2,
	SEEK,
	CHANGE,
	SHOW,
	CLEAR,
	SORT,
};
void menu()
{
	printf("-----------------------------------------------\n");
	printf("|**********    1.添加     2.删除     *********|\n");
	printf("|**********    3.查找     4.更改     *********|\n");
	printf("|**********    5.显示     6.清空     *********|\n");
	printf("|**********    7.排序     0.退出     *********|\n");
	printf("-----------------------------------------------\n");
}
int main()
{
	int n = 0;
	contact con;
	InitContact(&con);
	do
	{
		menu();
		printf("请选择>:");
		scanf("%d", &n);
		switch (n)
		{
		case ADD://增
			AddContact(&con);
			break;
		case OUT://删
			OutContact(&con);
			break;
		case SEEK://查
			Seekcontact(&con);
			break;
		case CHANGE://改
			ChangeContact(&con);
			break;
		case SHOW://显示
			ShowContact(&con);
			break;
		case CLEAR://清除
			ClearContact(&con);
			break;
		case SORT://按年龄排序
			SortContact(&con);
			break;
		case EXIT:
			printf("程序退出\n");
			break;
		default:
			printf("选择错误请重新选择\n");
			break;
		}
	} while (n);
	return 0;
}

*二.头文件代码


#include <stdio.h>
#include <string.h>
#define MAX 1000
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_NUMBER 20
#define MAX_RESS 50
typedef struct storage
{
	char name[MAX_NAME];
	int age;
	char sex[MAX_SEX];
	char number[MAX_NUMBER];
	char ress[MAX_RESS];
}storage;
typedef struct contact
{
	storage str[MAX];
	int size;
}contact;
void InitContact(contact *con);//初始化
void AddContact(contact* con);//增
void Seekcontact(contact* con);//查
void OutContact(contact* con);//删
void ShowContact(contact* con);//显示
void ChangeContact(contact* con);//改
void ClearContact(contact* con);//清空
void SortContact(contact* con);//排序

三.函数实现文件

#define _CRT_SECURE_NO_WARNINGS 1
#include "prog.h"
void InitContact(contact *con)//初始化
{
	con->size = 0;//存储数量初始化
	memset(con->str, 0, sizeof(con->str));//存储内容初始化
}




void AddContact(contact* con)//增加信息
{
	if (con->size == MAX)//数量等于大小
	{
		printf("通讯录已满无法继续添加\n");
		return ;
	}
	printf("姓名>:");
	scanf("%s", con->str[con->size].name);
	printf("年龄>:");
	scanf("%d", &(con->str[con->size].age));
	printf("性别>:");
	scanf("%s", con->str[con->size].sex);
	printf("手机号>:");
	scanf("%s", con->str[con->size].number);
	printf("住址>:");
	scanf("%s", con->str[con->size].ress);
	con->size++;//数量加一
	printf("增加成功\n");
}




int FindByName(contact* con, char* name)//遍历通讯录,
{
	int i = 0;
	for (i = 0; i < con->size; i++)
	{
		if (strcmp(con->str[i].name, name) == 0)
		{
			return i;//查找成功返回下标
		}
	}
	return -1;
}




void Seekcontact(contact* con)//查找
{
	if (con->size == 0)
	{
		printf("通讯录为空无法查找\n");
		return;
	}
	printf("请输入你要查找人的姓名>:");
	char name[MAX_NAME] = { 0 };
	scanf("%s", name);
	int pos = FindByName(con, name);
	if (pos == -1)
	{
		printf("通讯录中没有此人\n");
		return;
	}
	else
	{
		printf("%-20s%-10s%-20s%-20s%-50s\n","名字","年龄","性别","手机号","地址");
		printf("%-20s%-10d%-20s%-20s%-50s\n", con->str[pos].name, con->str[pos].age, con->str[pos].sex, con->str[pos].number, con->str[pos].ress);
	}
}





void OutContact(contact* con)//删除
{
	if (con->size == 0)
	{
		printf("通讯录为空,无法删除\n");
		return;
	}
	printf("请输入要删除人的姓名");
	char name[MAX_NAME] = { 0 };
	scanf("%s", name);
	int pos = FindByName(con, name);
	if(pos==-1)
	{
		printf("要删除人不在通讯录\n");
		return;
	}
	int i = 0;
	for (i = pos; i < (con->size - 1); i++)
	{
		con->str[i] = con->str[i + 1];
	}
	con->size--;
	printf("删除成功\n");
}





void ShowContact(contact* con)//打印全部
{
	if (con->size == 0)
	{
		printf("通讯录为空无法显示全部\n");
		return;
	}
	printf("%-20s%-10s%-20s%-20s%-50s\n", "名字", "年龄", "性别", "手机号", "地址");
	int i = 0;
	for (i = 0; i < con->size; i++)
	{
		printf("%-20s%-10d%-20s%-20s%-50s\n", con->str[i].name, con->str[i].age, con->str[i].sex, con->str[i].number, con->str[i].ress);
	}
}




void ChangeContact(contact* con)//改
{
	if (con->size == 0)
	{
		printf("通讯录中没有信息,无法更改\n");
		return;
	}
	printf("请输入要更改信息的名字>:");
	char name[MAX_NAME] = { 0 };
	scanf("%s", name);
	int pos = FindByName(con, name);
	if (pos == -1)
	{
		printf("没有此人\n");
		return;
	}
	else
	{
		printf("%-20s%-10s%-20s%-20s%-50s\n", "名字", "年龄", "性别", "手机号", "地址");
		printf("%-20s%-10d%-20s%-20s%-50s\n", con->str[pos].name, con->str[pos].age, con->str[pos].sex, con->str[pos].number, con->str[pos].ress);
		printf("请输入更改后的信息\n");
		printf("名字>:");
		scanf("%s", con->str[pos].name);
		printf("年龄>:");
		scanf("%d", &(con->str[pos].age));
		printf("性别>:");
		scanf("%s", con->str[pos].sex);
		printf("手机号>:");
		scanf("%s", con->str[pos].number);
		printf("地址>:");
		scanf("%s", con->str[pos].ress);
		printf("修改成功\n");
	}
}






void ClearContact(contact* con)//清空
{
	con->size = 0;
	memset(con->str, 0, sizeof(con->str));
	printf("通讯录已清空所有数据\n");
}



void SortContact(contact* con)//排序
{
	if (con->size == 0)
	{
		printf("通讯录为空,无法排序\n");
		return;
	}
	storage tmp;
	for (int i = 0; i < (con->size - 1); i++)
	{
		for (int j = i + 1; j < (con->size); j++)
		{
			if (strcmp(con->str[i].name,con->str[j].name)>0)
			{
				tmp = con->str[i];
				con->str[i] = con->str[j];
				con->str[j] = tmp;
			}
		}
		printf("按照名字首字母排序成功\n");
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值