1. 程式人生 > >c專案實現(1)實現電子詞典的翻譯

c專案實現(1)實現電子詞典的翻譯

專案實現功能

通過使用者的輸入,在字典檔案中進行 查詢,返回對應的翻譯內容。


字典檔案的樣式,該檔案已經上傳。

#a                                  --------要查詢的單詞
Trans:art. 一;字母A        --------註釋
#a.m.
Trans:n. 上午


專案實現的思路

1、首先讀取字典檔案

2、讀取使用者的輸入

3、查詢輸入匹配的解釋,返回結果

4、控制程式的結束和記憶體的釋放


專案的測試

需要通過gcc先編譯檔案,再在cmd中執行exe檔案實現


專案程式碼

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define MAX 111111  //設定最大的記錄數

struct dict{
    //將字典檔案中的內容儲存到結構體中
    char *key; //詞條
    char *content; //對應的翻譯
};

//實現讀取字典檔案的功能,對於字典的檔名是不變的,所以可以用const保證其不會改變
int open_dict(struct dict **p, const char *dict_filename){
    //開啟檔案
    FILE *pfile = fopen(dict_filename, "r");
    if(pfile == NULL){
        //如果開啟檔案失敗
        return 0;
    }

    //固定分配MAX大小的記憶體
    *p = (struct dict *)malloc(sizeof (struct dict) * MAX);
    //將記憶體初始化為0
    memset(*p, 0, sizeof (struct dict) * MAX);
    //pD指向陣列p的首地址
    struct dict *pD = *p;


    //設定字元的暫存區
    char buf[1024] = {0};
    size_t len = 0;
    int i = 0;

    //迴圈實現讀取檔案的每一行
    while(!feof(pfile)){
        //對暫存區的記憶體清0
        memset(buf, 0, sizeof (buf));
        //讀取檔案的每一行
        fgets(buf, sizeof (buf), pfile);
        //得到讀取的字串的長度
        len = strlen(buf);

        //根據字串的長度分配記憶體
        if(len > 0){
            //首先讀取詞條
            pD[i].key = (char *)malloc(len);
            memset(pD[i].key, 0, len);
            //將讀取到的記憶體拷貝到key中,這裡從1開始讀取是因為詞條前面有個#,需要去掉
            strcpy(pD[i].key, &buf[1]);
        }

        //再讀取字典中的解釋
        memset(buf, 0, sizeof (struct dict));
        //讀取檔案的每一行
        fgets(buf, sizeof (buf), pfile);
        //得到讀取的字串的長度
        len = strlen(buf);

        //根據字串的長度分配記憶體
        if(len > 0){
            //首先讀取詞條
            pD[i].content = (char *)malloc(len);
            memset(pD[i].content, 0, len);
            //將讀取到的記憶體拷貝到content中,這裡從6讀取是因為在解釋前面有Trans:,需要去掉
            strcpy(pD[i].content, &buf[6]);
        }

        //每次讀取兩行,儲存到到結構體中,之後再迴圈。
        i++;
    }

    //關閉字典檔案
    fclose(pfile);

    //返回讀取了一共多少個詞條組
    return i;
}

//實現對字典檔案的檢索
int search_dict(const struct dict *p, int size, const char *key, char *content){
    //利用順序查詢遍歷字典
    for(int i = 0; i < size; i++){
        if((p[i].key == NULL) || (p[i].content == NULL)){
            //字典中有的部分為空
            continue;
        }

        if(strncmp(p[i].key, key, strlen(key)) == 0){
            strcpy(content, p[i].content);
            //查詢成功
            return 1;
        }
    }

    //沒有查詢到詞條
    return 0;
}

//實現釋放記憶體的功能
void free_dict(struct dict *p, int size){
    //迴圈釋放key和content的記憶體
    for(int i = 0; i < size; i++){
        if(p[i].key){
            free(p[i].key);
        }

        if(p[i].content){
            free(p[i].content);
        }
    }

    //釋放p記憶體
    free(p);
}



//需要通過該主程式來讀取使用者的引數
int main(int argc, char *args[]){
    if(argc < 2){
       //提示使用者輸入的引數不對
       printf("usage: %s dict-filename\n", args[0]);
       return 0;
    }

    //記錄函式開始的時間
    long start_ms = 0;
    //記錄函式結束的時間
    long end_ms = 0;
    //設定字典檔案儲存空間
    struct dict *p = NULL;
    //讀取字典檔案開始
    start_ms = clock();

    //1、首先讀取字典檔案,根據使用者輸入的第一個引數讀取
    int size = open_dict(&p, args[1]);
    if(size == 0){
        //字典檔案開啟失敗,程式退出
        return 0;
    }
    //讀取字典檔案結束
    end_ms = clock();
    //列印一共花了多少時間
    printf("open_dict use %ld ms\n", end_ms - start_ms);


    //2、讀取使用者的輸入
    char key[1024];
    char content[1024];
    while (1) {
        //對key和content的空間清0
        memset(key, 0, sizeof (key));
        memset(content, 0, sizeof (content));

        //得到使用者的輸入
        scanf("%s", key);
        //判斷使用者的輸入
        if(strncmp(key, "command=exit", 12) == 0){
            break;
        }
        //開始查詢
        start_ms = clock();
        //3、查詢輸入匹配的解釋,返回結果
        if(search_dict(p, size, key, content)){
            printf("%s", content);
        }else{
            printf("not find\n");
        }

        end_ms = clock();
        printf("search_dict use %ld ms\n", end_ms - start_ms);
    }


    //4、記憶體的釋放
    start_ms = clock();
    free_dict(p, size);
    end_ms = clock();
    printf("free_dict use %ld ms\n", end_ms - start_ms);

    return 0;
}