通讯录

/**********************************************************
File Name:         通讯录项目
Author:            xxx      Date:2016-12-04
Description: 实现通讯录功能
Fuction List:       show()   	 显示最初布局
					Init_list()	 添加好友信息
					Print()		 列表好友信息
					Search()	 搜索好友
					Delete() 	 删除好友
************************************************************/
#include
   
   
    
    
#include
    
    
     
     

#define ok       	   0
#define error    	  -1
#define malloc_error  -2
#define N             20

typedef struct node
{
	int ID;
	char name[N];
	char tel[N ];
	char address[N];
	char company_phone[N];
	struct node *next;
}Address;
typedef Address* PAddress;

//显示最初布局
void show()
{
	system("clear");
	printf("\t\t*********************************************\n");
	printf("\t\t* \twelcome to address book             *\n");
	printf("\t\t*                                           *\n");
	printf("\t\t*\tA) Add the information of friends   *\n");
	printf("\t\t*\tB) Show all friends information     *\n");
	printf("\t\t*\tC) Search for the friend            *\n");
	printf("\t\t*\tD) Remove the friend       	    *\n");
	printf("\t\t*                                           *\n");
	printf("\t\t*********************************************\n");
	printf("                                         \n");
	printf("                                         \n");
	printf("                                         \n");
	printf("                                         \n");
	printf("         please input your choice:");
}

//添加好友信息
int Init_list(PAddress h,int i)
{
	if(h == NULL)
	{
		return error;
	}
	
	PAddress p = (PAddress)malloc(sizeof(Address)/sizeof(char));
	if (p == NULL)
	{
		return malloc_error;
	}
	
	system("clear");
	
	p->ID = i;                             
	
	printf("\n		please input the name:");
	scanf("%s", p->name);                             //输入姓名

	printf("\n		please input the tel:");
	scanf("%s", p->tel);                           //输入手机号
	
	int j = 0;
	int len = 0;
	while(p->tel[j++] != '\0')
	{
		len++;
	}
	while(len != 11)                //判断输入的手机号码是不是11位的
	{
		
		printf("\n		please input 11 numbers,input again:");
		scanf("%s", p->tel);
		len = 0;
		j = 0;
		while(p->tel[j++] != '\0')
		{
			len++;
		}
	}

	printf("\n		please input the address:");
	scanf("%s", p->address);

	printf("\n		please input the company phone:");
	scanf("%s", p->company_phone);
	
	j = 0;
	len = 0;
	while(p->company_phone[j++] != '\0')
	{
		len++;
	}
	while(len != 8)             //判断输入的电话号码是不是8位的
	{
		
		printf("\n		please input 8 numbers,input again:");
		scanf("%s", p->company_phone);
		len = 0;
		j = 0;
		while(p->company_phone[j++] != '\0')
		{
			len++;
		}
	}
			
	p->next = NULL;
	
	PAddress temp = h;
	while (temp->next)
	{
		temp = temp->next;
	}
	temp->next = p;
	
	return ok;
}

//列表好友信息
void Print(PAddress h)
{
	if(h == NULL)
	{
		return;
	}
	
	if(h->next == NULL)                  //判断链表是否为空
	{
		printf("\n\n");
		printf("There is no friend!\n");
		printf("\n\n");
	}
	
	PAddress temp = h->next;
	while(temp)
	{
		printf("Num:%d  name:%s  tel:%s  address:%s  company phone:%s\n", 
		temp->ID, temp->name, temp->tel, temp->address, temp->company_phone);
		temp = temp->next;
	}
	printf("\n");
}

//搜索好友
int Search(PAddress h,int i)
{
	if(h == NULL)
	{
		return error;
	}
	int flag = 1;
	PAddress temp = h->next;
	while(temp)
	{
		if(temp->ID == i)
		{
			flag = 0;
			printf("\n\n");
			printf("Num:%d  name:%s  tel:%s  address:%s  company phone:%s\n", 
			temp->ID, temp->name, temp->tel, temp->address, temp->company_phone);
			printf("\n\n");
		}
		temp = temp->next;
	}
	if(flag)
	{
		printf("\n\n");
		printf("Don't have the friend\n");
		printf("\n\n");
	}
	
	return ok;
}

//删除好友
int Delete(PAddress h, int i)
{
	if(h == NULL)
	{
		return error;
	}
	
	PAddress temp = h;
	
	if(temp->next == NULL)                //空表        
	{
		return error;
	}
	
	if(temp->next->next == NULL)             //一个结点的表
	{
		if(temp->next->ID == i)
		{
			PAddress tmp = temp->next;
			temp->next = NULL;
			free(tmp);
			printf("\n\n");
			printf("Deleted successfully\n");
			printf("\n\n");
			
			return ok;
		}
		else
		{
			return error;
		}
	} 
	
	while(temp)                     // 此处temp 不能写temp->next  要考虑删除的是最后一个结点
	{
		if(temp->next->ID == i)
		{
			PAddress p = temp->next;
			temp->next = p->next;
			free(p);
			printf("\n\n");
			printf("Deleted successfully\n");
			printf("\n\n");
			return ok;
		}
		if(temp->next->next == NULL )         //考虑i超出了表中元素的时候
		{
			return error;
		}			
		temp = temp->next;
	}
}

int main()
{
	char option[2];                              //定义一个字符串存放输入的选项  去除回车键对循环的影响
	int back = 1;
	int idd = 1;
	
	PAddress head_node = (PAddress)malloc(sizeof(Address)/sizeof(char));     //定义头结点
	if(head_node == NULL)
	{
		return malloc_error;
	}
	head_node->next = NULL;
	
	while(back--)
	{
		show();
		scanf("%s", option);
		
		 switch (option[0])
		 {
			 case 'A' :
			 {
				Init_list(head_node,idd++);
		
				system("clear");
				printf("\n\n\n\n\n\n");
				printf("		Congratulations on your success to create contacts!\n");
				printf("\n\n\n\n\n\n");
				printf("		If you want to back,please input (1):");
				scanf("%d", &back);
				break;
			}
			
			case 'B' :
			{
				back = 0;
				system("clear");
				printf("\n\n\n\n\n\n");
				
				Print(head_node);
				
				printf("If you want to back,please input (1):");
				scanf("%d", &back);
				break;
			}
			
			case 'C' :
			{
				int id;
				back = 0;
				system("clear");
				printf("\n\n\n\n\n\n");
				printf("if you want to search your friend,please input ID:");
				scanf("%d",&id);
				
				Search(head_node,id);
				
				printf("If you want to back,please input (1):");
				scanf("%d", &back);
				break;
			}
			
			case 'D' :
			{
				int id;
				back = 0;
				system("clear");
				printf("\n\n\n\n\n\n");
				printf("if you want to delete your friend,please input ID:");
				scanf("%d",&id);
				
				if (Delete(head_node,id) != ok)
				{
					printf("\n\n");
					printf("num %d friend is not exist!", id);
					printf("\n\n");
				}
				printf("If you want to back,please input (1):");
				scanf("%d", &back);
				break;
			}
			
			default :
			{
				printf("\n\n");
				system("clear");
				printf("\n\n\n\n\n\n");
				printf("\t\tplaese input A、B、C or D!\n");
				printf("\n\n");
				printf("If you want to back,please input (1):");
				scanf("%d", &back);
				
				break;
			}
		 }
	
	}
	return 0;
}

    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值