其他分享
首页 > 其他分享> > C语言实现手机通讯录

C语言实现手机通讯录

作者:互联网

简单的使用结构体,总共实现了6个功能。

使用vs2019进行编译,如果使用别的编译器可以将第一行删掉。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<math.h>
#include<Windows.h>

typedef struct person {
	char name[100];
	char numb[100];
	char gender[100];
	char age[100];
	char address[100];

}person;

typedef struct addressBook {
	person p[1000];
	int temp_size;
}addressBook;

addressBook mailList;

int beginList() {
	int temp_1 = 0;
	printf("+———————————————————————————+\n");
	printf("|                     通讯录系统                       |\n");
	printf("+———————————————————————————+\n");
	printf("|                    1.增加联系人                      |\n");
	printf("+———————————————————————————+\n");
	printf("|                    2.删除指定联系人                  |\n");
	printf("+———————————————————————————+\n");
	printf("|                    3.查找联系人                      |\n");
	printf("+———————————————————————————+\n");
	printf("|                    4.修改联系人信息                  |\n");
	printf("+———————————————————————————+\n");
	printf("|                    5.查找指定联系人                  |\n");
	printf("+———————————————————————————+\n");
	printf("|                    6.删除所有联系人                  |\n");
	printf("+———————————————————————————+\n");
	printf("|                    0.退出                            |\n");
	printf("+———————————————————————————+\n");
	printf("请输入你要进行的选项:");
	(void)scanf("%d", &temp_1);
	return temp_1;
}

void initList(addressBook* mailList) {//初始化
	mailList->temp_size = 0;
	memset(mailList->p, 0, sizeof(mailList->p));//将数组中的元素全置为0
}

void addList(addressBook* mailList) {//添加联系人
	char Name[100] = { 0 };
	char Numb[100] = { 0 };
	char Gender[100] = { 0 };
	char Age[100] = { 0 };
	char Address[100] = { 0 };
	int temp_1 = 0;

	printf("----------------增加联系人-----------\n");
	while (1) {
		printf("请输入联系人姓名:");
		(void)scanf("%s", &Name);
		strcpy(mailList->p[mailList->temp_size].name, Name);
		printf("请输入联系人电话:");
		(void)scanf("%s", &Numb);
		strcpy(mailList->p[mailList->temp_size].numb, Numb);
		printf("请输入联系人性别:");
		(void)scanf("%s", &Gender);
		strcpy(mailList->p[mailList->temp_size].gender, Gender);
		printf("请输入联系人年龄:");
		(void)scanf("%s", &Age);
		strcpy(mailList->p[mailList->temp_size].age, Age);
		printf("请输入联系人地址:");
		(void)scanf("%s", &Address);
		strcpy(mailList->p[mailList->temp_size].address, Address);
		mailList->temp_size++;

		printf("-----按1继续------------按2返回------\n");
		(void)scanf("%d", &temp_1);
		if (temp_1 == 1) {
			temp_1 = 0;
			system("cls");
		}
		else {
			break;
		}
	}
}

void deleteList(addressBook* mailList) {//删除联系人
	printf("----------------删除联系人-----------\n");
	int temp_3 = 0;
	int temp_1 = 0;
	while (1) {
		printf("请输入所要删除联系人序号(1<=num<=%d):\n", mailList->temp_size);

		if (mailList->temp_size == 0) {//判断通讯录是否为空
			printf("通讯录为空!\n");
			printf("----------------按2返回--------------\n");
			while (1) {
				(void)scanf("%d", &temp_1);
				if (temp_1 == 2) {
					break;
				}
				else {
					printf("输入错误请重新输入!\n");
				}
			}
			break;
		}

		while (1) {
			(void)scanf("%d", &temp_3);//输入相关序号进行删除
			if (temp_3 >= 1 && temp_3 <= mailList->temp_size) {
				if (temp_3 == mailList->temp_size) {
					mailList->temp_size--;
					break;
				}
				else {
					mailList->p[temp_3 - 1] = mailList->p[mailList->temp_size - 1];
					mailList->temp_size--;
					break;
				}
			}
			else {
				printf("输入错误请重新输入!\n");
			}
		}

		printf("-----按1继续------------按2返回------\n");//判断继续还是返回
		(void)scanf("%d", &temp_1);
		if (temp_1 == 1) {
			temp_1 = 0;
			system("cls");
		}
		else {
			break;
		}
	}
}

void alterList(addressBook* mailList) {//查看联系人
	int length = 0;
	int temp_2 = 0;
	while (1) {

		for (int i = 0; i < mailList->temp_size - 1; i++) {//由小到大排序
			for (int j = 0; j < mailList->temp_size - 1 - i; j++) {
				if (strcmp(mailList->p[j].name, mailList->p[j + 1].name) > 0) {
					mailList->p[mailList->temp_size + 1] = mailList->p[j];
					mailList->p[j] = mailList->p[j + 1];
					mailList->p[j + 1] = mailList->p[mailList->temp_size + 1];
				}
			}
		}

		printf("+----------------------------查看联系人------------------------------+\n");
		printf("|-联系人姓名----联系人电话----联系人性别----联系人性别----联系人地址-|\n");
		while (length != mailList->temp_size) {
			printf("| %6s %13s %13s %13s %13s     |\n", mailList->p[length].name, mailList->p[length].numb, mailList->p[length].gender, mailList->p[length].age, mailList->p[length].address);
			length++;
		}
		printf("+--------------------------------------------------------------------+\n");
		printf("\n");
		printf("+------------------------------按1返回-------------------------------+\n");
		(void)scanf("%d", &temp_2);
		if (temp_2 == 1) {
			break;
		}
	}
}

void findList(addressBook* mailList) {//修改联系人
	char Name[100] = { 0 };
	char Numb[100] = { 0 };
	char Gender[100] = { 0 };
	char Age[100] = { 0 };
	char Address[100] = { 0 };
	int temp_1 = 0;
	int temp_2 = 0;
	printf("+---------------修改联系人---------------+\n");
	printf("\n");
	while (1) {

		printf("+-----------------参照表-----------------+\n");
		printf("\n");
		int length = 0;
		printf("+-联系人序号----联系人姓名----联系人电话----联系人性别----联系人性别----联系人地址-+\n");
		while (length != mailList->temp_size) {
			printf("|%6d %13s %13s %13s %13s %13s      |\n", length + 1, mailList->p[length].name, mailList->p[length].numb, mailList->p[length].gender, mailList->p[length].age, mailList->p[length].address);
			length++;
		}
		printf("+----------------------------------------------------------------------------------+\n");

		printf("\n");
		printf("请输入所修改联系人序号(输入*不修改):");
		(void)scanf("%d", &temp_1);

		printf("请输入联系人姓名:");
		(void)scanf("%s", &Name);
		if (Name[0] != '*') {
			strcpy(mailList->p[temp_1 - 1].name, Name);
		}

		printf("请输入联系人电话:");
		(void)scanf("%s", &Numb);
		if (Numb[0] != '*') {
			strcpy(mailList->p[temp_1 - 1].numb, Numb);
		}

		printf("请输入联系人性别:");
		(void)scanf("%s", &Gender);
		if (Gender[0] != '*') {
			strcpy(mailList->p[temp_1 - 1].gender, Gender);
		}

		printf("请输入联系人年龄:");
		(void)scanf("%s", &Age);
		if (Age[0] != '*') {
			strcpy(mailList->p[temp_1 - 1].age, Age);
		}

		printf("请输入联系人地址:");
		(void)scanf("%s", &Address);
		if (Address[0] != '*') {
			strcpy(mailList->p[temp_1 - 1].address, Address);
		}

		printf("-----按1继续------------按2返回------\n");
		(void)scanf("%d", &temp_2);
		if (temp_2 == 1) {
			temp_2 = 0;
			system("cls");
		}
		else {
			break;
		}

	}
}

void alterOnlyList(addressBook* mailList) {
	char Name[100] = { 0 };
	int temp_2 = 0;
	while (1) {
		int length = 0;
		printf("+----------------------------查看联系人------------------------------+\n");
		printf("\n");
		printf("请输入所查找联系人姓名:");
		(void)scanf("%s", &Name);
		if (length == mailList->temp_size) {
			printf("通讯录为空!\n");
			printf("+------------------------------按2返回-------------------------------+\n");
			(void)scanf("%d", &temp_2);
			if (temp_2 == 2) {
				break;
			}
		}
		printf("\n");
		while (1) {
			if (strcmp(mailList->p[length].name, Name) == 0) {
				printf("+-联系人姓名----联系人电话----联系人性别----联系人性别----联系人地址-+\n");
				printf("| %6s %13s %13s %13s %13s     |\n", mailList->p[length].name, mailList->p[length].numb, mailList->p[length].gender, mailList->p[length].age, mailList->p[length].address);
				break;
			}
			else {
				length++;
			}
			if (length > mailList->temp_size) {
				printf("该联系人不存在!\n");
				break;
			}
		}
		printf("+--------------------------------------------------------------------+\n");
		printf("\n");
		printf("+------------------------按1继续----按2返回--------------------------+\n");
		(void)scanf("%d", &temp_2);
		if (temp_2 == 2) {
			break;
		}
		else {
			temp_2 = 0;
			system("cls");
		}
	}
}

void allDeleteList(addressBook* mailList) {
	printf("----------------删除联系人-----------\n");
	int temp_2 = 0;
	int temp_1 = 0;
	while (1) {
		printf("您确认要删除所有联系人吗?(输入 1 直接清除,输入 2 返回目录):");
		(void)scanf("%d", &temp_2);
		if (temp_2 == 1) {
			mailList->temp_size = 0;
			break;
		}
		else {
			break;
		}
	}
}

int main() {
	//1.定义信息结构
	//2.定义通讯录结构
	//3.初始化
	initList(&mailList);
	//4.创建界面
	int choose = 5;
	//定义函数指针
	typedef void(*func)(addressBook* mailList);
	func Mail[] = {
		NULL,
		addList,
		deleteList,
		alterList,
		findList,
		alterOnlyList,
		allDeleteList
	};

	while (1) {
		choose = beginList();
		if (choose == 0) {
			break;
		}
		if (choose < 7 && choose > 0) {
			Mail[choose](&mailList);
			system("cls");
		}
		else {
			printf("输入错误请重新输入:\n");
			system("cls");
		}
	}

	return 0;
}

标签:temp,void,联系人,mailList,C语言,length,通讯录,printf,手机
来源: https://blog.csdn.net/weixin_49312527/article/details/121129072