通讯录程序(C语言)

通讯录:

//contact.c文件
#include "contact.h"
void InitContact(Contact *pCon)
{
	assert(pCon != NULL);
	pCon->useSize = 0;
	memset(pCon->per, 0, sizeof(pCon->per));
}
void AddContact(Contact *pCon)
{
	if (pCon->useSize == MAX_NUMPERSON)
	{
		printf("不好意思,没地了\n");
		return;
	}
	printf("请输入名字:");
	scanf("%s", pCon->per[pCon->useSize].name);
	printf("请输入性别:");
	scanf("%s", pCon->per[pCon->useSize].sex);
	printf("请输入年龄:");
	scanf("%d", &(pCon->per[pCon->useSize].age));
	printf("请输入电话:");
	scanf("%s", pCon->per[pCon->useSize].tel);
	printf("请输入地址:");
	scanf("%s", pCon->per[pCon->useSize].add);
	pCon->useSize++;
	printf("添加成功!\n");
}
int SearchContact(Contact *pCon)
{
	int i = 0;
	char name[20];
	if (pCon->useSize == 0)
	{
		printf("通讯录为空\n");
		return -1;
	}
	printf("请输入名字:");
	scanf("%s", name);
	for (i = 0; i < pCon->useSize; i++)
	{
		if (strcmp(pCon->per[i].name, name))
		{
			return i;
		}
	}
	return -1;
}
void DelContact(Contact *pCon)
{
	int index = SearchContact(pCon);
	int i = 0;
	if (index == -1)
	{
		printf("查无此人\n");
		return;
	}
	for (i = index; i < pCon->useSize - 1; i++)
	{
		pCon->per[i] = pCon->per[i + 1];
	}
	pCon->useSize--;
}
void ShowContact(Contact *pCon)
{
	int i = 0;
	printf("%-20s %-10s %-10s %-11s %-15s", "姓名", "性别", "年龄", "电话", "地址");
	for (i = 0; i < pCon->useSize; i++)
	{
		printf("%-20s %-10s %-10s %-11s %-15s", pCon->per[i].name, pCon->per[i].sex, pCon->per[i].age, pCon->per[i].tel, pCon->per[i].add);
	}
}
void ClearContact(Contact *pCon)
{
	pCon->useSize = 0;
}
//test.c文件
#include "contact.h"
void memu()
{
	printf("*******1.add********2.search**********\n");
	printf("*******3.del********4.show**********\n");
	printf("*******5.clear********0.exit**********\n");
}
void start()
{
	int input = 0;
	Contact con;
	InitContact(&con);
	do
	{
		memu();
		printf("请输入你的操作:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			AddContact(&con);
			break;
		case 2:
		    SearchContact(&con);
			break;
		case 3:
			DelContact(&con);
			break;
		case 4:
			ShowContact(&con);
			break;
		case 5:
			ClearContact(&con);
			break;
		default:
			break;


		}
	} while(input);
}
int main()
{
	start();
	return 0;
}
//头文件 contact.h
#include<stdio.h>
#include<Windows.h>
#include<assert.h>
#define MAX_NUMPERSON 1000
typedef struct PersonInformation
{
	char name[20];
	char sex[20];
	int age;
	char tel[20];
	char add[20];
}PersonInformation;
typedef struct Contact
{
	int useSize;
	PersonInformation per[MAX_NUMPERSON];
}Contact;
void InitContact(Contact *pCon);
void AddContact(Contact *pCon);
int SearchContact(Contact *pCon);
void DelContact(Contact *pCon);
void ShowContact(Contact *pCon);
void ClearContact(Contact *pCon);





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值