索引

一,查询

/*************************************************************************
    > 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值