美文网首页c/c++C语言基础
C语言基础---词典核心代码

C语言基础---词典核心代码

作者: ZebraWei | 来源:发表于2018-07-23 19:28 被阅读4次

版权声明:本文为小斑马伟原创文章,转载请注明出处!
主要步骤:

  • 创建结构体存储单词和翻译
  • 读取单词 格式化存储对应的堆空间中
  • 单词查找
  • 销毁堆空间

头部文件

  //单词和翻译的结构体
  #pragma once
  #include<stdlib.h>

  //单词和翻译的结构体
  typedef struct DICT
  {
      char* word;
      char* trans;
  }dict;

  //索引的结构体
  typedef struct POSTION
  {
      int start;
      int end;
  }pos;

  dict* list = NULL;
  pos* index = NULL;

  //函数声明
  //获取单词库中的单词和翻译
  int GetWord();
  //查找单词
  int SearchWord(char* word,char* trans,int idx);
  //销毁数据
  void DestorySpace();

二、核心代码

int GetWord() {
FILE* fp = fopen("D:/d.txt", "r");
if (!fp) {
    printf("加载单词库失败\n");
    return -1;
}

list = (dict*)malloc(sizeof(dict)* SIZE);
index = (pos*)malloc(sizeof(pos)* 27);
char flag = 'a'; //记录当前索引标志位
index[0].start = 0; //记录a的索引
index[0].end = 0;
int idx = 0; //字母对应的索引
int i = 0; //数组小标
char* temp = (char*)malloc(sizeof(char)* 1024);
while (!feof(fp)) {
    memset(temp, 0, 1024);
    fgets(temp, 1024, fp);
    //开辟单词的堆空间 #a\n

    //去掉\n
    temp[strlen(temp) - 1] = 0;
    list[i].word = (char*)malloc(sizeof(char)*strlen(temp));
    //将单词放在指定堆空间中
    strcpy(list[i].word, &temp[1]);

    //创建索引
    //0-25 index[0].start index[0].end b
    if (idx != 26) {
        if (list[i].word[0] == flag) { //首字母是否为a
            index[idx].end++;
        }
        else {
            idx++;
            index[idx].start = index[idx - 1].end;
            index[idx].end = index[idx - 1].end + 1;
            flag++;
        }
    }
    
    memset(temp, 0, 1024);
    fgets(temp, 1024, fp); //Trans:srt - :字母A\n

    //去掉\n
    temp[strlen(temp) - 1] = 0;
    list[i].trans = (char*)malloc(sizeof(char)*strlen(temp) -5);
    strcpy(list[i].trans, &temp[6]);

    i++;
}

//释放和关闭文件
free(temp);
fclose(fp);

//记录中文的索引
index[26].start = index[26].end;
index[26].end = SIZE;
for (int i = 0; i < 27; i++) {
    printf("%c的起始位置: %d\n", 'a' + i, index[i].start);
    printf("%c的结束位置: %d\n", 'a' + i, index[i].end);
}

for (int i = 0; i < SIZE; i++) {
    printf("%s\n", list[i].word);
    printf("%s\n", list[i].trans);
}
return i;
}

三、单词查找

int SearchWord(char* word, char * trans,int idx) {
if (!word || !trans) {
    printf("输出发生异常\n"); //exit(-1)
    return -1;
}

for (int i = index[idx].start; i < index[idx].end; i++) {
    //如果用户输入的单词和词库中相同返回单词对应的翻译
    if (!strcmp(word, list[i].word)) {
        strcpy(trans, list[i].trans);
        return 0;
    }
}
return 1;
}

四、释放内存

void DestorySpace()
{   
if (list == NULL)
    return;
if (!index)
{
    free(index);
    index = NULL;
}

for (int i = 0; i < SIZE; i++) {
    free(list[i].word);
    free(list[i].trans);
}
free(list);
list = NULL;
}

相关文章

  • C语言基础---词典核心代码

    版权声明:本文为小斑马伟原创文章,转载请注明出处!主要步骤: 创建结构体存储单词和翻译 读取单词 格式化存储对应的...

  • 简述OC这门语言

    OC语言在c语言的基础上,增加了一层最小的面向对象语法,完全兼容C语言,在OC代码中,可以混用c,甚至是c++代码...

  • 01-简单的OC程序

    一、OC简介 C语言的基础上,增加了一层最小的面向对象语法 完全兼容C语言 可以在OC代码中混入C语言代码,甚至是...

  • 运算与对象操作(一):数据类型和常量

    前言:Objective-C是在C语言基础上拓展出的新语言 ,所以它是完全兼容C语言的代码的,C语言中的基本数据类...

  • IOS基础学习之C(一)

    iOS开发的核心语言是Objective-C,Objective-C是在C语言的基础加了一层面向对象的语法。为了能...

  • c语言概述

    iOS开发的核心语言是Objective-C,Objective-C是在c语言的基础加了一层面向对象的语法。为了能...

  • NDK开发——C语言初识

    前言 c是面向过程的语言,也是NDK开发的基础,本文不再赘述C的基础语法知识,直接通过实际代码来感受一下C语言与j...

  • Python源码剖析笔记0——C语言基础回顾

    要分析python源码,C语言的基础不能少,特别是指针和结构体等知识。这篇文章先回顾C语言基础,方便后续代码的阅读...

  • 编程功底之禅

    程序员要掌握的核心基础 编程之路的一些书籍资料推荐 c语言 国嵌c语言深度剖析(视频)C语言深度解剖(第2版):解...

  • C语言总结

    C语言小结,适用于有其他编程语言基础的开发者 ● 所有的 C 语言程序都需要包含 main() 函数。 代码从 m...

网友评论

    本文标题:C语言基础---词典核心代码

    本文链接:https://www.haomeiwen.com/subject/hjzjmftx.html