文件操作 修改后的通讯录

来源:互联网 发布:python中函数传参数 编辑:程序博客网 时间:2024/05/16 09:26
学完文件操作 修改通讯录后的程序
主函数main.c文件
#include <stdio.h>#include"txu.h"int main(){Node* head = Create_List();if (head == NULL){printf("创建链表失败\n");return -1;}read_data(head);while(1){    menu();    int choice;    printf("请输入你的选择:");    scanf("%d", &choice);    switch(choice)    {    case 1:add_Friend(head);break;    case 2:show_Friend(head);break;    case 3:Find_Friend(head);break;    case 4:Delete_Friend(head);break;    }write_data(head);}    return 0;}
".h"文件
#ifndef __TXU_H__#define __TXU_H__#define FALSE 0#define TRUE  1typedef struct _friend{int id;char name[20];char numb[20];char address[20];char cnumb[20];}Friend;typedef struct _node{Friend friend;struct _node *next;}Node;// 创建链表Node * Create_List();//显示菜单void menu();//添加好友int  add_Friend(Node *h);//列表好友int show_Friend(Node *h);//查找好友int Find_Friend(Node* h);//删除好友int Delete_Friend(Node* h);int Insert_Last(Node *h, Friend friend);int Get_Len(Node * h);void write_data(Node *h);void read_data(Node *h);#endif
函数文件“.c"
#include"txu.h"#include<stdio.h>#include<stdlib.h>#include<string.h>Node * Create_List()//创建链表{Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));if (list == NULL)return NULL;list->next = NULL;   // 空表return list;}void menu(){   printf("\t\t\t***************************************************\n");printf("\t\t\t|                                                 |\n");printf("\t\t\t|                  电子通讯录                      |\n"); printf("\t\t\t***************************************************\n");printf("\t\t\t|                                                 |\n");printf("\t\t\t|-------------------------------------------------|\n");printf("\t\t\t|      1:添加朋友信息        2:列表朋友信息        |\n");printf("\t\t\t|-------------------------------------------------|\n");printf("\t\t\t|      3:搜索查找朋友        4:删除朋友信息        |\n");printf("\t\t\t|-------------------------------------------------|\n");printf("\t\t\t***************************************************\n");}void write_data(Node *h)  //数据写入{FILE *fp = fopen("friend", "wb+");if (fp == NULL){perror ("fopen");return;}Node *temp=h->next;int count=Get_Len(h);fwrite(&count,sizeof(int),1,fp);int i;for(i=0;i<count;i++){int len=sizeof(temp->friend);fwrite(&len,sizeof(int),1,fp);    fwrite(&(temp->friend),sizeof(Friend),1,fp);temp=temp->next;}fclose(fp);}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;}int Insert_Last(Node *h, Friend friend){if (h == NULL)return FALSE;Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));if (node == NULL){return FALSE;}node->friend = friend;node->next = NULL;Node* tmp = h;while (tmp->next){tmp = tmp->next;}tmp->next = node;return TRUE;}void read_data(Node *h)  //数据读取{FILE *fp = fopen("friend", "ab+");if (fp == NULL){perror ("fopen");return;}int count;count=0;fread(&count,sizeof(int),1,fp);        int i;        Friend temp;for(i=0;i<count;i++)       {    int len;    fread(&len,sizeof(int),1,fp);    fread(&temp,len,1,fp);    Insert_Last(h, temp);}fclose(fp);}int  add_Friend(Node *h){if(h==NULL)return FALSE;Node *node=(Node*)malloc(sizeof(Node)/sizeof(char));if (node == NULL){return FALSE;}printf("请输入id:");scanf("%d",&(node->friend.id));printf("请输入name:");scanf("%s",node->friend.name);printf("请输入numb:");scanf("%s",node->friend.numb);printf("请输入address:");scanf("%s",node->friend.address);printf("请输入cnumb:");scanf("%s",node->friend.cnumb);node->next=NULL;//write_data(node);Node* tmp=h;while (tmp->next){tmp = tmp->next;}tmp->next = node;return TRUE;}int show_Friend(Node *h){if(h==NULL)return FALSE;int i=0;while(1){if(h==NULL|| h->next == NULL || h->next->next == NULL)break;    Node *pre=h->next;    Node *cur=h->next->next;Node *tmpl;    Node *p=h;    i=0;while(cur!=NULL)    {    tmpl=cur->next;    if((pre->friend.id)>(cur->friend.id)){cur->next=pre;        pre->next=tmpl;    p->next=cur;        p=p->next;cur=tmpl;        i++;}else{p=pre;pre=cur;cur=tmpl;}    }if(i==0){break;}}Node *tmp=h->next;while(tmp!=NULL){printf("id=%d\t",tmp->friend.id);printf("name=%s\t",tmp->friend.name);printf("numb=%s\t",tmp->friend.numb);printf("address=%s\t",tmp->friend.address);printf("cnumb=%s\n",tmp->friend.cnumb);tmp=tmp->next;}printf("\n");return TRUE;}int Find_Friend(Node* h){if (h == NULL)return FALSE;char *name;printf("请输入一个名字:");scanf("%s",name);Node *tmp=h->next;while(tmp){if(strcmp(tmp->friend.name,name)==0){printf("id=%d\t",tmp->friend.id);        printf("name=%s\t",tmp->friend.name);        printf("numb=%s\t",tmp->friend.numb);        printf("address=%s\t",tmp->friend.address);        printf("cnumb=%s\n",tmp->friend.cnumb);}tmp=tmp->next;}return TRUE;}int Delete_Friend(Node* h){if (h == NULL)return FALSE;Node *tmp=h;char *name;printf("请输入一个名字:");scanf("%s",name);int bn=0;while(tmp->next){         if(strcmp(tmp->next->friend.name,name)==0){printf("name:%s\n",tmp->next->friend.name);printf("id:%d\n",tmp->next->friend.id);bn++;}tmp=tmp->next;}if(bn>1){tmp=h;int id;printf("请输入一个id:");scanf("%d",&id);while(tmp->next){if(tmp->next->friend.id==id){Node *p=tmp->next;tmp->next=p->next;free(p);printf("删除成功");return 0;}tmp=tmp->next;}}else{tmp=h;while(tmp->next){if(strcmp(tmp->next->friend.name,name)==0){       Node *p=tmp->next;       tmp->next=p->next;       free(p);       printf("删除成功");   return 0;}tmp=tmp->next;}}return TRUE;}