通讯录(文件存储)

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <stdlib.h>
#include <assert.h>
#include<string.h>


struct person {
    char name[32];     
    char phone[12];     
    char gender[5];     
    unsigned char age;  
    char adress[20];

}; 
struct txl
{
   struct person ren;
   struct txl *next;
};
int menu()
{
       int n = 0;
       printf("\n");
       printf("*************************\n");
       printf("***     通讯录系统    ***\n");
       printf("***1.添加       2.删除***\n");
       printf("***3.查找       4.修改***\n");
       printf("***5.显示       6.清空***\n");
       printf("***7.排序       8.保存***\n");
       printf("***9.读入       0.退出***\n");
       printf("*************************\n");
       printf("请选择功能:");
       scanf("%d",&n);
       return n;

}
void  insert(struct txl **head)
{

    int i = 0;
    int n = 0;
    assert(head != NULL);   
    printf("请输入所添加人数");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
    struct txl *p = (struct txl*)malloc(sizeof(struct txl));
    p ->next = NULL;
    printf("name:");
    scanf("%s",p->ren.name);
    printf("gender:");
    scanf("%s",p->ren.gender);
    printf("age:");
    scanf("%d",&p->ren.age);
    printf("phone:");
    scanf("%s",p->ren.phone);
    printf("adress:");
    scanf("%s",p->ren.adress);
    printf("\n");
    p->next = *head;
    *head = p;

    }
}
void   print(struct txl *head)
{   
    while(head != NULL){

        printf("name:%s,gender:%s,age:%d,phone:%s,adress:%s",head->ren.name,head->ren.gender,
            head->ren.age,head->ren.phone,head->ren.adress);
        printf("\n");
        head = head->next;

    }


}
void  Drop(struct txl **head)

 {  
    char name[32];
    struct txl *pre = NULL;
    struct txl *cur = *head;
    assert(*head != NULL);
    printf("请输入要删除的人的名字:");
    scanf("%s", name);

    while ( cur != NULL ) 
    {
        if ( strcmp(cur->ren.name, name) == 0)
            break;
        pre = cur; // 将当前节点付给pre 
        cur = cur->next; // 当前节点走向下一个 
    } 

    if ( pre == NULL ) 
    { // 第一个节点 
        *head = cur->next; // 让头指针指向第二个节点 
        free(cur); // 释放当前节点 
    } 
    else 
    { // 不是第一个节点 
        if ( cur != NULL ) 
        { 
            pre->next = cur->next;
            free(cur);
        } 
        else // 防止没有找到,cur为NULL
            printf("查无此人");
    }
} 

void  find(struct txl *head)
{   
    struct txl *cur = head;
    char name[32];
    printf("姓名:");
    scanf("%s", name);
    while ( cur != NULL  )
    {
        if ( strcmp(cur->ren.name, name) == 0 ) {
            printf("name:%s,gender:%s,age:%d,phone:%s,adress:%s",cur->ren.name,cur->ren.gender,
            cur->ren.age,cur->ren.phone,cur->ren.adress);
            break;
        } 
        cur = cur->next;
    }
    if ( cur == NULL ) {
        printf("查无此人\n"); 
    }


}
void sort(struct txl *head)
{
    struct txl *cur = head;
    struct txl *p = cur;
    while ( cur != NULL  ) {
        p = cur;
        while (p != NULL ) {
            if ( strcmp(p->ren.name ,cur->ren.name) < 0 ) {
                char tmp[100];
                int k = 0;
                strcpy(tmp, p->ren.name);
                strcpy(p->ren.name, cur->ren.name);
                strcpy(cur->ren.name, tmp);
                strcpy(tmp, p->ren.gender);
                strcpy(p->ren.gender, cur->ren.gender);
                strcpy(cur->ren.gender, tmp);
                k = p->ren.age;
                p->ren.age = cur->ren.age;
                cur->ren.age = k;
                strcpy(tmp, p->ren.phone);
                strcpy(p->ren.phone, cur->ren.phone);
                strcpy(cur->ren.phone, tmp);
                strcpy(tmp, p->ren.adress);
                strcpy(p->ren.adress, cur->ren.adress);
                strcpy(cur->ren.adress, tmp);

            }
            p = p->next;
        }

        cur = cur->next; 
    }
}
void revise(struct txl **head)
{
     struct txl *cur = *head;
     char name[32];
     assert(*head != NULL);
     printf("请输入想修改信息的人的名字:");
     scanf("%s",name);
     while(cur != NULL )
     {
         if(strcmp(cur->ren.name,name))
         {
            printf("请输入新联系人的名字:");  
            scanf("%s", cur->ren.name);            
            printf("请输入新联系人的性别:");
            scanf("%s", cur->ren.gender);
            printf("请输入新联系人的年龄:");  
            scanf("%d", &cur->ren.age);           
            printf("请输入新联系人的电话:");  
            scanf("%s", cur->ren.phone);  
            printf("请输入新联系人的地址:");  
            scanf("%s", cur->ren.adress);  
            break;  
         }
         cur = cur->next;
     }
    if(cur == NULL)
        printf("查无此人\n");
}

void save(struct txl *head)
{
    struct txl *cur = head;
    FILE *fp = fopen("txl.txt", "w");
    if ( fp == NULL ) {
        printf("该文件无法打开\n");
        exit(1);
    }   

    while ( cur != NULL ) {
        fwrite(&cur->ren, sizeof(cur->ren), 1, fp);
        cur = cur->next;
    }

    fclose(fp);
}

void load(struct txl ** head)
{
    struct person peo;
    // 1. 打开文件
    FILE *fp = fopen("txl.txt", "r");
    if ( fp == NULL )
    {
          printf("该文件无法打开\n");
          exit(1);
    }   
    // 2. 读取文件,装入链表

    while ( 1 )
    {
        struct txl *p = malloc(sizeof(struct txl));
        if ( fread(&peo, sizeof(peo), 1, fp) != 1 )
            break;
        p->ren = peo;//把读出来的数据赋值给链表
        p->next = NULL;

        p->next = *head;
        *head = p;
    }

    print(*head);
    // 3. 关闭文件
    fclose(fp); 
}

void empty(struct txl **head)
{
    struct txl *cur = *head;

    save(*head);// 1. 保存数据到文件 
    // 2. 销毁链表  
    assert(*head != NULL);
    while ( cur != NULL ) {
        struct txl *nxt = cur->next;
        free(cur);
        cur = nxt;
    }
     *head = NULL;

}

int main()
{
   int i = 0;
   struct txl *head = NULL;
   do{
       i = menu();
       switch( i ){

           case 1:
               insert(&head);
               break;
           case 2:
               Drop(&head);
               break;
           case 3:
               find(head); 
               break;
           case 4:
               revise(&head);
               break;
           case 5:
               print(head);
               break;
           case 6:
               empty(&head);
               break;
           case 7: 
               sort(head);
               break;
           case 8: 
               save(head);
               break;
           case 9: 
               load(&head);
               break;
           case 0:
               save(head);
               printf("退出\n");
               break;
           default:
               printf("输入有误\n");
               break;

       }  

   }while(i);

}

猜你喜欢

转载自blog.csdn.net/dream8834/article/details/80621059