c语言练习6

通讯录添加文件读写功能

#include<stdio.h>
#include<stdlib.h>//exit(0),包含<malloc.h>
#include<string.h>//strcpy
#define _CRT_SECURE_NO_WARNINGS 1
void createFace();
void createHeadNode();
void addNewNode(struct node* phead);
void deleteNodeByNo(struct node* phead);
void showAllNode(struct node* phead);
void info_output(struct node* pfind);
void searchNodeByNo(struct node* phead);
void updateNodeByNo(struct node* phead);
void initData(struct node* phead);
void flush_data(struct node* phead);
struct Person {
    int no;
    char name[20];
    int age;
    char tel[15];
    char address[30];
};
struct node {
    Person data;
    struct node* pnext;
};
struct node* phead = NULL;
int count = 0;
int main()
{
    createHeadNode();
    createFace();
    return 0;
}
//从文件中读取数据到链表
void initData(struct node* phead) {
    struct node* pfind = phead;
    struct node* pnew = NULL;
    FILE* fp = NULL;//文件指针,读
    fp = fopen("data.txt", "r");
    if (fp == NULL) {
        printf("文件读取失败\n");
        return;
    }
    while (1) {
        pnew = (struct node*)malloc(sizeof(struct node));
        fscanf(fp, "%d%s%d%s%s", &pnew->data.no, pnew->data.name, &pnew->data.age, pnew->data.tel, pnew->data.address);
        pnew->pnext = NULL;
        if (feof(fp))//feof遇到文件结束,函数值为非零值,否则函数值为0
        {
            break;
        }
        count++;
        pfind->pnext = pnew;
        pfind = pfind->pnext;
    }
    fclose(fp);
}
void createFace() {
    initData(phead);
    printf("当前文件中有%d条数据\n", count);
    int a = 0;
    
    while (1) {
        printf("\t\t\t欢迎使用通讯录管理系统\n");
        printf("\t\t\t1:添加一条联系人信息\n");
        printf("\t\t\t2:删除一条联系人信息\n");
        printf("\t\t\t3:打印所有联系人信息\n");
        printf("\t\t\t4:查询一条联系人信息\n");
        printf("\t\t\t5:修改一条联系人信息\n");
        printf("\t\t\t6:读取存档并退出系统\n");
        scanf("%d", &a);
        switch (a) {
        case 1:addNewNode(phead); break;
        case 2:deleteNodeByNo(phead); break;
        case 3:showAllNode(phead); break;
        case 4:searchNodeByNo(phead); break;
        case 5:updateNodeByNo(phead); break;
        case 6:flush_data(phead);
        }
    }
    
}
//创建链表的头节点
void createHeadNode() {
    phead = (struct node*)malloc(sizeof(struct node));
    if (!phead) {
        printf("头节点分配失败\n");
        return;
    }
    else {
        phead->pnext = NULL;
    }
}
//添加一个新节点
void addNewNode(struct node* phead) {
    struct node* pfind = phead;
    struct node* pnew = NULL;
    while (pfind->pnext != NULL) {
        pfind = pfind->pnext;
    }
    pnew = (struct node*)malloc(sizeof(struct node));
    printf("请输入编号\n");
    scanf("%d", &pnew->data.no);
    printf("请输入姓名\n");
    scanf("%s", pnew->data.name);
    printf("请输入年龄\n");
    scanf("%d", &pnew->data.age);
    printf("请输入电话\n");
    scanf("%s", pnew->data.tel);
    printf("请输入地址\n");
    scanf("%s", pnew->data.address);
    
    pnew->pnext = NULL;//尾节点的指针为空
    pfind->pnext = pnew;//前一个指针的后一个指向新节点,即可连接起来
    printf("%s的信息录入成功\n", pnew->data.name);
}
//删除一个指定的节点
void deleteNodeByNo(struct node* phead) {
    int no = 0;
    struct node* ptemp = NULL;
    struct node* pfind1 = phead;//前指针
    struct node* pfind2 = phead->pnext;//后指针
    
    printf("请输入需要删除的联系人的编号\n");
    scanf("%d", &no);
    while (pfind2 != NULL) {
        if (pfind2->data.no == no) {
            break;
        }
        pfind1 = pfind1->pnext;
        pfind2 = pfind2->pnext;
    }
    if (pfind2 == NULL) {
        printf("对不起,查无此记录");
    }
    else {
        ptemp = pfind2->pnext;
        free(pfind2);
        pfind2 = NULL;
        pfind1->pnext = ptemp;
        printf("删除成功\n");
    }

}
//遍历链表,输出数据
void showAllNode(struct node * phead) {
    struct node* pfind = phead->pnext;
    while (pfind != NULL) {
        info_output(pfind);
        pfind = pfind->pnext;
    }
}
//封装函数,用于输出,目的是为了简化代码
void info_output(struct node * pfind) {
    printf("++++++++++++++++++++++++++++++++++++++++++++++\n");
    printf("编号:%d\t姓名:%s\t年龄:%d\t电话:%s\t地址:%s\n", pfind->data.no, pfind->data.name, pfind->data.age, pfind->data.tel, pfind->data.address);
    printf("++++++++++++++++++++++++++++++++++++++++++++++\n");
}
//查询一条指定节点
void searchNodeByNo(struct node* phead) {
    int no = 0;
    struct node* pfind = phead->pnext;
    //struct node* pfind = phead;
    printf("请输入你想查找联系人的编号\n");
    scanf("%d", &no);
    while (pfind != NULL) {
        if (pfind->data.no == no) {
            break;
        }
        pfind = pfind->pnext;
    }
    if (pfind == NULL) {
        printf("对不起,查无此记录\n");
    }
    else {
        info_output(pfind);
    }
}
//修改一条指定节点中的数据
void updateNodeByNo(struct node* phead) {
    int no = 0;
    struct node* pfind = phead;
    printf("请输入你想修改联系人的编号\n");
    scanf("%d", &no);
    while (pfind != NULL) {
        if (pfind->data.no == no) {
            break;
        }
        pfind = pfind->pnext;
    }
    if (pfind == NULL) {
        printf("对不起,查无此记录\n");
    }
    else {
        info_output(pfind);
        printf("请输入编号\n");
        scanf("%d", &pfind->data.no);
        printf("请输入姓名\n");
        scanf("%s", pfind->data.name);
        printf("请输入年龄\n");
        scanf("%d", &pfind->data.age);
        printf("请输入电话\n");
        scanf("%s", pfind->data.tel);
        printf("请输入地址\n");
        scanf("%s", pfind->data.address);
        printf("%s的信息修改成功\n", pfind->data.name);
        info_output(pfind);
    }
}
//将链表中的数据写入到data.txt文件中
void flush_data(struct node* phead) {
    struct node* pfind = phead->pnext;
    FILE* fp = NULL;
    fp = fopen("data.txt", "w");
    
    while (pfind != NULL) {
        fprintf(fp, "%d\t%s\t%d\t%s\t%s\n", &pfind->data.no, pfind->data.name, &pfind->data.age, pfind->data.tel, pfind->data.address);
        pfind = pfind->pnext;
    }
    fclose(fp);
    printf("系统已退出\n");
    exit(0);
}

通讯录添加模糊查询功能

#include<stdio.h>
#include<stdlib.h>//exit(0),包含<malloc.h>
#include<string.h>//strcpy
#define _CRT_SECURE_NO_WARNINGS 1
void createFace();
void createHeadNode();
void addNewNode(struct node* phead);
void deleteNodeByNo(struct node* phead);
void showAllNode(struct node* phead);
void info_output(struct node* pfind);
void searchNodeByNo(struct node* phead);
void updateNodeByNo(struct node* phead);
void initData(struct node* phead);
void flush_data(struct node* phead);
void searchNodeByDim(struct node* phead);
int isSameNode(char input[], char name[]);
struct Person {
    int no;
    char name[20];
    int age;
    char tel[15];
    char address[30];
};
struct node {
    Person data;
    struct node* pnext;
};
struct node* phead = NULL;
int count = 0;
int main()
{
    createHeadNode();
    createFace();
    return 0;
}
//从文件中读取数据到链表
void initData(struct node* phead) {
    struct node* pfind = phead;
    struct node* pnew = NULL;
    FILE* fp = NULL;//文件指针,读
    fp = fopen("data.txt", "r");
    if (fp == NULL) {
        printf("文件读取失败\n");
        return;
    }
    while (1) {
        pnew = (struct node*)malloc(sizeof(struct node));
        fscanf(fp, "%d%s%d%s%s", &pnew->data.no, pnew->data.name, &pnew->data.age, pnew->data.tel, pnew->data.address);
        pnew->pnext = NULL;
        if (feof(fp))//feof遇到文件结束,函数值为非零值,否则函数值为0
        {
            break;
        }
        count++;
        pfind->pnext = pnew;
        pfind = pfind->pnext;
    }
    fclose(fp);
}
void createFace() {
    initData(phead);
    printf("当前文件中有%d条数据\n", count);
    int a = 0;
    
    while (1) {
        printf("\t\t\t欢迎使用通讯录管理系统\n");
        printf("\t\t\t1:添加一条联系人信息\n");
        printf("\t\t\t2:删除一条联系人信息\n");
        printf("\t\t\t3:打印所有联系人信息\n");
        printf("\t\t\t4:查询一条联系人信息\n");
        printf("\t\t\t5:修改一条联系人信息\n");
        printf("\t\t\t6:模糊查询联系人信息\n");
        printf("\t\t\t7:读取存档并退出系统\n");
        scanf("%d", &a);
        switch (a) {
        case 1:addNewNode(phead); break;
        case 2:deleteNodeByNo(phead); break;
        case 3:showAllNode(phead); break;
        case 4:searchNodeByNo(phead); break;
        case 5:updateNodeByNo(phead); break;
        case 6:searchNodeByDim(phead); break;
        case 7:flush_data(phead);
        }
    }
    
}
//创建链表的头节点
void createHeadNode() {
    phead = (struct node*)malloc(sizeof(struct node));
    if (!phead) {
        printf("头节点分配失败\n");
        return;
    }
    else {
        phead->pnext = NULL;
    }
}
//添加一个新节点
void addNewNode(struct node* phead) {
    struct node* pfind = phead;
    struct node* pnew = NULL;
    while (pfind->pnext != NULL) {
        pfind = pfind->pnext;
    }
    pnew = (struct node*)malloc(sizeof(struct node));
    printf("请输入编号\n");
    scanf("%d", &pnew->data.no);
    printf("请输入姓名\n");
    scanf("%s", pnew->data.name);
    printf("请输入年龄\n");
    scanf("%d", &pnew->data.age);
    printf("请输入电话\n");
    scanf("%s", pnew->data.tel);
    printf("请输入地址\n");
    scanf("%s", pnew->data.address);
    
    pnew->pnext = NULL;//尾节点的指针为空
    pfind->pnext = pnew;//前一个指针的后一个指向新节点,即可连接起来
    printf("%s的信息录入成功\n", pnew->data.name);
}
//删除一个指定的节点
void deleteNodeByNo(struct node* phead) {
    int no = 0;
    struct node* ptemp = NULL;
    struct node* pfind1 = phead;//前指针
    struct node* pfind2 = phead->pnext;//后指针
    
    printf("请输入需要删除的联系人的编号\n");
    scanf("%d", &no);
    while (pfind2 != NULL) {
        if (pfind2->data.no == no) {
            break;
        }
        pfind1 = pfind1->pnext;
        pfind2 = pfind2->pnext;
    }
    if (pfind2 == NULL) {
        printf("对不起,查无此记录");
    }
    else {
        ptemp = pfind2->pnext;
        free(pfind2);
        pfind2 = NULL;
        pfind1->pnext = ptemp;
        printf("删除成功\n");
    }

}
//遍历链表,输出数据
void showAllNode(struct node * phead) {
    struct node* pfind = phead->pnext;
    while (pfind != NULL) {
        info_output(pfind);
        pfind = pfind->pnext;
    }
}
//封装函数,用于输出,目的是为了简化代码
void info_output(struct node * pfind) {
    printf("++++++++++++++++++++++++++++++++++++++++++++++\n");
    printf("编号:%d\t姓名:%s\t年龄:%d\t电话:%s\t地址:%s\n", pfind->data.no, pfind->data.name, pfind->data.age, pfind->data.tel, pfind->data.address);
    printf("++++++++++++++++++++++++++++++++++++++++++++++\n");
}
//查询一条指定节点
void searchNodeByNo(struct node* phead) {
    int no = 0;
    struct node* pfind = phead->pnext;
    //struct node* pfind = phead;
    printf("请输入你想查找联系人的编号\n");
    scanf("%d", &no);
    while (pfind != NULL) {
        if (pfind->data.no == no) {
            break;
        }
        pfind = pfind->pnext;
    }
    if (pfind == NULL) {
        printf("对不起,查无此记录\n");
    }
    else {
        info_output(pfind);
    }
}
//修改一条指定节点中的数据
void updateNodeByNo(struct node* phead) {
    int no = 0;
    struct node* pfind = phead;
    printf("请输入你想修改联系人的编号\n");
    scanf("%d", &no);
    while (pfind != NULL) {
        if (pfind->data.no == no) {
            break;
        }
        pfind = pfind->pnext;
    }
    if (pfind == NULL) {
        printf("对不起,查无此记录\n");
    }
    else {
        info_output(pfind);
        printf("请输入编号\n");
        scanf("%d", &pfind->data.no);
        printf("请输入姓名\n");
        scanf("%s", pfind->data.name);
        printf("请输入年龄\n");
        scanf("%d", &pfind->data.age);
        printf("请输入电话\n");
        scanf("%s", pfind->data.tel);
        printf("请输入地址\n");
        scanf("%s", pfind->data.address);
        printf("%s的信息修改成功\n", pfind->data.name);
        info_output(pfind);
    }
}
void searchNodeByDim(struct node * phead) {//dim是模糊查询的简写
    char input[20];
    struct node* pfind = phead->pnext;
    printf("请输入关键字(模糊查询)\n");
    scanf("%s", input);
    while (pfind !=NULL) {
        if (isSameNode(input, pfind->data.name)) {
            info_output(pfind);
        }
        pfind = pfind->pnext;
    }
}
//实现模糊查询算法的子函数
int isSameNode(char input[], char name[]) {
    char *p1, *p2;
    p1 = input;
    p2 = name;
    while (*p1 != '\0') {
        while (*p2 != '\0') {
            if (*p1 == *p2) {
                return 1;
            }
            p2++;
        }
        p1++;
    }
    return 0;
}
//将链表中的数据写入到data.txt文件中
void flush_data(struct node* phead) {
    struct node* pfind = phead->pnext;
    FILE* fp = NULL;
    fp = fopen("data.txt", "w");
    
    while (pfind != NULL) {
        fprintf(fp, "%d\t%s\t%d\t%s\t%s\n", &pfind->data.no, pfind->data.name, &pfind->data.age, pfind->data.tel, pfind->data.address);
        pfind = pfind->pnext;
    }
    fclose(fp);
    printf("系统已退出\n");
    exit(0);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值