能够实现数据的读写的通讯录

在学完文件编程之后,我们可以对之前的通讯录进行文件的保存,所以昨天我改了一下,用两个结构体来做,有一个结构体钻们用来保存用户信息,把能够保存数据用户数据新通讯录发到我的博客上去。
#ifndef __INFORMATION_H__
#define __INFORMATION_H__

#define FALSE -1
#define TRUE  0
#define N   20
#define SIZE 1024

typedef struct _data
{
	int  ID;                 //好友ID
	char Name[N];           //姓名(英文)
	char Phone_Num[N] ;     //手机号
	char Home_Address[N];	//家庭住址
	char Company_Tell[N];   //公司电话	
}Linkdata;

typedef struct _node
{
	
	Linkdata data;
    struct _node *next;		
}NODE, *PNode;


//菜单界面
void Menu();

//创建一个链表
PNode create_list();

//添加好友信息( 尾插法插入好友信息)
int Add_Friend(PNode h);

//列出好友(升序排列)
int Friend_Information(PNode h);

//通过输入好友姓名搜索并显示该好友的信息
int Search_Friend(PNode h ,char *name);

//删除好友
int Delete_Friend(PNode h ,char *name);
//保存数据
int save_information(PNode h);
//读出数据
int read_information(PNode h); 
//读数据时创的无信息的结点
int add (PNode h);
#endif

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

PNode create_list()
{
	PNode list  =  (PNode)malloc(sizeof(NODE)/sizeof(char));
	if(list == NULL)
		return NULL;
	list->next = NULL;
    
    return list;	
}
void Menu()
{
	system("clear");
	printf("\t**********************************************\n");
	printf("\t~               欢迎使用通讯                 ~\n");
	printf("\t~                                            ~\n"); 
	printf("\t~              1>>>>>添加好友                ~\n");
	printf("\t~              2>>>>>列出好友                ~\n");
	printf("\t~              3>>>>>搜索好友                ~\n"); 
	printf("\t~              4>>>>>删除好友                ~\n");
	printf("\t~              5>>>>>退出                    ~\n");
	printf("\t~                                            ~\n");
	printf("\t~                                            ~\n");
	printf("\t~                               作者:倪鹏程 ~\n"); 
	printf("\t**********************************************\n");
	printf("\t~                                            ~\n");
	printf("\t提示:请输入对应数字选择相应功能               \n");
}
//尾插法插入好友信息
int Add_Friend(PNode h)
{	
	if(h == NULL)
		return FALSE;

	PNode p = (PNode)malloc(sizeof(NODE)/sizeof(char));
	if(p == NULL)
		return FALSE;
	p->next = NULL;
	system("clear");
	printf("\t*********添加好友信息*********\n");
	
	printf("\t您的好友的ID:");
	scanf("%d",&p->data.ID);
	printf("\n");
	
	printf("\t您的好友的姓名:");
	scanf("%s",p->data.Name);
	printf("\n");
	
	printf("\t您的好友的手机号:");
	scanf("%s",p->data.Phone_Num);
	printf("\n");
	
	printf("\t您的好友的家庭住址:");
	scanf("%s",p->data.Home_Address);
	printf("\n");
	
	printf("\t您的好友的公司电话:");
	scanf("%s",p->data.Company_Tell);
	printf("\n");
	
	PNode tmp = h;
	while(tmp->next)
	{
		tmp = tmp->next;
	}
	tmp->next = p;
	
	return TRUE;
}
//列出好友
int Friend_Information(PNode h)
{	
	int count = 0;
	int tmp;
	int i,j;
	PNode p = h->next;
	printf("***************好友信息****************\n");
	
	if(p == NULL)
	{
		printf("没有好友信息。\n");
	}
	//用count来测量链表节点的个数
	while(p)
	{
		count++;
		p = p->next;
	}
	p = h->next;
	//冒泡排序
	for(i = 0; i < count - 1; i++)
	{
		for(j = 0; j < count - i - 1; j++)
		{
			if(p->data.ID > p->next->data.ID)
			{
			    tmp = p->data.ID;
                p->data.ID  =  p->next->data.ID;
                p->next->data
				.ID = tmp;				
			}
			p = p->next;
		}
		p = h->next;
	}
	p = h->next;
	while(p)
	{
	    printf("****************************************\n");
	    printf("ID:%d\n姓名:%s\n手机号:%s\n家庭住址:%s\n公司电话:%s\n",
		p->data.ID,p->data.Name,p->data.Phone_Num,p->data.Home_Address,p->data.Company_Tell);
		printf("****************************************\n");
		p = p->next;
	}
	printf("\n");
	return TRUE;
}
//搜索好友
int Search_Friend(PNode h,char *name)
{
	PNode p = h;
	PNode q = NULL;
	int  flag = 0;
	
	if((p != NULL)&&(p->next != NULL))
	{
		while(p->next)
		{
			q = p->next;
			if((strcmp(q->data.Name,name) == 0)&&(q != NULL))
			{
				flag = 1;
				printf("****************************************\n");
				printf("该好友信息:\nID:%d\n姓名:%s\n手机号:%s\n 家庭住址:%s\n公司电话:%s\n",
				q->data.ID,q->data.Name,q->data.Phone_Num,q->data.Home_Address,q->data.Company_Tell);
				printf("****************************************\n");
			}
			p = p->next;
		}
	}
	if(flag != 1)
	{
		printf("没有该好友的信息\n");
	}
	return TRUE;
}
//删除好友
int Delete_Friend(PNode h, char *name)
{
	int count = 0;  //用count来表示出相同名字的个数     
	PNode p = h;
	PNode q = NULL;
	printf("***************删除好友****************\n");
	while(p->next)     //遍历所有结点
	{
		q = p->next;
		if(strcmp(q->data.Name, name) == 0)
		{    
	        count++;
		}
		p = p->next;
	}
	printf("一共有%d个相同名字的信息 \n",count);
	p = h;
 	if(count > 1)
	{
		Search_Friend(h, name);
		//p = h;
		int num;
		printf("请输入你要删除的ID :  ");
		scanf("%d",&num);
		
		while((p != NULL)&&(p->next != NULL))
		{
			q = p->next;
			if(q->data.ID == num)
			{
				p->next = q->next;
				free(q);
				printf("删除成功\n");
			}
			p = p->next;
		}
	}
	if(count == 1)
	{
		while((p != NULL)&&(p->next != NULL))
		{
			q = p->next;
			if(strcmp(q->data.Name, name) == 0)
			{
				p->next = q->next;
				free(q);
				printf("删除成功\n");
			}
			p = p->next;
		}
	}
	if(count == 0)
	{
		printf("删除失败,没有该好友的信息\n");
	}
	return TRUE;
}
int add(PNode h)
{	
	if(h == NULL)
		return FALSE;

	PNode p = (PNode)malloc(sizeof(NODE)/sizeof(char));
	if(p == NULL)
		return FALSE;
	p->next = NULL;	
	PNode tmp = h;
	while(tmp->next)
	{
		tmp = tmp->next;
	}
	tmp->next = p;
	
	return TRUE;
}
int  save_information(PNode h)
{
	FILE *fp = fopen("information.txt", "w+");
	if(fp == NULL)
	{
		perror("fopen");
		return FALSE;
	}
	PNode  p = h->next;
	while(p)
	{
		fwrite(&p->data ,sizeof(Linkdata) ,1, fp);
		p = p->next;
	}
    fclose(fp);
	return TRUE;
}

int  read_information(PNode h)
{
	if(h == NULL)
		return FALSE;
	FILE *fp = fopen("information.txt", "a+");
	if(fp == NULL)
	{
		perror("fopen");
		return FALSE;
	}
    Linkdata buf = {0};
	
	PNode p = h; 
	while(fread(&buf , sizeof(Linkdata), 1, fp))
	{   
		add(h);
		p->next->data = buf;
		//memset(&buf, 0, sizeof(Linkdata));
		p = p->next;
	}
	fclose(fp);
	return TRUE;
}

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


int main()
{
	int num;
	int num1;
	char name[N];
    PNode head = create_list();
	int j;
	read_information(head);
	while(1)
	{	
		Menu();
		scanf("%d",&num);
	    switch(num)
	    {
		    case 1:              //添加好友
		    {   	
				Add_Friend(head);
				printf("\n");
				
				printf("添加成功\n");
				getchar();
				printf("请输入回车键返回主菜单!\n");
				getchar();
                break;				
		    }
		    case 2:                      //列出好友
		    {
				system("clear");
				Friend_Information(head);
				
				getchar();
				printf("请输入回车键返回主菜单!\n");
				getchar();
				break;			
		    }
			case 3:                       //查询好友
			{
				system("clear");
				printf("请输入你要查询好友的姓名:");
				scanf("%s",name);
				Search_Friend(head, name);
				
				getchar();
				printf("请输入回车键返回主菜单!\n");
				getchar();
				break;	
			}
			case 4:                           //删除好友
			{
				system("clear");
				printf("请输入你要删除好友的姓名:");
				scanf("%s",name);
				Delete_Friend(head, name);
				
				getchar();
				printf("请输入回车键返回主菜单!\n");
				getchar();
				break;	
			}
			case 5:                        //退出
			{
                system ("clear");
				save_information(head);
                return TRUE;
			}
			default:
			{
				system("clear");
				getchar();
				printf("请严格按照数字提示完成相应操作!\n");
				printf("请输入回车键返回主菜单!\n");
				getchar();
				break;
			}
	    }
	
	
	}
	
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值