电子通讯录(2)

昨天仅仅写了个只能实现添加好友的,今天把剩下的列举好友、搜索好友和删除好友全都加进来了。但仅仅是个初版,界面还是需要调整好的。

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

#define OK          	0
#define ERROR          -1
#define MALLOC_ERROR   -2

typedef struct friend
{
	int id;                         //结点数据
	char name[20];
	char phone[20];
	char addr[20];
	char company_phone[20];
	struct friend *next;           //结点指针
}Friend;
typedef Friend *PFriend;           //重命名结点指针类型

//添加好友(尾插法)
int Add_Friend(PFriend s,int id)
{
	if (s == NULL)
	{
		return ERROR;
	}
	
	//要插入的朋友信息
	PFriend friend = (PFriend)malloc(sizeof(Friend)/sizeof(char));
	if (friend == NULL)
	{
		return MALLOC_ERROR;
	}
	friend->id = id;
	printf ("\n\tput your name:");
	scanf ("%s",friend->name);
	printf ("\n\tput your phone:");
	scanf ("%s",friend->phone);
	printf ("\n\tput your addr:");
	scanf ("%s",friend->addr);
	printf ("\n\tput your company_phone:");
	scanf ("%s",friend->company_phone);
	
	friend->next = NULL;
	
	//找最后一个结点
	PFriend temp = s;
	while (temp->next)
	{
		temp = temp->next;
	}
	temp->next = friend;
	
	return OK;
	
}

//列举好友
int List_Friend(PFriend s)
{
	if (s == NULL)
	{
		return ERROR;
	}
	
	PFriend temp = s->next;
	if (temp == NULL)
	{
		printf ("\n\n\n");
		printf ("暂无好友信息,请先添加好友!\n\n\n");
	}
	while (temp)
	{
		printf ("\n\tid:%d,name:%s,phone:%s,addr:%s,company_phone:%s\n",temp->id,temp->name,temp->phone,temp->addr,temp->company_phone);
		temp = temp->next;
	}
	
	return OK;
}

//搜索好友
int Search_Friend(PFriend friend,char *name)
{
	PFriend s = friend;
	PFriend temp = NULL;
	
	if (s == NULL)
	{
		system("clear");
		printf ("\n\n\n");
		printf ("\n\t暂无此好友,请重新输入!\n\n\n");
	}
	
	while (s != NULL && (s->next) != NULL)
	{
		temp = s->next;
		if (temp == NULL && strcmp(temp->name,name) != 0)
		{
			system("clear");
			printf ("\n\n\n");
			printf ("\n\t经查询无此好友,请重新输入!\n\n\n");
		}
		else if (temp != NULL && strcmp(temp->name,name) == 0)
		{
			system("clear");
			printf ("\n\n\n");
			printf ("\tid:%d\n",temp->id);
			printf ("\tname:%s\n",temp->name);
			printf ("\tphone:%s\n",temp->phone);
			printf ("\taddr:%s\n",temp->addr);
			printf ("\tcompany_phone:%s\n",temp->company_phone);
			printf ("\n\n\n");
		}
		
		s = s->next;
	}
}

//删除好友
int Delete_Friend(PFriend friend,char *name)
{
	PFriend s = friend;
	PFriend temp = NULL;
	
	while (s != NULL && (s->next) != NULL)
	{
		temp = s->next;
		if (temp == NULL && strcmp(temp->next,name) != 0)
		{
			system("clear");
			printf ("\n\n\n");
			printf ("\n\t该无该好友,无法删除,请重新输入!\n\n\n");
		}
		else if (temp != NULL && strcmp(temp->name,name) == 0)
		{
			s->next = temp->next;
			free(temp);
			system("clear");
			printf ("\n\n\n");
			printf ("\n\t成功删除该好友!\n\n\n");
		}
		
		s = s->next;
	}
	
	return OK;
}

//界面显示
void interface()
{
	printf ("1) 添加好友\n");
	printf ("2) 列举好友\n");
	printf ("3) 搜索好友\n");
	printf ("4) 删除好友\n");
	printf ("请选择:");
}

int main()
{
	int id = 0;
	
	PFriend head_node = (PFriend)malloc(sizeof(Friend)/sizeof(char));
	if (head_node == NULL)
	{
		return MALLOC_ERROR;
	}
	head_node->next == NULL;
	
	//选择
	char choice[2];
	system("clear");            //清屏
	while(1)
	{
		interface();
		scanf ("%s",choice);      //字符串以"\n"结尾
		switch(choice[0])
		{
			case '1'://添加好友
			{
				id++;
				if (Add_Friend(head_node,id) != OK)
				{
					return ERROR;
					system("clear");
					printf ("\n\n\n");
					printf ("\t添加好友失败,请重新添加!");
				}
				system("clear");
				printf ("\n\n\n");
				printf ("\t添加好友成功!\n");
				break;
			}
			case'2'://列举好友
			{
				system("clear");
				printf ("\n\n\n");
				List_Friend(head_node);
				break;
			}
			case'3'://搜索好友
			{
				char name[20];
				system("clear");
				printf ("\n\n\n");
				printf ("\n\t请输入要搜索的好友姓名:");
				scanf ("%s",name);
				Search_Friend(head_node,name);
				break;
			}
			case'4'://删除好友
			{
				char name[20];
				system("clear");
				printf ("\n\n\n");
				printf ("\n\t请输入要删除的好友姓名:");
				scanf ("%s",name);
				Delete_Friend(head_node,name);
				break;
			}
			default:
			{
				system("clear");
				printf ("\n\n\n");
				printf ("请输入正确的选项!\n\n\n");
				break;
			}
		}
	}
	
	system("claer");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值