通讯录(带系统编程)

、main函数

#include <stdio.h>
#include "Linklist.h"

int main()
{	
	Node *head = NULL;
	CreateList(&head);
	//创建链表,初始化
	
	if(NULL == head->next)
		Restore(head);			//输入上次保存的值
	
	while(1)
	{	
		int i;
		
		system("clear");
		Menu();
		
		printf("请选择功能:\n");
		scanf("%d",&i);
		Chose(head,i);//选择功能
	}
	
	return 0;
}

二、头文件

#ifndef _LINKLIST_h_
#define	_LINKLIST_h_

#define SIZE 10

enum{EXIT,ADD,DELETE,FIND,SEEK,CLEAR,SAVE,REVISE};		//功能
enum{ID = 1,NAME,PHONE,TELEPHONE};			//区分信息
typedef enum {FALSE,TRUE}BOOL;

//数据区
typedef struct some_data
{
	int id;			//id
	char name[SIZE];//姓名
	long phone;		//电话
	long telephone;	//家庭电话
}Data;

typedef struct node		//结点
{
	Data data;
	struct node *next;
}Node;

//创建空链表
BOOL CreateList(Node **head);
//插入
BOOL inster_last(Node *head,Data *my_data);
//功能
void Chose(Node *head,int i);	
//添加	
void my_add(Node *head,Data *data);
//删除实现
int  my_detele(Node *head,Data *my_data);
//删除选择
void detele(Node *head,Data *my_data,int j);
//查找选择
void Find(Node *head,Data *my_data,int j);
//查找数据
int my_find(Node *head,Data *my_data);
//修改数据选择
void Revise(Node *head,Data *my_data,int j);
//修改数据
BOOL my_revise(Node *head,Data *my_data);
//打印数据
void Display(Node *head);
//读
void Read();
//清空文件夹
void Clear(Node *head);
//写
void Write(Node *head);
//还原
void Restore(Node *head);	
//菜单
void Menu();
void Menu_delete();
void Menu_find();
void Menu_revise();

#endif //_LINKLIST_h_

三、.c文件

#include "Linklist.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

//创建空链表
BOOL CreateList(Node **head)
{
	*head = (Node *)malloc(sizeof(Node)/sizeof(char));
	if(NULL == *head)
	{
		return FALSE;
	}
	
	(*head)->next = NULL;
	
	return TRUE;
}

//插入
BOOL inster_last(Node *head,Data *my_data)
{
	if(NULL == head || NULL == my_data)
	{
		return FALSE;
	}
	Node *pa = (Node *)malloc(sizeof(Node)/sizeof(char));
	if(	NULL == pa)
	{
		return FALSE;
	}
	pa->data.id = my_data->id;
	strcpy(pa->data.name,my_data->name);
	pa->data.phone = my_data->phone;
	pa->data.telephone = my_data->telephone;
	
	pa->next = NULL;
	while(head->next)
	{
		head = head->next;
	}
	
	head->next = pa;
		
	return TRUE;
}

//主界面菜单
void Menu()
{
	printf("**************************************************\n");
	printf("**\t\t1、添加数据\t\t\t**\n");
	printf("**\t\t2、删除数据\t\t\t**\n");
	printf("**\t\t3、查找数据\t\t\t**\n");
	printf("**\t\t4、查看数据\t\t\t**\n");
	printf("**\t\t5、清空数据\t\t\t**\n");
	printf("**\t\t6、保存数据\t\t\t**\n");
	printf("**\t\t7、修改数据\t\t\t**\n");
	printf("**\t\t0、退出\t\t\t\t**\n");
	printf("**\t\t\t      记得要保存数据哦!!**\n");
	printf("**************************************************\n");
}

//删除界面菜单
void Menu_delete()
{
	printf("\t\t1、按id删除数据\n");
	printf("\t\t2、按name删除数据\n");
	printf("\t\t3、按phone删除数据\n");
	printf("\t\t4、按telephone删除数据\n");
	printf("\t\t0、返回\n");
}

//查找界面菜单
void Menu_find()
{
	printf("\t\t1、按id查找数据\n");
	printf("\t\t2、按name查找数据\n");
	printf("\t\t3、按phone查找数据\n");
	printf("\t\t4、按telephone查找数据\n");
	printf("\t\t0、返回\n");
}

//修改界面
void Menu_revise()
{
	printf("\t\t1、按id修改数据\n");
	printf("\t\t2、按name修改数据\n");
	printf("\t\t3、按phone修改数据\n");
	printf("\t\t4、按telephone修改数据\n");
	printf("\t\t0、返回\n");
}

//功能选择
void Chose(Node *head,int i)
{	
	int j;
	Data *my_data = (Data *)malloc(sizeof(Data)/sizeof(char));
	
	switch(i)
	{
		case ADD:
				system("clear");
				my_add(head,my_data);
			break;
		case DELETE:
				system("clear");
				Menu_delete();
				printf("请选择按什么删除数据\n");
				scanf("%d",&j);
				detele(head,my_data,j);
			break;
		case FIND:
				system("clear");
				Menu_find();
				printf("请选择按什么查找数据\n");
				scanf("%d",&j);
				Find(head,my_data,j);
			break;
		case SEEK:
				//Display(head);
				Read();
			break;
		case EXIT:
			exit(-1);
			break;
		case CLEAR:
			Clear(head);
			break;
		case SAVE:
			Write(head);
			break;
		case REVISE:
				system("clear");
				Menu_revise();
				printf("请选择按什么修改数据\n");
				scanf("%d",&j);
			Revise(head,my_data,j);
			break;
		default :
			printf("无效的选项\n");
			sleep(1);
			break;
	}
	
}

//添加
void my_add(Node *head,Data *my_data)
{	
	printf("请输入id\n");
	scanf("%d",&(my_data->id));
		
	printf("请输入name\n");
	scanf("%s",my_data->name);
		
	printf("请输入phone\n");
	scanf("%ld",&(my_data->phone));
		
	printf("请输入telephone\n");
	scanf("%ld",&(my_data->telephone));
	
	inster_last(head,my_data);	
}

//删除选择
void detele(Node *head,Data *my_data,int j)
{
	int count = 0;
	
	switch(j)
	{				
		case ID:
				printf("请输入要删除的的id:\n");
				scanf("%d",&(my_data->id));
				count = my_detele(head,my_data);
				if(count == 0)
					printf("删除失败\n");
				else
					printf("删除成功\n");
				sleep(1);
			break;
		case NAME:
				printf("请输入要删除的的name:\n");
				scanf("%s",my_data->name);
				count = my_detele(head,my_data);
				if(count == 0)
					printf("删除失败\n");
				else if(count == 1)
					printf("删除成功\n");
				else if(count > 1)
				{
					printf("有%d个相同的名字\n",count);
					printf("请输入要删除的id\n");
					scanf("%d",&(my_data->id));
					count = my_detele(head,my_data);
					if(count == 0)
						printf("删除失败\n");
					else if(count = 1)
						printf("删除成功\n");
				}
				sleep(1);
			break;
		case PHONE:
				printf("请输入要删除的的phone:\n");
				scanf("%ld",&(my_data->phone));
				count = my_detele(head,my_data);
				if(count == 0)
					printf("删除失败\n");
				else
					printf("删除成功");
				sleep(1);
			break;
		case TELEPHONE:
				printf("请输入要删除的的telephone:\n");
				scanf("%ld",&(my_data->telephone));
				count = my_detele(head,my_data);
				if(count == 0)
					printf("删除失败\n");
				else
					printf("删除成功\n");
				sleep(1);
			break;
		case EXIT:
			return;
			break;
		default :
			printf("无效的选项\n");
			sleep(1);
			break;
	}
}

//删除数据
int  my_detele(Node *head,Data *my_data)
{
	if(NULL == head || NULL == my_data)
	{
		return FALSE;
	}
	
	int count = 0;
	Node *tmp = head;
	while(tmp->next)
	{	
		if(tmp->next->data.id == my_data->id || tmp->next->data.phone == my_data->phone\
		 ||tmp->next->data.telephone == my_data->telephone)
		{	
			Node *p1 = tmp->next;
			tmp->next = p1->next;
			free(p1);
			
			return 1;
		}
		if(0 == strcmp(tmp->next->data.name,my_data->name))			//相同名字
		{	
			printf("id=%d,phone=%ld,telephone=%ld,name=%s\n",tmp->next->data.id,\
			tmp->next->data.phone,tmp->next->data.telephone,tmp->next->data.name);
			count++;
		}	
		tmp = tmp->next;
	}
	if(1 == count)
	{	
		Node *tmp1 = head;
		while(tmp1->next)
		{	
			if(0 == strcmp(tmp1->next->data.name,my_data->name))
			{	
				Node *p1 = tmp1->next;
				tmp1->next = p1->next;
				free(p1);
			
				return 1;
			}
			tmp1 = tmp1->next;
		}
	}
	
	return count;
}

//查找选择
void Find(Node *head,Data *my_data,int j)
{
	int count;
	switch(j)
	{				
		case ID:
				printf("请输入要查找的的id:\n");
				scanf("%d",&(my_data->id));
				count = my_find(head,my_data);
				if(count == 0)
				{
					printf("无此id\n");
					sleep(1);
				}
			break;
		case NAME:
				printf("请输入要查找的的name:\n");
				scanf("%s",my_data->name);
				count = my_find(head,my_data);
				if(count == 0)
				{
					printf("无此name\n");
					sleep(1);
				}
			break;
		case PHONE:
				printf("请输入要查找的的phone:\n");
				scanf("%ld",&(my_data->phone));
				count = my_find(head,my_data);
				if(count == 0)
				{
					printf("无此phone\n");
					sleep(1);
				}
			break;
		case TELEPHONE:
				printf("请输入要查找的的telephone:\n");
				scanf("%ld",&(my_data->telephone));
				count = my_find(head,my_data);
				if(count == 0)
				{
					printf("无此telephone\n");
					sleep(1);	
				}
			break;
		case EXIT:
			return;
			break;
		default :
			printf("无效的选项\n");
			sleep(1);
			break;
	}
}

//查找数据
int my_find(Node *head,Data *my_data)
{
	if(NULL == head || NULL == my_data)
	{
		return;
	}
	
	system("clear");
	int count = 0;
	Node *tmp = head;
	while(1)
	{
		while(tmp->next)
		{	
			if(tmp->next->data.id == my_data->id || tmp->next->data.phone == my_data->phone\
			||tmp->next->data.telephone == my_data->telephone || 0 == strcmp(tmp->next->data.name,my_data->name))
			{	
				printf("id:%d\t",tmp->next->data.id);
				printf("name:%s\t",tmp->next->data.name);
				printf("phone:%ld\t",tmp->next->data.phone);
				printf("telephone:%ld\n",tmp->next->data.telephone);
				count++;
			}
			tmp = tmp->next;
		}
		if(0 == count)
			break;
		int i;
		printf("\t\t\t\t请输入0退出\n");
		scanf("%d",&i);
		if(0 == i)
			break;
	}
	return count;
}

//修改选择
void Revise(Node *head,Data *my_data,int j)
{
	switch(j)
	{				
		case ID:
				printf("请输入要修的的id:\n");
				scanf("%d",&(my_data->id));
				if(TRUE == my_revise(head,my_data))
				{
					printf("修改id成功\n");
					sleep(1);
				}
				else
				{
					printf("修改id失败\n");
					sleep(1);
				}
			break;
		case NAME:
				printf("请输入要修改的的name:\n");
				scanf("%s",my_data->name);
				if(TRUE == my_revise(head,my_data))
				{
					printf("修改name成功\n");
					sleep(1);
				}
				else
				{
					printf("修改id失败\n");
					sleep(1);
				}
			break;
		case PHONE:
				printf("请输入要修改的的phone:\n");
				scanf("%ld",&(my_data->phone));
				if(TRUE == my_revise(head,my_data))
				{
					printf("修改phone成功\n");
					sleep(1);
				}
				else
				{
					printf("修改id失败\n");
					sleep(1);
				}
			break;
		case TELEPHONE:
				printf("请输入要修改的的telephone:\n");
				scanf("%ld",&(my_data->telephone));
				if(TRUE == my_revise(head,my_data))
				{
					printf("修改telephone成功\n");
					sleep(1);
				}
				else
				{
					printf("修改id失败\n");
					sleep(1);
				}
			break;
		case EXIT:
			return;
			break;
		default :
			printf("无效的选项\n");
			sleep(1);
			break;
	}
}

//修改数据
BOOL my_revise(Node *head,Data *my_data)
{
	if(NULL == head || NULL == my_data)
	{
		return;
	}
	
	Node *tmp = head;

	while(tmp->next)
	{	
		if(tmp->next->data.id == my_data->id)
		{	
			int new_id;
			printf("请输入要修改成的值:\n");
			scanf("%d",&new_id);
			tmp->next->data.id = new_id;
			return TRUE;
		}
		if(0 == strcmp(tmp->next->data.name,my_data->name))
		{
			char new_name[SIZE];
			printf("请输入要修改成的值:\n");
			scanf("%s",new_name);
			strcpy(tmp->next->data.name,new_name);
			return TRUE;
		}	
		if(tmp->next->data.phone == my_data->phone)
		{
			long new_phone;
			printf("请输入要修改成的值:\n");
			scanf("%ld",&new_phone);
			tmp->next->data.phone = new_phone;
			return TRUE;
			
		}
		if(tmp->next->data.telephone == my_data->telephone)
		{
			long new_telephone;
			printf("请输入要修改成的值:\n");
			scanf("%ld",&new_telephone);
			tmp->next->data.telephone = new_telephone;
			return TRUE;
		}
		tmp = tmp->next;
	}
	return FALSE;
}

//打印
void Display(Node *head)
{
	if(NULL == head)
		return;
	
	Node *tmp = head->next;
	system("clear");
	while(1)
	{
		while(tmp)
		{
			printf("id:%d\t",tmp->data.id);
			printf("name:%s\t",tmp->data.name);
			printf("phone:%ld\t",tmp->data.phone);
			printf("telephone:%ld\n",tmp->data.telephone);
			tmp = tmp->next;
		}
		int i;
		printf("\t\t\t\t请输入0退出\n");
		scanf("%d",&i);
		if(0 == i)
			break;
	}
	printf("\n");
}

//读出数据
void Read()
{
	FILE *fp1 = fopen("1.txt", "r");
	if(NULL == fp1)
	{
		perror("打开文件失败");
		exit(-1);
	}
	
	int count = 0;
	Data *new_data = (Data *)malloc(sizeof(Data)/sizeof(char));
	if(NULL == new_data)
	{
		printf("创建失败\n");
		return;
	}
	system("clear");
	printf("------------------------------------------------------------------\n");
	while(TRUE)
	{
		while(TRUE)
		{
			size_t ret = fread(new_data,sizeof(Data),1, fp1);
				
			if(0 == ret)
			{
				if(0 == feof(fp1))
				{
					printf("读取失败\n");
					exit(-1);
				}
				break;
			}
			printf("id:%d\t",new_data->id);
			printf("name:%s\t",new_data->name);
			printf("phone:%ld\t",new_data->phone);
			printf("telephone:%ld\n",new_data->telephone);
			printf("-----------------------------------------------------------\n");
		}
		int i;
		printf("\t\t\t\t\t\t\t请输入0退出\n");
		scanf("%d",&i);
		if(0 == i)
			break;
	}
	printf("\n");
	
	fclose(fp1);
}

//清空文件夹
void Clear(Node *head)
{
	FILE *fp = fopen("1.txt", "w");
	if(NULL == fp)
	{
		perror("打开文件失败");
		exit(-1);
	}
	
	fclose(fp);
	
	
	while(head->next)
	{
		Node *p = head->next;
		head->next = p->next;
		free(p);
	}
	
	
	printf("清空文件夹成功\n");
	sleep(1);
}

//保存
void Write(Node *head)
{
	Node *tmp = head->next;
	FILE *fp = fopen("1.txt", "w");
	if(NULL == fp)
	{
		perror("打开文件失败");
		exit(-1);
	}
	
	while(tmp != NULL)
	{
		size_t ret = fwrite(&(tmp->data),sizeof(Data),1,fp);
		if(0 == ret)
		{
			if(0 == feof(fp))
			{
				printf("写入文件失败\n");
				return;
			}
		}

		tmp = tmp->next;
	}
	
	fclose(fp);	
}

//还原退出后保存的数据
void Restore(Node *head)
{
	FILE *fp = fopen("1.txt", "r");
	if(NULL == fp)
	{
		perror("打开文件失败");
		exit(-1);
	}
	
	while(1)
	{
		Node *new_index = (Node *)malloc(sizeof(Node)/sizeof(char));
		if(NULL == index)
		{
			return;
		}
		size_t ret = fread(&(new_index->data),sizeof(Data),1,fp);
		if(0 == ret)
		{
			if(0 == feof(fp))
			{
				printf("写入文件失败\n");
				return;
			}
			break;
		}
		
		new_index->next = NULL;
		
		while(head->next)
		{
			head = head->next;
		}
	
		head->next = new_index;
	}
	
	fclose(fp);	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值