通俗易懂的通讯录C语言

通讯录(C语言版)

通讯录功能

1.添加联系人
2.删除联系人
3.查找联系人(可以通过姓名查找,也可以通过电话查找)
4.修改联系人(通过姓名查找)
5.显示所有联系人
6.清空所有联系人
7.排序所有联系人

contact.h

#ifndef _CONTACT_H_
#define __CONTACT_H_

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>


#pragma warning (disable:4996)

#define SIZE 128
#define TOTAL 1000

//姓名、性别、年龄、电话、住址
typedef struct person{
	char name[SIZE / 4];
	char sex;//f,m
	int age;
	char telphone[SIZE / 4];
	char address[SIZE];
}person_t;

typedef struct contact{
	int size;//当前有多少人
	int cap;//总容量有多少人
	person_t persons[TOTAL];
}contact_t;

void AddPerson(contact_t *ct);
void DeletePerson(contact_t *ct);
//index :-1 :show all
//index>=0 : show index person
void ShowContact(contact_t *ct,int index);
void SearchPerson(contact_t *ct);
void ModPerson(contact_t *ct);
void ClsContact(contact_t *ct);
void SortContact(contact_t *ct);

#endif

main.c

#include"contact.h"

static void Menu()
{
	printf("############################\n");
	printf("# 1.Add       2.Del        #\n");
	printf("# 3.Search    4.Mod        #\n");
	printf("# 5.Show      6.Cls        #\n");
	printf("# 7.Sort      8.Quit       #\n");
	printf("############################\n");
	printf("Please Select:>");
}

int main()
{
	contact_t ct;
	memset(&ct, 0, sizeof(ct));//初始化全为0
	ct.cap = TOTAL;//容量为TOTAL,也就是1000

	int quit = 0;
	int select = 0;
	while (!quit)
	{
		Menu();
		scanf("%d",&select);
		switch (select){
		        case 1://Add
					AddPerson(&ct);
					break;
				case 2:
					DeletePerson(&ct);
					break;
				case 3:
					SearchPerson(&ct);
					break;
				case 4:
					ModPerson(&ct);
					break;
				case 5:
					ShowContact(&ct,-1);
					break;
				case 6:
					ClsContact(&ct);
					break;
				case 7:
					SortContact(&ct);
					break;
				default:
					quit = 1;
					break;
		}
	}
	system("pause");
	return 0;
}

contact.c

#include"contact.h"

static int IsFull(contact_t *ct){
	assert(ct);
	return ct->size == ct->cap;
}

static int IsExist(contact_t *ct, const char *telphone){
	assert(telphone);
	person_t *end = ct->persons + ct->size;
	int index = 0;
	for (person_t *p = ct->persons; p < end; p++, index++){
		if (0 == strcmp(p->telphone, telphone)){
			return index;
		}
	}
	return -1;
}
static int IsNameExist(contact_t *ct, const char *name){
	assert(name);
	person_t *end = ct->persons + ct->size;
	int index = 0;
	for (person_t *p = ct->persons; p < end; p++, index++){
		if (0 == strcmp(p->name, name)){
			return index;
		}
	}
	return -1;
}

static void SearchPersonTelHelper(contact_t *ct){
	assert(ct);

	char search_key[SIZE / 4];
	printf("请输入你要查找人的电话号码# ");
	scanf("%s", search_key);

	int index = IsExist(ct, search_key);
	if (-1 == index)
	{
		printf("你要查找的人[%s]不存在!\n", search_key);
		return;
	}
	else{
		ShowContact(ct, index);
	}
}

static void SearchPersonNameHelper(contact_t *ct){
	assert(ct);

	char search_key[SIZE / 4];
	printf("请输入你要查找人的姓名# ");
	scanf("%s", search_key);

	int index = IsNameExist(ct, search_key);
	if (-1 == index)
	{
		printf("你要查找的人[%s]不存在!\n", search_key);
		return;
	}
	else{
		ShowContact(ct, index);
	}
}


static int CompPerson(const void *x, const void *y)
{
	person_t *x_p = (person_t *)x;
	person_t *y_p = (person_t *)y;

	return strcmp(x_p->name, y_p->name);
}

void AddPerson(contact_t *ct){

	assert(ct);

	if (IsFull(ct)){
		printf("contact is full!\n");
		return;
	}
	person_t *p = ct->persons + ct->size;
	printf("姓名# ");
	scanf(" %s", p->name);
	printf("电话# ");
	scanf(" %s", p->telphone);
	printf("性别# ");
	scanf(" %c", &(p->sex));
	printf("年龄# ");
    scanf(" %d", &(p->age));
	printf("地址# ");
	scanf(" %s", p->address);
	ct->size += 1;

	printf("....add success!\n");

}
void DeletePerson(contact_t *ct){
	
	assert(ct);

	char de_key[SIZE / 4];
	printf("请输入你要删除人的电话号码# ");
	scanf("%s", de_key);
	int index = 0;
	if ((index = IsExist(ct, de_key)) >= 0){
		ct->persons[index] = ct->persons[ct->size - 1];
		ct->size -= 1;
		printf("delete success!\n");
	}
	else{
		printf("你要删除的用户[%s],是不存在的!\n", de_key);
	}
}
void SearchPerson(contact_t *ct){
	assert(ct);
	printf("#######################\n");
	printf("#1.按照姓名 2.按照电话#\n");
	printf("#######################\n");
	printf("select:>");
	int select = 0;
	scanf("%d", &select);
	switch (select){
	case 1:
		SearchPersonNameHelper(ct);
		break;
	case 2:
		SearchPersonTelHelper(ct);
		break;
	}
}

void ModPerson(contact_t *ct){

	assert(ct);

	char de_key[SIZE / 4];
	printf("请输入你要修改人的姓名# ");
	scanf("%s", de_key);

	int pos = IsNameExist(ct,de_key);

	if (-1 == pos){
		printf("要修改的人不存在!\n");
	}
	else
	{
		printf("请修改名字:>");
		scanf(" %s", ct->persons[pos].name);
		printf("请修改电话:>");
		scanf(" %s", ct->persons[pos].telphone);
		printf("请修改性别:>");
		scanf(" %c", &(ct->persons[pos].sex));
		printf("请修改年龄:>");
		scanf(" %d", &(ct->persons[pos].age));
		printf("请修改地址:>");
		scanf(" %s", ct->persons[pos].address);
		printf("修改成功!\n");
	}

}

void ShowContact(contact_t *ct,int index)
{
	assert(ct);
	if (-1 == index){
		person_t *end = ct->persons + ct->size;
		printf("统计:%d/%d\n", ct->size, ct->cap);
		printf("-----------------------------------------------\n");
		for (person_t *p = ct->persons; p < end; p++){
			printf("|%-8s|%-2c|%-3d|%-11s|%-10s|\n",
				p->name, p->sex, p->age, p->telphone, p->address);
		}
		printf("-----------------------------------------------\n");
	}
	else{
		person_t *p = ct->persons + index;
		printf("|%-8s|%-2c|%-3d|%-11s|%-10s|\n",
			p->name, p->sex, p->age, p->telphone, p->address);
	}
}
void ClsContact(contact_t *ct)
{
	assert(ct);
	ct->size = 0;
}
void SortContact(contact_t *ct)
{
	assert(ct);
	qsort(ct->persons, ct->size, sizeof(person_t), CompPerson);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值