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
    评论
校园悬赏任务平台对字典管理、论坛管理、任务资讯任务资讯公告管理、接取用户管理、任务管理、任务咨询管理、任务收藏管理、任务评价管理、任务订单管理、发布用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行校园悬赏任务平台程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。校园悬赏任务平台的开发让用户查看任务信息变得容易,让管理员高效管理任务信息。 校园悬赏任务平台具有管理员角色,用户角色,这几个操作权限。 校园悬赏任务平台针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理任务信息,管理任务资讯公告信息等内容。 校园悬赏任务平台针对用户设置的功能有:查看并修改个人信息,查看任务信息,查看任务资讯公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看任务,删除任务操作,新增任务操作,修改任务操作。任务资讯公告信息管理页面提供的功能操作有:新增任务资讯公告,修改任务资讯公告,删除任务资讯公告操作。任务资讯公告类型管理页面显示所有任务资讯公告类型,在此页面既可以让管理员添加新的任务资讯公告信息类型,也能对已有的任务资讯公告类型信息执行编辑更新,失效的任务资讯公告类型信息也能让管理员快速删除。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值