zoukankan      html  css  js  c++  java
  • 大文本数据排序

    大文本数据排序及二分法

    #define  _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    
    char **g_pp;//保存指针数组
    #define N 28
    struct index
    {
        int *pindex;
        int length;
    }allindex;//索引
    
    
    
    int getN()
    {
        FILE *pf = fopen("file.txt", "r");
        if (pf==NULL)
        {
            return -1;
        } 
        else
        {
            int i = 0;
            while (!feof(pf))
            {
                char str[50] = { 0 };
                fgets(str, 50, pf);//读取
                i++;
            }
            fclose(pf);
            return i;
    
        }
    }
    void eatN(char *str)
    {
        while (*str!='')
        {
            if (*str=='
    ' || *str=='
    ')
            {
                *str = '';
            }
    
            str++;
        }
    
    }
    
    void initmem()
    {
        g_pp = calloc(N, sizeof(char*));//分配指针数组
        FILE *pf = fopen("file.txt", "r");
        if (pf == NULL)
        {
            return -1;
        }
        else
        {
            for (int i = 0; i < N; i++)
            {
                char str[50] = { 0 };
                fgets(str, 50, pf);//读取
                g_pp[i] = calloc(strlen(str) + 1, sizeof(char));//分配
                
                sprintf(g_pp[i], str);//打印进去
                eatN(g_pp[i]);
                printf("%s", g_pp[i]);//显示测试
    
    
            }
    
            
            fclose(pf);
        
    
        }
    
    
    
    
    
    
    }
    int com(void *p1, void*p2)
    {
        char **pp1 = p1;
        char **pp2 = p2;
    
        return strcmp(*pp1, *pp2);
    
    }
    void sort()
    {
        qsort(g_pp, N, sizeof(char*), com);
    
    
    }
    void show()
    {
        printf("
    此时状态
    ");
        for (int i = 0; i < N;i++)
        {
            printf("
    %s", g_pp[i]);
        }
    }
    void writetofile()
    {
        FILE *pf = fopen("filesort.txt", "w");
        for (int i = 0; i < N;i++)
        {
            char temp[100] = { 0 };
            sprintf(temp, "%s
    ", g_pp[i]);
            fputs(temp, pf);
        }
    
        fclose(pf);
    }
    
    
    void init(char *path)
    {
        printf("
    索引数组开始分配");
        allindex.length = N;
        allindex.pindex = calloc(N, sizeof(int));//分配内存
        printf("
    索引数组完成分配");
    
        printf("
    开始读取");
        FILE *pf = fopen("filesort.txt", "rb");//
    ->
    
        if (pf == NULL)
        {
            return -1;
        }
        else
        {
            int alllength = 0;
            for (int i = 0; i < N; i++)
            {
                char str[50] = { 0 };
                fgets(str, 50, pf);
                allindex.pindex[i] = alllength;//错位从0开始
    
                int length = strlen(str);
                alllength += length;
    
            }
    
    
    
            fclose(pf);
        }
        printf("
    结束读取");
    
        printf("
    开始写入");
        FILE *pfw = fopen("index.txt", "wb");//写入索引
        fwrite(allindex.pindex, sizeof(int), allindex.length, pfw);
        fclose(pfw);//关闭
        printf("
    结束写入");
    
    
        free(allindex.pindex);
    
    
        
    
    
    
    
    
    }
    void qucik()
    {
        printf("
    索引数组开始分配");
        allindex.length = N;
        allindex.pindex = calloc(N, sizeof(int));//分配内存
        printf("
    索引数组完成分配");
    
        printf("
    开始读取");
        FILE *pfw = fopen("index.txt", "rb");//写入索引
        fread(allindex.pindex, sizeof(int), allindex.length, pfw);
        fclose(pfw);//关闭
        printf("
    结束读取");
    }
    
    
    
    void main内存索引()
    {
        //int num = 0;
        //scanf("%d", &num);
        //printf("%d", getN());
        /*initmem();
        sort();
        show();
        writetofile();*/
        //init("filesort.txt");
        qucik();
        FILE *pf = fopen("filesort.txt", "rb");
        while (1)
        {
            printf("
    请输入要读取的行数");
            int num = 0;
            scanf("%d", &num);
    
            fseek(pf, allindex.pindex[num], SEEK_SET);
            char str[128] = { 0 };
            fgets(str, 128, pf);//读取
            printf("
    %s", str);
    
    
        }
        fclose(pf);
    
    
    
        system("pause");
    }
    
    void main索引文件查找()
    {
    
    
        FILE *pf1 = fopen("index.txt", "rb");
        FILE *pf2 = fopen("filesort.txt", "rb");
        while (1)
        {
            printf("
    请输入要读取的行数");
            int num = 0;
            scanf("%d", &num);
    
            int indexnum = 0;
            fseek(pf1, num*sizeof(int), SEEK_SET);
            fread(&indexnum, sizeof(int), 1, pf1);//读索引到indexnum
    
            fseek(pf2, indexnum, SEEK_SET);
            char str[128] = { 0 };
            fgets(str, 128, pf2);//读取
            printf("
    %s", str);
    
        }
        fclose(pf1);
        fclose(pf2);
    
    
    
    
    
    
    
    
        system("pause");
    }
    void eatg(char *str)
    {
        while (*str!='')
        {
    
            if (*str=='-')
            {
                *str = '';
            }
            str++;
        }
    
    }
    
    
    void binsearch(char *searchstr)
    {
        int tou = 0;
        int wei = N - 1;
        int flag = 0;
        while (tou <=wei)
        {
            int zhong = (tou + wei) / 2;
            char zhongstr[256] = { 0 };
            {
                FILE *pf1 = fopen("index.txt", "rb");
                FILE *pf2 = fopen("filesort.txt", "rb");
    
    
                int indexnum = 0;
                fseek(pf1, zhong*sizeof(int), SEEK_SET);
                fread(&indexnum, sizeof(int), 1, pf1);//读索引zhong到indexnum
    
                fseek(pf2, indexnum, SEEK_SET);
                fgets(zhongstr, 128, pf2);//读取
        
                fclose(pf1);
                fclose(pf2);
            }
            eatN(zhongstr);
            char pnewzhongstr[256] = { 0 };
            sprintf(pnewzhongstr, zhongstr);
            eatg(pnewzhongstr);//遇到-终止
            int res = strcmp(pnewzhongstr, searchstr);//1 0  -1
            if (res==0)
            {
                flag = 1;
                printf("%s", zhongstr);
                break;
            } 
            else if (res==1)
            {
                wei = zhong - 1;
            } 
            else
            {
                tou = zhong + 1;
            }
    
            
        }
    
    
        if (flag)
        {
            printf("
    find");
        } 
        else
        {
            printf("
     not find");
        }
    
    
    }
    
    
    void main()
    {
    
    
        char str[256] = { 0 };
        scanf("%s", str);
        binsearch(str);
    
    
    
        system("pause");
    }
  • 相关阅读:
    常见的排序算法(四):归并排序
    常见的排序算法(三):快速排序
    常见的排序算法(二):选择排序
    a标签没有闭合引起自动插入很多a标签的问题
    js笔记
    Vue笔记
    CSS笔记
    bootstrap--网格化布局
    backgroud图片充满元素的方法
    bootstrap--概述
  • 原文地址:https://www.cnblogs.com/sjxbg/p/5882008.html
Copyright © 2011-2022 走看看