c_动态通讯录(柔性数组)

实现一个动态通讯录

通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人

将通讯录改成动态的版本:使用了柔性数组(结构体中最后一个元素),所用知识可见 c的动态内存管理
结构体传参:必须传结构体指针,因为结构体不会发生降维问题,所用知识可见 结构体总结
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>

#pragma warning(disable:4996)

#define nameSize 8
#define telSize 12
#define addrSize 20

/*
实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
将通讯录改成动态的版本。
*/

typedef struct information{
	char name[nameSize];
	char sex;
	int age;
	char tel[telSize];
	char addr[addrSize];
}Note;

typedef struct flexible{
	int personNum;
	Note arrNote[0];//柔性结构体数组
}fNote;

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("please input your choice:");
}

void myAdd(fNote *fnote, int *personSum)
{
	printf("please input information<name sex age tel addr>:\n");
	Note note;
	scanf("%s %c %d %s %s", note.name, &(note.sex), &(note.age), note.tel, note.addr);
	if (fnote->personNum > *personSum){
		*(fnote->arrNote + (*personSum)++) = note;
		printf("add succeed!\n");
	}
	else{
		printf("address book is Full!\n");
	}
}

void myDelete(fNote *fnote, int *personSum)
{
	printf("please input the person of name you want to delete:");
	char tmp[nameSize];
	scanf("%s", tmp);
	int i = 0;
	while (i < *personSum){
		if (!strcmp(fnote->arrNote[i].name, tmp)){
			if (*personSum == 1){
				*personSum = 0;
				return;
			}
			//将数组添加的最后一个元素移至此覆盖当前值
			fnote->arrNote[i] = *(fnote->arrNote + (*personSum - 1));
			(*personSum)--;
			printf("delete succeed!\n");
			return;
		}
		i++;
	}
	printf("There is no this person!\n");
}

void myFind(fNote *fnote, int *personSum)
{
	printf("please input the person of name you want to find:");
	char tmp[nameSize];
	scanf("%s", tmp);
	int i = 0;
	while (i < *personSum){
		if (!strcmp(fnote->arrNote[i].name, tmp)){
			printf("name:%-10ssex:%-5cage:%-5dtel:%-15saddr:%-22s\n", fnote->arrNote[i].name, \
				fnote->arrNote[i].sex, fnote->arrNote[i].age, fnote->arrNote[i].tel, fnote->arrNote[i].addr);
			return;
		}
		i++;
	}
	printf("There is no this person!\n");
}

void myModify(fNote *fnote, int *personSum)
{
	printf("please input the person of name you want to modify:");
	char tmp[nameSize];
	scanf("%s", tmp);
	int i = 0;
	while (i < *personSum){
		if (!strcmp(fnote->arrNote[i].name, tmp)){
			printf("please input information<name sex age tel addr>:\n");
			scanf("%s %c %d %s %s", fnote->arrNote[i].name, &(fnote->arrNote[i].sex),\
				&(fnote->arrNote[i].age), fnote->arrNote[i].tel, fnote->arrNote[i].addr);
			printf("modify succeed!\n");
			return;
		}
		i++;
	}
	printf("There is no this person!\n");
}

void myShow(fNote *fnote, int *personSum)
{
	int i = 0;
	if (!*personSum){
		printf("no one!\n");
	}
	while (i < *personSum){
		printf("name:%-10ssex:%-5cage:%-5dtel:%-15saddr:%-22s\n", fnote->arrNote[i].name, \
			fnote->arrNote[i].sex, fnote->arrNote[i].age, fnote->arrNote[i].tel, fnote->arrNote[i].addr);
		i++;
	}
}

void myClear(int *personSum)
{
	*personSum = 0;
	printf("clear succeed!\n");
}


int myCmpNote(const void * note1, const void *note2)
{
	Note *x = (Note *)note1;
	Note *y = (Note *)note2;
	int res = strcmp((*x).name, (*y).name);
	if (res > 0){
		return 1;
	}
	else if (res < 0){
		return -1;
	}
	else{
		return 0;
	}
}

void mySort(fNote *fnote, int *personSum)
{
	int tmp = *personSum;
	qsort(fnote->arrNote, tmp, sizeof(Note), myCmpNote);
	myShow(fnote, personSum);
}

void branch(fNote *fnote, int sel, int *personSum)
{
	switch (sel){
	case 1:
		myAdd(fnote, personSum);
		break;
	case 2:
		myDelete(fnote, personSum);
		break;
	case 3:
		myFind(fnote, personSum);
		break;
	case 4:
		myModify(fnote, personSum);
		break;
	case 5:
		myShow(fnote, personSum);
		break;
	case 6:
		myClear(personSum);
		break;
	case 7:
		mySort(fnote, personSum);
		break;
	default:
		perror("switch error");
	}
}

int  main()
{
	int personSum = 0;
	int num;
	printf("Please enter the number of people to add first:");
	scanf("%d", &num);
	fNote *fnote;
	fnote = (fNote *)malloc(sizeof(fNote) + num *sizeof(Note));
	if (fnote != NULL){
		fnote->personNum = num;
		int sel;
		do{
			menu();
			scanf("%d", &sel);
			system("cls");
			if (!sel){
				exit(EXIT_FAILURE);
			}
			else if (sel > 7 && sel < 0){
				printf("enter error!please try again:[0,7]");
			}
			else{
				branch(fnote, sel, &personSum);
			}
		} while (1);
	}
	free(fnote);
	fnote = NULL;

	system("pause");
	return 0;
}

试结果:
运行界面:先输入需要添加联系人的个数(动态分配)
输入3(添加三个联系人)
加入三个联系人后,显示所有
当再添加联系人的时候,就添加不了
其他功能均测试成功,图(相同)可见 存储1000人的通讯录

猜你喜欢

转载自blog.csdn.net/tec_1535/article/details/80515175