zoukankan      html  css  js  c++  java
  • 6.C语言文件操作之英语电子字典的实现,dos版

    多的不说,直接上代码:

    里面涉及的字典文件在这:这是传送门,下载下来以后把该文件放在工程目录下即可

      1 #define _CRT_SECURE_NO_WARNINGS
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <time.h>
      7 
      8 #define MAX 111111 //最大记录数
      9 
     10 struct dict
     11 {
     12     char *key;
     13     char *content;
     14 };
     15 
     16 //去掉字符串结尾的回车,空格字符
     17 void format_string(char *str)
     18 {
     19     size_t i;
     20     for (i = strlen(str) - 1; i >= 0; i--)
     21     {
     22         if (str[i] != '
    ' && str[i] != ' ' &&str[i] != '
    ')
     23         {
     24             str[i + 1] = '';
     25             break;
     26         }
     27     }
     28 }
     29 
     30 //打开字典文件,并读取文件内容
     31 int open_dict(struct dict **p, const *dict_filenaem)
     32 {
     33     FILE *pfile = fopen(dict_filenaem, "r");
     34     if (pfile == NULL)
     35         return 0;//文件打开失败,函数返回
     36 
     37     *p = (struct dict *)malloc(sizeof(struct dict) * MAX);//固定分配MAX大小内存
     38     memset(*p, 0, sizeof(struct dict)*MAX);//将分配内存初始化为0
     39 
     40     char buf[1024] = { 0 };
     41     size_t len = 0;
     42     int i = 0;//计数器
     43     while (!feof(pfile))//循环读取文件,直到文件末尾
     44     {
     45         memset(buf, 0, sizeof(buf));
     46         fgets(buf, sizeof(buf), pfile);//读取文件一行
     47         len = strlen(buf);//得到读取到字符串长度
     48         if (len > 0)
     49         {
     50             (*p)[i].key = (char *)malloc(len);//根据字符串长度分配内存
     51             memset((*p)[i].key, 0, len);
     52             format_string(buf);//去掉字符串结尾的空格和回车
     53             strcpy((*p)[i].key, &buf[1]);//将读取的内容拷贝到key中
     54         }
     55 
     56         memset(buf, 0, sizeof(buf));
     57         fgets(buf, sizeof(buf), pfile);
     58         len = strlen(buf);
     59         if (len > 0)
     60         {
     61             (*p)[i].content = (char *)malloc(len);
     62             memset((*p)[i].content, 0, len);
     63             strcpy((*p)[i].content, &buf[6]);
     64         }
     65 
     66         i++;
     67     }
     68 
     69     fclose(pfile);//关闭字典文件
     70 
     71     return i;//返回读取到的字典词条数
     72 
     73 }
     74 
     75 //根据关键字key,在字典中查找内容
     76 int search_dict(const struct dict *p, int size, const char *key, char *content)
     77 {
     78     int i = 0;
     79     for (i = 0; i < size; i++)//遍历字典
     80     {
     81         if ((p[i].key == NULL) || (p[i].content) == NULL)
     82             continue;
     83 
     84         if (strcmp(p[i].key, key) == 0)
     85         {
     86             strcpy(content, p[i].content);
     87             return 1;//找到符合条件记录,返回1
     88         }
     89     }
     90 
     91     return 0;//没有找到符合条件记录,返回0
     92 }
     93 
     94 //释放内存
     95 void free_dict(struct dict *p, int size)
     96 {
     97     int i = 0;
     98     for (i = 0; i < size; i++)
     99     {
    100         if (p[i].key)
    101         {
    102             free(p[i].key);
    103         }
    104         if (p[i].content)
    105         {
    106             free(p[i].content);
    107         }
    108     }
    109     free(p);//释放p内存
    110 }
    111 
    112 int main()
    113 {
    114 
    115     struct dict *p = NULL;
    116     
    117     int dict_size = open_dict(&p, "dict.txt");
    118 
    119     char key[1024];
    120     char content[1024];
    121     while (1)
    122     {
    123         memset(key, 0, sizeof(key));
    124         memset(content, 0, sizeof(content));
    125         fgets(key, sizeof(key),stdin);//从键盘得到用户输入
    126         format_string(key);
    127         if (strncmp(key, "command=exit", 12) == 0)
    128             break;
    129 
    130         if (search_dict(p, dict_size, key, content))//根据用户输入在字典中检索
    131         {
    132             printf("%s", content);
    133         }
    134         else
    135         {
    136             printf("not found
    ");
    137         }
    138     }
    139     system("pause");
    140     return 0;
    141 }
    View Code

     下面是移植到QT上的界面程序:传送门,移植很简单,但是要注意QT采用 的是UTF8编码,而windows的记事本是采用GBK编码,所以需要进行编码转化

    • 步骤如下
    • 1.添加头文件<QTextCodec>
    • 2.建立一个从UTF8到GBK转化的桥梁:QTextCodec *codec = QTextCodec::codecForName("GBK");
    • 3.转化成GBK编码,然后copy到char数值中进行操作 strcpy(key,codec->fromUnicode(ui->lineEdit->text()));

    如果要把GBK编码转化成utf8编码,则需要一个操作即可codec->toUnicode(content),上代码

     1 void Widget::on_pushButton_clicked()
     2 {
     3     QTextCodec *codec = QTextCodec::codecForName("GBK");//建立一个从UTF8到GBK转化的桥梁
     4     char key[1024] = {0};
     5     char content[1024] = {0};
     6     //ui->lineEdit->text();//得到用户在edit中输入的内容,类型是QString,QString是QT提供的一个字符串类
     7                          //qt默认的字符串都是UTF8编码
     8     strcpy(key,codec->fromUnicode(ui->lineEdit->text()));
     9     format_string(key);
    10 
    11     if (search_dict(p, dict_size, key, content))//根据用户输入在字典中检索
    12     {
    13        //content内容是GBK格式的字符串
    14         ui->label->setText(codec->toUnicode(content));//把GBK编码转化成UTF8格式
    15     }
    16     else
    17     {
    18         ui->label->setText("not found");
    19     }
    20 }
    View Code
  • 相关阅读:
    PCA (主成分分析)详解 (写给初学者) 结合matlab(转载)
    解释一下核主成分分析(Kernel Principal Component Analysis, KPCA)的公式推导过程(转载)
    主成分分析(PCA)原理详解(转载)
    神经网络的6种有监督训练算法
    PyCharm下载与激活
    matlab中元胞数组的创建与内容读取
    创新式开发探索(四) —— 探索式学习
    耐心,细心,贴心,静心
    使用 JsPlumb 绘制拓扑图的通用方法
    使用 amcharts 和 highcharts 绘制多曲线时间趋势图的通用方法
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8006954.html
Copyright © 2011-2022 走看看