索引

来源:互联网 发布:three.js空调贴图 编辑:程序博客网 时间:2024/05/29 04:42

一,查询

/*************************************************************************    > File Name:    > Author: songli    > QQ:2734030745    > Mail: 15850774503@163.com    > Created Time: ************************************************************************/#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <string.h>#include <stdlib.h>#define MAX  111104//索引struct INDEX{    int start;    int end;}index[27];typedef struct DICT{    char *word; //单词    char *trans; //单词解释} DICT;DICT * dict = NULL;int GetFile(char *filename, char *wd){    FILE *in = fopen(filename, wd);    if (!in)    {        printf("can not open file!\n");        return -1;    }    //结构体的堆空间    dict = (DICT *)malloc(sizeof(DICT) * MAX);    //开辟临时空间    char *buf = (char *)malloc(sizeof(char) * 1024);     int i = 0;    //排序    char ch = 'a';    int sum = 0;    //下标    int wodsindex = 0;    index[wodsindex].start = 0;    while (!feof(in))    {        memset(buf, 0, sizeof(char) * 1024);        //读取一行        fgets(buf, sizeof(char) * 1024, in);        int len = strlen(buf);        //开辟结构体内的空间   //释放内存的是根据开辟内存释放的        dict[i].word = (char *)malloc(sizeof(char) *len - 1);        memset(dict[i].word, 0, sizeof(char) *len - 2);        //buf的最后的一个字符置成'\0'        buf[strlen(buf) - 1] = '\0';        //拷贝到word内存中        strcpy(dict[i].word, &buf[1]);        //printf("%s\n", dict[i].word);        if (wodsindex == 26)        {            sum++;            index[26].end = sum;        }        else        {            if (buf[1] == ch)            {                sum++;            }            else            {                ch++;                //设置结束位置                index[wodsindex].end = sum - 1;                index[wodsindex + 1].start = sum;                wodsindex++;                sum++;            }        }        //读取翻译        memset(buf, 0, sizeof(char) * 1024);        fgets(buf, sizeof(char) * 1024, in);        len = strlen(buf);        //开辟结构体内的空间        dict[i].trans = (char *)malloc(sizeof(char) * len - 6);        memset(dict[i].trans, 0, sizeof(char) *len - 6);        //buf的最后的一个字符置成'\0'        buf[strlen(buf) - 1] = '\0';        //拷贝到word内存中        strcpy(dict[i].trans, &buf[6]);        //printf("%s\n", dict[i].trans);        i++;    }    for (i = 0; i < 26; i++)    {        printf("start = %d\n", index[i].start);        printf("end = %d\n", index[i].end);    }    free(buf);    fclose(in);    printf("加载完成。。。\n");    return 0;}int SearchWord(char *word, char *trans, int dex){    int i;    for (i = index[dex].start; i <= index[dex].end; i++)    {        if (!strcmp(word, dict[i].word))        {            strcpy(trans, dict[i].trans);            return 0;        }    }    return -1;}void DeshitySpase(){    int i;    for (i = 0; i < MAX; i++)    {        if (dict[i].word != NULL)        {            free(dict[i].word);            dict[i].word = NULL;        }        if (dict[i].trans != NULL)        {            free(dict[i].trans);            dict[i].trans = NULL;        }    }    if (dict != NULL)    {        free(dict);        dict = NULL;    }}int main(int argc, char *argv[]){    /*测试代码*/    /*FILE *fp = fopen("D:/FILE/dict.txt", "r");    int i = 0;    char * buf = (char *)malloc(sizeof(char) * 1024);    while (!feof(fp))    {        fgets(buf, sizeof(char) * 1024, fp);        i++;    }    printf("%d\n", i / 2);*/    char *filename = "D:/FILE/dict.txt";    char *wd = "r";    GetFile(filename, wd);    char word[100], trans[100];    while (1)    {        scanf("%s", &word);        if (!strcmp(word, "comm==exit"))        {            break;        }        int len;        //判断首字母的范围        if (word[0] < 'z' && word > 'a')        {             len = SearchWord(word, trans, word[0] - 'a');        }        else        {            len = SearchWord(word, trans, word[0] - 26);        }        if (!len)        {            printf("%s\n", trans);        }        else        {            printf("没有找到了....\n");        }    }    DeshitySpase();    system("pause");    return EXIT_SUCCESS;}
原创粉丝点击