C语言利用结构体实现一个通讯录

该博客介绍如何用C语言实现一个通讯录系统,能存储1000个联系人的详细信息,包括姓名、性别、年龄、电话和住址。系统提供添加、删除、查找、修改、显示、排序和清空联系人等功能。通过`student.h`头文件和`test.c`主程序完成操作,并展示了一个友好的运行界面。
摘要由CSDN通过智能技术生成

实现一个通讯录;

通讯录可以用来存储1000个人的信息,每个人的信息包括:

姓名、性别、年龄、电话、住址

提供以下方法:

1. 添加联系人信息

2. 删除指定联系人信息

3. 查找指定联系人信息

4. 修改指定联系人信息

5. 显示所有联系人信息

6. 以名字排序所有联系人

7. 清空所有联系人

student.h:

#ifndef __CRT
#define _CRT_SECURE_NO_WARNINGS 1
#endif

#ifndef __STUDENT

#ifndef __STDIO_H
#include <stdio.h>
#endif

#ifndef __STDLIB_H
#include <stdlib.h>
#endif

#ifndef __SRTRIG_H
#include <string.h>
#endif

#ifndef __MAXSIZE
#define MAXSIZE 1000
#endif

typedef struct Stu
{
	char name[20];
	char sex[3];
	int age;
	char tel[12];
	char addr[20];
}Stu;

void menu()
/*菜单*/
{
	printf("\n");
	printf("***************************************************************************\n");
	printf("**   ☆☆☆☆★★★★★      欢迎学生信息管理系统      ★★★★★☆☆☆☆**\n");
	printf("**    ☆☆☆★★★★★         1、添加联系人信息        ★★★★★☆☆☆ **\n");
	printf("**    ☆☆☆★★★★★       2、删除指定联系人信息      ★★★★★☆☆☆ **\n");
	printf("**    ☆☆☆★★★★★       3、查找指定联系人信息      ★★★★★☆☆☆ **\n");
	printf("**    ☆☆☆★★★★★       4、修改指定联系人信息      ★★★★★☆☆☆ **\n");
	printf("**    ☆☆☆★★★★★       5、显示所有联系人信息      ★★★★★☆☆☆ **\n");
	printf("**    ☆☆☆★★★★★      6、以名字排序所有联系人     ★★★★★☆☆☆ **\n");
	printf("**    ☆☆☆★★★★★         7、清空所有联系人        ★★★★★☆☆☆ **\n");
	printf("**    ☆☆☆★★★★★            0、退出系统           ★★★★★☆☆☆ **\n");
	printf("***************************************************************************\n");
	printf("\n");
}

void Add(Stu *stu, int *count)
/*添加联系人信息*/
{
	if ((*count) >= MAXSIZE - 1)
	{
		printf("本系统已达到最大容纳人数,不能再添加学生了!\n");
		return;
	}
	printf("请输入要添加的学生的姓名:> ");
	scanf("%s", (stu + (*count))->name);
flag:
	printf("请输入要添加的学生的姓别(男/女):> ");
	scanf("%s", (stu + (*count))->sex);
	if ((strcmp((stu + (*count))->sex, "男") != 0) &&
		(strcmp((stu + (*count))->sex, "女") != 0))
	{
		printf("请输入正确的性别!\n");
		goto flag;
	}
	printf("请输入要添加的学生的年龄:> ");
	scanf("%d", &(stu + (*count))->age);
	printf("请输入要添加的学生的电话:> ");
	scanf("%s", (stu + (*count))->tel);
	printf("请输入要添加的学生的住址:> ");
	scanf("%s", (stu + (*count))->addr);
	printf("添加成功!\n");
	(*count)++;/*已有人数加1*/
}

void Delete(Stu *stu, int *count)
/*删除指定联系人信息*/
{
	char _name[20];
	if ((*count) <= 0)
	{
		printf("此学生系统中还没有学生!\n");
		return;
	}
	printf("请输入您要删除的学生的姓名:> ");
	scanf("%s", _name);
	for (int i = 0; i < (*count); i++)
	{
		if (strcmp((stu + i)->name, _name) == 0)
		{
			for (int j = i; j < (*count) - 1; j++)
			{
				strcpy((stu + j)->name, (stu + j + 1)->name);
				strcpy((stu + j)->sex, (stu + j + 1)->sex);
				(stu + j)->age = (stu + j + 1)->age;
				strcpy((stu + j)->tel, (stu + j + 1)->tel);
				strcpy((stu + j)->addr, (stu + j + 1)->addr);
			}
			(*count)--;
			printf("删除成功!\n");
			return;
		}/*if*/
	}/*for*/
	printf("此学生系统中没有此学生!\n");
}

void Search(const Stu *stu, const int count)
/*查找指定联系人信息*/
{
	char _name[20];
	printf("请输入您要查找的学生的姓名:> ");
	scanf("%s", _name);
	for (int i = 0; i < count; i++)
	{
		if (strcmp((stu + i)->name, _name) == 0)
		{
			printf("*********=======您查抄的学生的信息为=======*********\n");
			printf("    *********   姓名:> %s\n", (stu + i)->name);
			printf("    *********   性别:> %s\n", (stu + i)->sex);
			printf("    *********   年龄:> %d\n", (stu + i)->age);
			printf("    *********   电话:> %s\n", (stu + i)->tel);
			printf("    *********   住址:> %s\n", (stu + i)->addr);
			return;
		}
	}/*for*/
	printf("没有找到您要查找的学生!\n");
}

void Alter(Stu *stu, const int count)
/*修改指定联系人信息*/
{
	char _name[20];
	printf("请输入您要修改的学生的姓名:> ");
	scanf("%s", _name);
	for (int i = 0; i < count; i++)
	{
		if (strcmp((stu + i)->name, _name) == 0)
		{
			printf("请输入修改后的姓名:> ");
			scanf("%s", (stu + i)->name);
			printf("请输入修改后的性别:> ");
			scanf("%s", (stu + i)->sex);
			printf("请输入修改后的年龄:> ");
			scanf("%d", &(stu + i)->age);
			printf("请输入修改后的电话:> ");
			scanf("%s", (stu + i)->tel);
			printf("请输入修改后的住址:> ");
			scanf("%s", (stu + i)->addr);
			printf("修改成功!\n");
			return;
		}
	}/*for*/
	printf("此学生系统中没有您要修改的学生!\n");
}

void Show(const Stu *stu, const int count)
/*显示所有联系人信息*/
{
	if (count == 0)
	{
		printf("此学生系统中没有学生!\n");
	}
	else
	{
		printf("  姓名  |  性别  |  年龄  |     电话    |    住址    \n");
		for (int i = 0; i < count; i++)
		{
			printf("%2s  |%5s  |%6d  |%13s  |%s\n", (stu + i)->name, (stu + i)->sex, (stu + i)->age, (stu + i)->tel, (stu + i)->addr);
		}
	}
}

int StcCmp(const void*num1, const void *num2)
/*快排的比较函数*/
{
	return (strcmp(((Stu *)num1)->name, ((Stu *)num2)->name) > 0) ? 1 : -1;
}
void Sort(Stu *stu, const int count)
/*以名字排序所有联系人*/
{
	if (count == 0)
	{
		printf("此学生系统中没有学生!\n");
		return;
	}
	qsort(stu, count, sizeof(stu[0]), StcCmp);

}

void Empty(Stu *stu, int *count)
/*清空所有联系人*/
{
	if (count == 0)
	{
		printf("此学生系统中没有学生!\n");
		return;
	}
	for (int i = 0; i < *count; i++)
	{
		*(stu + i)->name = NULL;
		*(stu + i)->sex = NULL;
		(stu + i)->age = 0;
		*(stu + i)->tel = NULL;
		*(stu + i)->addr = NULL;
	}
	*count = 0;
	printf("清空成功!");
}
#endif

主程序:test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include "student.h"

#define MAXSIZE 1000

typedef enum EN
{
	EXIT,    //退出
	ADD,     //添加
	DELETE,  //删除
	SEARCH,   //查找
	ALTER,    //修改
	SHOW,     //显示
	SORT,     //排序
	EMPTY,    //清空
}EN;

int main()
{
	Stu stu[MAXSIZE];
	int count = 0; //系统中的现有人数
	int input = 1;
	while (input)
	{
		menu();
		printf("请选择您要进行的服务项目:> ");
		scanf_s("%d", &input);
		switch (input)
		{
		case EXIT:
			printf("感谢您试用本服务系统,欢迎您的下次使用!\n");
			system("pause");
			return 0;
		case ADD:      //添加
			Add(stu, &count);		
			break;
		case DELETE:   //删除
			Delete(stu, &count);
			break;
		case SEARCH:    //查找
			Search(stu, count);
			break;
		case ALTER:    //修改
			Alter(stu, count);
			break;
		case SHOW:     //显示
			Show(stu, count);
			break;
		case SORT:    //排序
			Sort(stu, count);
			Show(stu, count);
			break;
		case EMPTY:   //清空
			Empty(stu, &count);
			break;
		default:
			printf("请选择正确的服务项目!\n");
			break;
		}/*switch*/

	}/*while*/

	printf("\n");
	system("pause");
	return 0;
}

运行界面:

wKiom1ZZqmnRoYg3AABM6U46yHg008.png

进入运行界面后,可根据自己的需求选择合适的服务项目。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值