基于B+树的学生信息管理系统

146 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

知识准备

b+树线程代码很少,主要用于数据库底层管理,如下两份资请自行阅读。

www.bilibili.com/video/BV1n7…
blog.csdn.net/xiaohusaier…

需求

在这里插入图片描述

重点代码

B+树相关代码出自另一篇博客,引用如上。
如下是本人写的,readdata执行数据的录入和导出。

//readdata.h
#ifndef READDATA_H_INCLUDED
#define READDATA_H_INCLUDED
#include <stdio.h>
#include "bplusTree.h"
struct student{
    int id,grade;
    char gender[10];
    char name[30];
};
extern struct student st[5000000];
BPlusTree get_info(BPlusTree T);
void write_data(BPlusTree T);
#endif // READDATA_H_INCLUDED
//readdata.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "readdata.h"
#include "bplusTree.h"

#define FILENAME "info.csv"
#define OUTFILENAME "out.txt"
struct student st[5000000];
BPlusTree  get_info(BPlusTree T){
    FILE *fp = NULL;
	char *line,*record;
	char buffer[1024];
	if ((fp = fopen(FILENAME, "at+")) != NULL)
	{
		int j,id;
		fgets(buffer, sizeof(buffer), fp);
		while ((line = fgets(buffer, sizeof(buffer), fp))!=NULL)//当没有读取到文件末尾时循环继续
		{
			record = strtok(line, ",");
			j=0;
			while (record != NULL)//读取每一行的数据
			{
			    switch(j)
			    {
                case 0:
                    id=atoi(record);
                    T = Insert(T, id);
                    st[id].id=id;
                    break;

                case 1:
                    strcpy(st[id].gender,record);
                    break;

                case 2:
                    strcpy(st[id].name,record);
                    break;

                case 3:
                    st[id].grade=atoi(record);
                    break;
			    }
				//printf("%s ", record);//将读取到的每一个数据打印出来
				record = strtok(NULL, ",");
				j++;
			}
			printf("\n");

		}
		fclose(fp);
		fp = NULL;
	}
    return T;
}
void write_data(BPlusTree T){
    FILE* fp1 = fopen(OUTFILENAME , "w");
    Position Tmp=FindFirst(T);
    int id,i;
    /* 第一片树叶 */
    while (Tmp != NULL){
        i = 0;
        while (i < Tmp->KeyNum){
            id=Tmp->Key[i++];
            fprintf(fp1, "%d,%s,%s,%d\n",\
                     st[id].id,st[id].name,st[id].gender,st[id].grade);

        }
        Tmp = Tmp->Next;
    }
    fclose(fp1);//关闭文件
    printf("Data saved.\n");
}

查询该节点是否存在,模拟磁盘的思路,磁盘中的文件如果删除,知识索引删除,文件实体暂时不会更新。

extern Position FindFirst(BPlusTree T){
    Position Tmp;
    if (T == NULL)
        return T;
    Tmp = T;
    while (Tmp->Children[0] != NULL)
        Tmp = Tmp->Children[0];
    return Tmp;
}
extern int IsExist(BPlusTree T,KeyType Key){
    int  j = 0,i;
    while(j<T->KeyNum){
        //printf("%d\r\n",T->Key[j]);
        if(j==0 && Key<T->Key[j]){
            return -1;
        }else if(Key == T->Key[j]){
            return 1;
        }else if((j<T->KeyNum-1 && Key>=T->Key[j] && Key<T->Key[j+1])\
                 ||(j==T->KeyNum-1 && Key>=T->Key[j])){
            if(T->Children[j]==NULL) {
                   // printf("%d\r\n",T->Key[j]);
                for(i=0;i<T->KeyNum;i++){
                    if(T->Key[i]==Key) return 1;
                }
                return -1;
            }
            else return IsExist(T->Children[j],Key);
        }
        j++;
    }
    return 0;
}

完整工程

download.csdn.net/download/re…

效果

数据导入
在这里插入图片描述

查找
在这里插入图片描述
删除后 查无此人
在这里插入图片描述
在这里插入图片描述