利用文件知识写一个通讯录

//注意文件在主函数中插入的位置

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


int main()
{
	
	Node *head = Create_List();
	Friend data;
	if (head == NULL)
	{
		printf("创建链表失败\n");
		return -1;
	}
	
	menu();

	
	while(1)
	{
		//读取数据
		read_data(head,data);	
		int i;
		scanf("%d",&i);

		switch(i)
		{
			case 1:
			//添加好友信息
				 menu();
				 Insert_Add(head);
				 break;
			
			case 2:
			//查看好友信息
				 menu();
				 Sort(head);
				 Display(head);
				 break;	 
			case 3:
			//搜索好友
				 menu();
				 Find(head);
				 break;
			case 4:
			//删除好友
				 menu();
				 Delete(head);
				 break;	
			case 5:
			//退出
				 menu(); 
				 //写入数据
				 write_data(head,data);
				 exit(0);
				 break;	
            				 
		}
		
		
	}

		
	return 0;
	
}

#include "friendadress.h"#include <stdlib.h>#include <stdio.h>#include<string.h>void Sort(Node *h){if(h == NULL || h->next == NULL || h->next->next == NULL)return ;int n=Get_Len(h);printf("%d\n",n);int i,j;for(i=0;i<n-1;i++){Node *tmp = h; Node *pur = h->next; Node *cur = h->next->next;for(j=0;j<n-i-1;j++){if(pur->data.id>cur->data.id){pur->next=cur->next; cur->next=pur;tmp->next=cur;tmp=cur;cur=pur->next;}else{tmp = pur;pur = cur;cur = cur->next;}}}}#if 0void Sort(Node *h){if(h == NULL || h->next == NULL || h->next->next == NULL)return ;int n = Get_Len(h);int i,j;for(i=0; i<n-1; i++){Node *pre = h->next;Node *cur = h->next->next;Node *tmp = h;for(j=0; j<n-1-i; j++){if(pre->data.id > cur->data.id){pre->next = cur->next;cur->next = pre;tmp->next = cur;tmp = cur;cur = pre->next;}else{tmp = pre;pre = cur;cur = cur->next;}}}}#endifNode *Create_List(){Node* List = (Node*)malloc(sizeof(Node)/sizeof(char));if (List == NULL)return NULL;List->next = NULL; // 空表return List;}void menu(){system("clear");printf("***********************************************************************\n");printf("* *\n");printf("* 主界面:主要显示软件功能 *\n");printf("* *\n");printf("* 1:添加好友信息 2:列表好友信息 *\n");printf("* 3:搜索好友 4:删除好友 *\n");printf("* 5:退出 *\n");printf("* *\n");printf("* 请输入需要的操作(1-5): *\n");printf("* *\n");printf("* *\n");printf("***********************************************************************\n");}#if 1int Insert_Head(Node *h, Friend data){if (h == NULL)return FALSE;Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));if (node == NULL){return FALSE;}node->data = data;node->next = h->next;h->next = node;return TRUE;}int Insert_Add(Node *h){Friend data;printf("请输入一个id: \t\n");//int id;scanf("%d",&data.id);printf("请输入一个name: \t\n");//Friend data.name;scanf("%s",data.name);printf("请输入一个tel: \t\n");//Friend data.tel;scanf("%s",data.tel);printf("请输入一个adress: \t\n");//Friend data->adress;scanf("%s",data.adress);printf("请输入一个ctel: \t\n");//Friend data->ctel;scanf("%s",data.ctel);if(Insert_Head(h,data)==TRUE)printf("添加成功\n");elseprintf("添加失败\n");return 0;}//搜索好友int Find(Node *h){Friend data;printf("请输入一位好友姓名:");scanf("%s", data.name);Find_Element(h,data);}void Find_Element(Node *h,Friend data){int flag=0;if (h == NULL)return ;Node *tmp = h->next;while (tmp){if (strcmp(tmp->data.name,data.name)==0){ flag=1;printf ("%2d %s %s %s %s\n", tmp->data.id,tmp->data.name,tmp->data.tel,tmp->data.adress,tmp->data.ctel);;}tmp = tmp->next;}if(flag==0) printf("未找到此好友\n");}int Delete(Node *h){Friend data;printf("请输入要删除的一位好友姓名:");scanf("%s", data.name);//printf("删除成功\n");if( Delete_Data(h, data)==TRUE){ printf("请输入要删除的一位好友ID:"); scanf("%d", &data.id);if(Delete_Id(h,data)==TRUE)printf("删除成功\n");} }int Delete_Data(Node* h, Friend data){if (h == NULL)return FALSE;Node *tmp = h->next;while (tmp){{if (strcmp(tmp->data.name , data.name)==0)printf ("%2d %s %s %s %s\n", tmp->data.id,tmp->data.name, tmp->data.tel,tmp->data.adress,tmp->data.ctel); tmp = tmp->next;}} return TRUE;}int Delete_Id(Node* h,Friend data){if (h == NULL)return FALSE;Node *tmp = h;while (tmp->next){if (tmp->next->data.id == data.id)break;tmp = tmp->next;}Node *p = tmp->next;tmp->next = p->next;free(p);return TRUE;}int Get_Len(Node * h){if (h == NULL)return 0;Node *tmp = h;int count = 0;while (tmp->next){count++;tmp = tmp->next;}return count;}void Display(Node *h){if (h == NULL)return;Node *tmp=h->next; printf("好友信息\n");while (tmp){ printf ("%2d %s %s %s %s\n", tmp->data.id,tmp->data.name,tmp->data.tel,tmp->data.adress,tmp->data.ctel);tmp = tmp->next;}printf ("\n");}//写数据void write_data(Node *head,Friend data){Node *p=head->next;if (p == NULL){return ;}FILE *fp=fopen("friend","ab+");if(fp==NULL){perror("fopen");return ;}//要写入的个数int len=Get_Len(p);fwrite(&len,sizeof(Friend),1,fp);int i;for(i=0;i<len;i++){//写入数据长度int n=sizeof(p->data);fwrite(p,sizeof(Friend),1,fp);//写入数据fwrite(p,sizeof(Node),1,fp);p=p->next;}fclose(fp);}//读取数据void read_data(Node *head,Friend data){Node *p=head->next;if (p == NULL){return ;}FILE *fp=fopen("friend","wb+");if(fp==NULL){perror("fopen");return ;}//读记录的个数int count=Get_Len(head);fread(&count,sizeof(Friend),1,fp);printf("记录个数是:%d\n",count);int i;for(i=0;i<count;i++){int len;fread(&len,sizeof(Friend),1,fp);//读取数据fread(p,len,1,fp);printf("id=%d,name=%s,tel=%s,adress=%s,ctel=%s\n",p->data.id,p->data.name,p->data.tel,p->data.adress,p->data.ctel);p=p->next;}fclose(fp);}#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值