c语言实现手机通讯录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40301026/article/details/79461255

主要用链表来处理,写完觉的自己对链表的理解和操作都加深了许多,而且设计思路都有很大的提升。
主要实现实现了一下功能:
void add_contacts(People head);//增加联系人
int  del_contacts(People head);//删除联系人
int seek_contacts(People head);//查找联系人
int amend_contacts(People head);//修改联系人
int show_contacts(People head);//显示联系人
int empty_contacts(People head);//清空联系人

主要代码:

contacts.h
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define OK 1
#define ERROR -1
#define MAXSIZE 15

typedef struct Contacts
{
	char name[MAXSIZE];  //姓名
	char sex[MAXSIZE];   //性别
	char phone[MAXSIZE]; //电话号码	
	struct Contacts *next;
}Contacts ,*People;

//主要功能函数
void add_contacts(People head);//增加联系人
int  del_contacts(People head);//删除联系人
int seek_contacts(People head);//查找联系人
int amend_contacts(People head);//修改联系人
int show_contacts(People head);//显示联系人
int empty_contacts(People head);//清空联系人


contacts.cpp
#include"contacts.h"

void add_contacts(People head)//增加联系人(每次只能增加一个)
{
	People p;
	p=(People)malloc(sizeof(Contacts));//头插法
	fflush(stdin);//清空缓存区。
	char a=0;
	printf("whether add to contacts?(y/n)。\n");
	a=getchar();
	if(a=='y'||a=='Y')
	{
		printf("please enter the name of contact.\n");
		scanf("%s",p->name);
		printf("please enter the gender of contact.\n");
		scanf("%s",p->sex);
		printf("please enter your contact number.\n");
		scanf("%s",p->phone);

		p->next=head->next;
		head->next = p;

		printf("The added contact information:\n");
		printf("The name of contact: %s\n",head ->next ->name);
		printf("The gender of contact information: %s\n",head ->next ->sex);
		printf("Your contact number: %s\n",head ->next->phone);
	}
}

int del_contacts(People head)//删除联系人
{
	int n=0;
	char ch=0;
	char name[15];
	char phone[15];
	People p1,p2;
	p1=head;

	if(head->next==NULL)
    {
        printf("Linkedlist is empty !\n");
		return 0;
	}
	else
	{
		printf("Please choose the right way .\n");
		printf("1.Delete by name.\n");
	    printf("2.Delete by phone .\n");
		printf("Please choice 1 or 2 . \n");
		scanf("%d",&n);
	}
	switch(n)
	{
	case 1: printf("Please enter the name you want to find .\n");
			fflush(stdin);
			gets(name);
			while(p1->next!=NULL)
			{
				if(strcmp(p1->next->name,name)==0)
				{
					printf("The user exists .\n");
					printf("%s    ,  %s    ,%s \n",p1->next->name,p1->next->sex,p1->next->phone);
					p2=p1->next;
					p1->next=p2->next;
					free(p2);
				}
				else
					p1=p1->next;
			}
			return OK;break;
	case 2: printf("Please ent that phone you want to find .\n");
		    fflush(stdin);
			gets(phone);
			while(p1->next!=NULL)
			{
				if(strcmp(p1->next->phone,phone)==0)
				{
					printf("The user exists .\n");
					printf("%s    ,  %s    ,%s \n",p1->next->name,p1->next->sex,p1->next->phone);
					p2=p1->next;
					p1->next=p2->next;
					free(p2);
				}
				else
					p1=p1->next;
			}
			return OK;break;
	}
}

int seek_contacts(People head)//查找联系人
{
	People p;
	p=head;
	int ch=0;
	char name[15];
	char phone[15];
	if(head->next==NULL)
    {
        printf("Linkedlist is empty !\n");
	}
	else
	{
		printf("1.Find by name .\n");
		printf("2.Find by phone .\n");
		printf("Please choice 1 or 2 . \n");
		scanf("%d",&ch);
	}
	switch(ch)
	{
	case 1: printf("Please enter the name you want to find .\n");
			fflush(stdin);
			gets(name);
			while(p->next!=NULL)
			{
				p=p->next;
				if(strcmp(p->name,name)==0)
				{
					printf("The user exists .\n");
					printf("%s    ,  %s    ,%s \n",p->name,p->phone,p->sex);  
				}
			}
			return OK;break;
	case 2: printf("Please ent that phone you want to find .\n");
		    fflush(stdin);
			gets(phone);
			while(p->next!=NULL)
			{
				p=p->next;
				if(strcmp(p->phone,phone)==0)
					printf("%s    ,  %s    ,%s \n",p->name,p->phone,p->sex);
			}
			return OK;break;
	}
	return 0;
}

int amend_contacts(People head)//修改联系人
{
	People p;
	p=head;
	int n=0;
	char sex[15];
	char name[15];
	char phone[15];
	if(head->next==NULL)
    {
        printf("Linledlist is empty!\n");
        printf("\n");
    }
	printf("1.Find by name.\n");
	printf("2.Find by phone.\n");
	printf("piease choice 1 or 2 . \n");
	scanf("%d",&n);
	fflush(stdin);
	switch(n)
	{
	case 1: printf("please enter a name to  modify .\n");
			gets(name);
			while(p->next!=NULL)
			{
				p=p->next;
				if(strcmp(p->name,name)==0)
				{
					printf("%s    ,  %s    ,%s \n",p->name,p->phone,p->sex);
					printf("Please enter the information you want to modify.<name ,phone,gender>\n");
					scanf("%s  %s   %s",name,phone,sex);
					strcpy(p->name,name);
					strcpy(p->phone,phone);
					strcpy(p->sex,sex);
					printf("Modified information:");
					printf("%s , %s ,  %s\n",p->name,p->phone,p->sex);
				}
			}
			break;
	case 2: printf("Please enter the phone to modify . \n");
			gets(name);
			while(p->next!=NULL)
			{
				p=p->next;
				if(strcmp(p->phone,phone)==0)
				{
					printf("%s    ,  %s    ,%s \n",p->name,p->phone,p->sex);
					printf("Please enter the information you want to modify.<name ,phone,gender>\n");
					scanf("%s  %s   %s",name,phone,sex);
					strcpy(p->name,name);
					strcpy(p->phone,phone);
					strcpy(p->sex,sex);
					printf("%s , %s ,  %s\n",p->name,p->phone,p->sex);
			    }
			break;
			}
	}
	return 0;
}

int show_contacts(People head)//显示联系人
{
	People p;
	if(head->next==NULL)
		printf("LinkedList is empty !");
	else
	{
		p=head;
		printf("That is all contacts message:\n");
		while(p->next!=NULL)
		{
			p=p->next;
			printf("name:%s         people number:%s                gender:%s\n",p->name,p->phone,p->sex);
		}
	}
	return 0;
}

int empty_contacts(People head) //清空联系人
{
	People p1,p2;
	p1=head;
	p2=head;
	while(head->next!=NULL)
	{
		p1=head->next;
		p2=p1->next;
		head->next=p1->next;
		free(p1);
	}
	if(head->next==NULL)
		printf("All contacts were delete !\n");
	return 0;
}

test.cpp
#include"contacts.h"
int menu(People head)
{
	
	char str1[]="                         ";
	char str2[]="***********";
	int n=0;
	
	system("color 4e");
	printf("\n");
	printf("                 ");
	printf("%s手机通讯录%s\n",str2,str2);
	printf("%s请选择以下功能:\n",str1);
	printf("%s1.增加联系人。\n",str1);
	printf("%s2.删除联系人。\n",str1);
	printf("%s3.查找联系人。\n",str1);
	printf("%s4.修改联系人。\n",str1);
	printf("%s5.显示联系人。\n",str1);
	printf("%s6.清空联系人。\n",str1);
	printf("%s7.退出。\n",str1);
	scanf("%d",&n);
	switch(n)
	{
	case 1: add_contacts(head);break;
	case 2: del_contacts(head);break;
	case 3: seek_contacts(head);break;
	case 4:	amend_contacts(head);break;
	case 5: show_contacts(head);break;
	case 6: empty_contacts(head);break;
	case 7: return 0;break;
	}
	system("pause");
	system("cls");
	return 1;
}
int main()
{
	People head;            //定义一个头节点

	head=(People)malloc(sizeof(Contacts));
	head->next=NULL;

	while(menu(head));
	return 0;
}

测试时遇到主要问题解决总结:

 

1.在修改void add_contacts(People head) 时发现如下的情况,仔细检查代码并无大的错误。

 

 

具体代码:

void add_contacts(People head)//增加联系人(每次只能增加一个)

{

People p;

p=(People)malloc(sizeof(Contacts));//头插法

 

char a=0;

printf("whether add to contacts?(y/n)。\n");

a=getchar();

if(a=='y'||a=='Y')

{

printf("please enter the name of contact.\n");

scanf("%s",p->name);

printf("please enter the gender of contact.\n");

scanf("%s",p->sex);

printf("please enter your contact number.\n");

scanf("%s",p->phone);

 

p->next=head->next;

head->next = p;

 

printf("The added contact information:\n");

printf("The name of contact: %s\n",p->name);

printf("The gender of contact information: %s\n",p->sex);

printf("Your contact number: %s\n",p->phone);

}

}

解决:

通过网络查询发现:是缓存区接收了两个字符的原因,接收了一个  1(菜单选择中的1)和回车程序选择不是 y或 Y ,if()判断进入了 NO, 跳出程序,所以只要清除缓存区的回车键即可。

 

C语言如何清空缓冲区

方法一:

fflush(stdin);

fflush(stdin)在VC上可以使用,但是其他编译器不能保证对fflush的实现。

方法二:

setbuf(stdin, NULL);

setbuf(stdin, NULL);是使stdin输入流由默认缓冲区转为无缓冲区。但缓冲区没有了。

方法三:

char ch;while((ch = getchar()) != '\n' && ch != EOF);

这种方法是最好的方法,因为它使用的是C语言的基本语法,在什么情况都是支持的

 

2.

int menu()

{

People head;            //定义一个头节点

 

head=(People)malloc(sizeof(Contacts));

head->next=NULL;

 

char str1[]="                         ";

char str2[]="***********";

int n=0;

system("color 4e");

printf("\n");

printf("                 ");

printf("%s手机通讯录%s\n",str2,str2);

printf("%s请选择以下功能:\n",str1);

printf("%s1.增加联系人。\n",str1);

printf("%s2.删除联系人。\n",str1);

printf("%s3.查找联系人。\n",str1);

printf("%s4.修改联系人。\n",str1);

printf("%s5.显示联系人。\n",str1);

printf("%s6.清空联系人。\n",str1);

printf("%s7.退出。\n",str1);

scanf("%d",&n);

switch(n)

{

case 1: add_contacts(head);break;

case 2: del_contacts(head);break;

case 3: seek_contacts(head);break;

case 4: amend_contacts(head);break;

case 5: show_contacts(head);break;

case 6: empty_contacts(head);break;

case 7: return 0;break;

}

system("pause");

system("cls");

return 1;

}

int main()

{

while(menu(head));

return 0;

}

问题: 每次运行显示 链表为空!

解决:

在测试代码中 红字的部分写在了menu()中导致head->next 每次都为空,放在int main()很好的解决了这个问题。

 

 

 

switch(n)

{

case 1: printf("please enter phone:\n");

gets(phone);

while(p1->next!=NULL)

{

p1=p1->next;

if(strcmp(p1->phone,phone)==0)

{

printf("%s    %s\n",p1->phone,p1->name);

printf("The confirmation is to be deleted?<y\n>?\n");

ch=getchar();

fflush(stdin);

if(ch=='y'||'Y')

{

p2=p1->next;

p1->next=p2->next;

free(p2);

}

}

}break;

case 2: printf("please enter name:\n");

gets(name);

while(p1->next!=NULL)

{

p1=p1->next;

if(strcmp(p1->name,name)==0)

{

printf("%s    %s\n",p1->phone,p1->name);

printf("The confirmation is to be deleted?<y\n>?\n");

ch=getchar();

if(ch=='y'||'Y')

{

p2=p1->next;

p1->next=p2->next;

free(p2);

return OK;

}

else

return ERROR ;

}

}break;

}

return 0;


猜你喜欢

转载自blog.csdn.net/qq_40301026/article/details/79461255