c语言实现静态通讯录——超级简单版

头文件.c代码

#pragma once

#include 
#include 
#include 

#define MAX_NAME 20
#define MAX_HUMAN 100
#define MAX_SEX 5
#define MAX_ADDS 15
#define MAX_PHONENUM 12


//创建单个人员类型
struct human
{
	char name[MAX_NAME];
	int age;
	char sex[MAX_SEX];
	char phonenum[MAX_PHONENUM];
	char adds[MAX_ADDS];
};
//创建通讯录类型
typedef struct Contact
{
	struct human list[MAX_HUMAN];
	int sz;
}Contact;

//初始化通讯录

void Init_contact(Contact* ps);

//增加通讯录成员

void Add_contact(Contact* ps);

//打印通讯录成员

void Print_contact(const Contact* ps);

//删除通讯录成员
void Del_contact(Contact* ps);

//查找特定成员并打印

void Sreach_contact(const Contact* ps);

//修改特定成员

void Mod_contact(Contact* ps);

主函数.c代码

#define  _CRT_SECURE_NO_WARNINGS 1

#include "contact.h"


void menu()
{
	printf("*****************************\n");
	printf("********0.exit   1.add*******\n");
	printf("********2.del    3.print*****\n");
	printf("********4.sreach 5.mod*******\n");
	printf("*****************************\n");
}

enum option
{
	EXIT,
	ADD,
	DEL,
	PRINT,
	SREACH,
	MOD
};

int main()
{
	Contact n;
	//初始化通讯录
	Init_contact(&n);

	int input = -1;
	do
	{
		menu();
		printf("请输入要执行的操作:>");
		scanf("%d", &input);
		switch (input)
		{
		case EXIT:
			printf("退出通讯录\n");
			break;
		case ADD:
			Add_contact(&n);
			break;
		case DEL:
			Del_contact(&n);
			break; 
		case PRINT:
			Print_contact(&n);
			break;
		case SREACH:
			Sreach_contact(&n);
			break;
		case MOD:
			Mod_contact(&n);
			break;
		default:
			printf("选择错误请重新选择!\n");
			break;
		}

	} while (input);


	return 0;
}

函数实现.c 源码

#define  _CRT_SECURE_NO_WARNINGS 1

#include "contact.h"

//查找用户名是否存在返回-1或下标

static int  Find_name(const Contact* ps, char* arr)
{
	int i = 0;
	for (i = 0; i < ps->sz; i++)
	{
		if (strcmp(ps->list[i].name, arr) == 0)
		{
			return i;
		}
	}
	return -1;
}

void Init_contact(Contact* ps)
{
	memset(ps, 0, sizeof(ps->list));
	ps->sz = 0;
}

void Add_contact(Contact* ps)
{
	if (ps->sz == MAX_HUMAN)
	{
		printf("通讯录已满,无法增加");
		return;
	}
	printf("%-6s %-3s %-5s %-10s %-5s\n", "姓名", "年龄", "性别", "电话号码", "地址");
	scanf("%s", ps->list[ps->sz].name);
	scanf("%d", &(ps->list[ps->sz].age));
	scanf("%s", ps->list[ps->sz].sex);
	scanf("%s", ps->list[ps->sz].phonenum);
	scanf("%s", ps->list[ps->sz].adds);
	ps->sz++;
	printf("增加成功\n");
}

void Print_contact(const Contact* ps)
{
	printf("%-6s %-3s %-5s %-10s %-5s\n", "姓名", "年龄", "性别", "电话号码", "地址");

	int i = 0;
	for (i = 0; i < ps->sz; i++)
	{
		printf("%-6s %-3d %-5s %-10s %-5s\n", ps->list[i].name, 
			ps->list[i].age, 
			ps->list[i].sex, 
			ps->list[i].phonenum, 
			ps->list[i].adds);
	}
}

void Del_contact(Contact* ps)
{
	char name[MAX_NAME] = {0};
	printf("请输入要删除联系人的名字:>");
	scanf("%s", name);
	int ret = Find_name(ps, name);
	if (ret != -1)
	{
		int i = 0;
		for (i = ret; i < ps->sz-1; i++)
		{
			ps->list[i] = ps->list[i + 1];
		}
		if (ret == ps->sz)
		{
			memset(ps->list + ret, 0, sizeof(ps->list[0]));
		}
		ps->sz--;
		printf("删除成功");
	}
	else
	{
		printf("要删除的人不存在。\n");
	}

}

void Sreach_contact(const Contact* ps)
{
	char name[MAX_NAME] = { 0 };
	printf("请输入要查找联系人的名字:>");
	scanf("%s", name);
	int ret = Find_name(ps, name);
	if (ret != -1)
	{
		printf("%-6s %-3d %-5s %-10s %-5s\n", ps->list[ret].name,
			ps->list[ret].age,
			ps->list[ret].sex,
			ps->list[ret].phonenum,
			ps->list[ret].adds);
	}
	else
	{
		printf("查找的人不存在\n");
	}
}

void Mod_contact(Contact* ps)
{
	char name[MAX_NAME] = { 0 };
	printf("请输入要修改联系人的名字:>");
	scanf("%s", name);
	int ret = Find_name(ps, name);
	if (ret != -1)
	{
		printf("%-6s %-3s %-5s %-10s %-5s\n", "姓名", "年龄", "性别", "电话号码", "地址");
		scanf("%s", ps->list[ret].name);
		scanf("%d", &(ps->list[ret].age));
		scanf("%s", ps->list[ret].sex);
		scanf("%s", ps->list[ret].phonenum);
		scanf("%s", ps->list[ret].adds);
		printf("修改成功\n");
	}
	else
	{
		printf("要修改的人不存在");
	}
}

你可能感兴趣的:(c语言,c++,学习)