通讯录的完成

通讯录是每一个C语言的学习者,在学到数据结构课程后应该实现的程序,用到的知识C语言和数据结构都有涉及,比如:数组,函数,指针,链表,结构体,字符库函数等等。
  

先看代码:

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
struct S
{
	char name[20];    
	char dianhua[15]; 
	char xingbie[5];
	int age;         
	struct S *next;  //用来保存下一个人所在地址
};
void  mune()
{
	printf("+-----------------------------------+\n");
	printf("|     1.添加             2.查找     |\n");
	printf("|     3.删除             4.排序     |\n");
	printf("|     5.显示             6.清空     |\n");
	printf("|     7.修改             0.退出     |\n");
	printf("+-----------------------------------+\n");
}
void dayin(struct S *s)//打印通讯录中所有联系人
{
	while (s != NULL)
	{
		printf("%s\n", s->name);
		s = s->next;
	}
}
void tianjia(struct S **ph)//添加联系人
{
	struct S *p = malloc(sizeof(struct S));
	printf("姓名:");
	scanf("%s", p->name);
	printf("电话:");
	scanf("%s", p->dianhua);
	printf("性别:");
	scanf("%s", p->xingbie);
	printf("年龄:");
	scanf("%d", &p->age);
	p->next = NULL;
	if (*ph == NULL)
	{
		*ph = p;
	}
	else
	{
		struct S *cur = *ph;
		while (cur->next != NULL)
		{
			cur = cur->next;
		}
		cur->next = p;
	}
}
void chazhao(struct S *ph)//查找联系人
{
	printf("通讯录中的人有:\n");
	dayin(ph);
	printf("请输入需要查找人的姓名:\n");
	char name[100]; 
	scanf("%s", name);
	struct S  *cur = ph;
	while (cur != NULL)
	{
		if (strcmp((cur->name), name) == 0)
		{
			printf("%s,此人的电话为:%s\n", cur->name,cur->dianhua);
			break;
		}
		cur = cur->next;
	}
	if (cur == NULL)
	{
		printf("对不起,没有此人\n");
	}
}
void shanchu(struct S **ph)//删除联系人
{
	printf("通讯录中的人有:\n");
	dayin(*ph);
	printf("请输入需要删除的人的姓名:\n");
	char name[100];
	scanf("%s", name);
	struct S *p = NULL;
	struct S *cur = *ph;
	while ( cur!= NULL)
	{
		if (strcmp((cur->name), name) == 0)
		{
			break;
		}
		p = cur;
		cur = cur->next;
	}
	if (p == NULL)
	{
		*ph = cur->next;
		free(cur);
	}
	else
	{
		if (cur != NULL)
		{
			p->next = cur->next;
		}
		free(cur);
	}
}
void paixu(struct S *ph)//对联系人的年龄进行排序
{
	struct S *cur = ph;
	struct S *p = cur;
	while (cur != NULL)
	{
		p = cur;
		while (p != NULL)
		{
			if (p->age < cur->age)
			{
				int  tmp = 0;
				char tmp1[20];
				char tmp2[20];
				char tmp3[20];
				
				tmp = p->age;
				p->age = cur->age;
				cur->age = tmp;


				strcpy(tmp1, p->dianhua);
				strcpy(p->dianhua, cur->dianhua);
				strcpy(cur->dianhua, tmp1);

				strcpy(tmp2, p->name);
				strcpy(p->name, cur->name);
				strcpy(cur->name, tmp2);

				strcpy(tmp3, p->xingbie);
				strcpy(p->xingbie, cur->xingbie);
				strcpy(cur->xingbie, tmp3);
			}
			p = p->next;
		}
		cur = cur->next;
	}
	printf("从小到大的排序已经完成\n");
	dayin(ph);
}
void qingkong(struct S **ph)//清空通讯录
{
	struct S *cur = *ph;
	while (cur != NULL)
	{
		cur = cur->next;
	}
	*ph = cur;
	printf("已经清空\n");
}
void xianshi(struct S *ph)//显示通讯录的中联系人
{
	printf("通讯录中的联系人有:\n");
	while (ph != NULL)
	{
		printf("%s\n", ph->name);
		ph = ph->next;
	}
}
void xiugai(struct S **ph)//对联系人的电话进行修改
{
	printf("通讯录中的人有:\n");
	dayin(*ph);
	printf("请输入需要修改的人的姓名:\n");
	char name[100];
	scanf("%s", name);
	struct S *p = NULL;
	struct S *cur = *ph;
	while (cur != NULL)
	{
		if (strcmp((cur->name), name) == 0)
		{
			break;
		}
		p = cur;
		cur = cur->next;
	}
	if (p == NULL)
	{
		printf("请输入电话:");
		char  dianhua[15];
		scanf("%s", dianhua);
		strcpy(cur->dianhua, dianhua);
	}
	else
	{
		printf("请输入电话:");
		char  dianhua[15];
		scanf("%s", dianhua);
		strcpy(cur->dianhua, dianhua);
	}
}

int main()
{
	mune();
	struct S *s=NULL ;
	int input = 1;
	do
	{
		printf("请输入选项:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			tianjia(&s);
			break;
		case 2:
			chazhao(s);
			break;
		case 3:
			shanchu(&s);
			break;
		case 4:
			paixu(s);
			break;
		case 5:
			xianshi(s);
			break;
		case 6:
			qingkong(&s);
			break;
		case 7:
			xiugai(&s);
			break;
		case 0:
			printf("已退出\n");
			break;
		default:
			printf("输入有误!请重新输入:\n");
			break;
		}
	}
	while (input);
	system("pause");
	return 0;
}

既然是通讯录,第一步自然就是添加联系人!
联系人的添加就是在某一块内存中,拿到一个结构体大小的空间,用来存放它,所以首先就是建立一个指针指向这个空间,然后通过以后的步骤对这个空间进行修改。如果添加两个或者两个以上的联系人,我们需要用链表将多个空间联系在一起。

以四个联系人为例:
添加完成后,接下来就是查找,查找的核心为当然是找,所以就会用到strcmp的字符库函数。



第三步:删除

第四步:排序
排序和冒泡排序的基本思想一样,建立两个指针后,相互比较,根据条件,交换位置。但是交换的不仅仅只有年龄,应该交换名字,性别,年龄,电话。

第五步:显示
显示就是在保存联系人的那个空间里,按顺序找,直到第四个联系人指向NULL,打印每一个联系人的名字。

第六步:清空

第七部:修改
修改的核心来自于第三步删除中的寻找,找到后才能修改。

运行:

猜你喜欢

转载自blog.csdn.net/oldwang1999/article/details/80457727