2018.08.01

改良后的小通讯录:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1000
#define blue "\e[1;34m"      //设置字体颜色
#define yellow "\e[1;33m"
#define red    "\e[0;31m"
#define cyan   "\e[0;36m"
#define purple "\e[0;35m"
#define brown  "\e[0;33m"
#define white  "\e[1;37m"
#define none "\e[0m"


struct student                      //定义结构体
{
	char name[20];
	int num;
};
void welcome()
{
	system("clear");
	printf("*****************************\n");
	printf(red"欢迎来到帅哥的通讯录..........\n"none);
}
void menu()
{
	printf("\n\n\n**********************\n");
	printf(purple"1、添加信息""             " blue  "2、查看信息\n"none);
	printf(cyan  "3、查找信息""             " red   "4、删除信息\n"none);
	printf(yellow"5、修改信息""             " brown "6、排序\n"none);
	printf(white "7、退出                           \n"none);
	printf("******************************\n");
}
void addinfo(struct student *s[])
{
	static int i = 0;     //设置静态变量,作用在于可以在每次添加新联系人时,原联系人不会被覆盖
	printf(cyan"请录入联系人的信息\n"none);
	while(1)  //设置无限循环,方便信息的录入
	{
		s[i] = (struct student *)malloc(sizeof(struct student));
		scanf("%s%d",s[i]->name, &s[i]->num);
		i++;
		getchar();//键盘提前获得一个字符,一般是回车键换行,因为下面需要用回车作判断,所以多设     置一个字符录入,
		if(getchar() == '\n')
		break;
	}
	system("clear");
}
void see(struct student *s[])
{
	int i = 0;
	while(s[i] != NULL)  //由于主函数定义的是结构体指针数组,数组每个元素都是指针,指针 可由是否是空指针判断终止条件
	{
		printf(blue"name:%s num:%d\n"none,s[i]->name, s[i]->num);
		i++;
	}
}
void find(struct student *a[])
{
	system("clear");
	int i = 0,h;
	printf(brown"想查找的人或者想查找的信息:\n"none);
	scanf("%d",&h);
	switch(h)
	{
		case 0:                                          //按姓名查询或者电话号码查询,由用户自己选择
			printf(yellow"请输入要查找的人名:\n"none);
			char *p;
			p = (char *)malloc(sizeof(char) * 64);
			scanf("%s",p);
			while(a[i] != NULL)
			{
				if(strcmp(p, a[i]->name) == 0)
				{
					printf(cyan"name:%s num:%d\n"none,a[i]->name, a[i]->num);
					break;
				}
				i++;
				if(a[i] == NULL)
				{
					printf(red"查无此人!\n"none);
				}
			}
			free(p);
			break;
		case 1:
			printf(red"请输入要查找的电话号码:\n"none);
			int k;
			scanf("%d",&k);
			while(a[i] != NULL)
			{
				if(k == a[i]->num)
				{
					printf(cyan"name:%s num:%d\n"none,a[i]->name, a[i]->num);
					break;
				}
				i++;
				if(a[i] == NULL)
				{
					printf(red"查无此人!\n"none);
				}
			}
			break;
	}

}
void delete(struct student *a[])
{
	system("clear");
	int i = 0;
	printf(purple"请输入想删除的人的名字:\n"none);
	char *p; 
	p = (char *)malloc(sizeof(char) * 64);
	scanf("%s",p);

	while(a[i] != NULL)
			{
				if(strcmp(p, a[i]->name) == 0) // 先查找到这个人,然后释放这个指针指向的空间
				{
					free(a[i]);
					while(a[i] != NULL)
					{
						a[i] = a[i + 1];
						i++;
					}
				}
				i++;
			}
	free(p);
}
void change(struct student *s[])
{
	system("clear");
	int i = 0, m;
	printf(brown"请输入想修改的人的名字:\n"none);
	char *p;
	p = (char *)malloc(sizeof(char) * 64);
	scanf("%s",p);

	while(s[i] != NULL)
			{
				if(strcmp(p, s[i]->name) == 0)
				{
					printf(yellow"请输入想修改后的号码:\n"none);
					scanf("%d",&m);
					s[i]->num = m;
				}
				i++;
			}
	free(p);

}
void sort(struct student *a[])
{
	system("clear");
	int n = 0, i, j;
	struct student *tmp;
	while(a[n] != NULL)
	{
		n++;
	}
	for(i = 0; i < n - 1; i++)
	{
		for(j = 0; j < n - 1 - i; j++)
		{
			if(strcmp(a[j]->name, a[j + 1]->name) > 0)
			{
				tmp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = tmp;
			}
		}
	}


}
int main()
{
	struct student *stu[SIZE] = {0};
	int choice;
	
	welcome();
	while(1)
	{
		menu();
		scanf("%d",&choice);
		switch(choice)
		{
			case 1:
				addinfo(stu);
				break;
			case 2:
				see(stu);
				break;
			case 3:
				find(stu);
				break;
			case 4:
				delete(stu);
				break;
			case 5:
				change(stu);
				break;
			case 6:
				sort(stu);
				break;
			case 7:
				exit(0);
				break;
		
		
		}
	
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/scv5876666/article/details/81347906
今日推荐