天天看點

C基礎學習筆記——01-C基礎第15天(快 yi通)

在學習C基礎總結了筆記,并分享出來。有問題請及時聯系部落客:​​Alliswell_WP​​,轉載請注明出處。

01-C基礎第15天(快 yi通)

練習:查找單詞記錄單詞翻譯

程式分析

一、讀取檔案中的單詞

1、打開檔案判斷檔案可用性

2、建立構體struct dict {char * word,char* trans}結構體需要開辟堆空間

3、讀取檔案内容開辟堆空間,存放在堆空間中檔案行數/2=擷取單詞個數兩行算作一個單詞

4、關閉檔案

二、查找單詞

1、通過鍵盤擷取單詞

2、通過循環查找單詞記錄單詞翻譯

3、退出死循環

三、釋放堆空間

1、釋放單詞和翻譯

2、釋放結構體堆空間

索引:

struct index{ char flag,int start,int end}flag=a      36
for(int i=index.start;i<index.end;i++)
{
}      

dict.c代碼如下:

1 #define _CRT_SECURE_NO_WARNINGS
  2 #include "dict.h"
  3 
  4 
  5 
  6 ////全局變量
  7 //dic * p;
  8 
  9 //1、打開檔案 存儲資料
 10 void ReadFile(dic ** p)
 11 {
 12     //開辟堆空間
 13     //*p = (dic*)malloc(sizeof(dic)*WORDMAX);//  realloc()  連結清單
 14     *p = (dic*)malloc(sizeof(dic)* WORDMAX);
 15 
 16     //讀取檔案内容
 17     FILE * fp = fopen("D:\\dict.txt", "r");
 18     if (!fp)
 19         return -1;
 20     int i = 0;
 21     char buf[1024];
 22     while (!feof(fp))
 23     {
 24         memset(buf, 0, 1024);
 25         fgets(buf, 1024, fp);
 26 
 27         //格式化操作
 28         //buf[strlen(buf) - 1] = '\0';
 29         for (int i = strlen(buf); i > 0; i--)
 30         {
 31             if (buf[i] == '\n')
 32             {
 33                 buf[i] = '\0';
 34                 break;
 35             }
 36         }
 37 
 38 
 39         //開辟堆空間
 40         (*p+i)->word = (char *)malloc(strlen(buf) + 1);
 41         memset((*p + i)->word, 0, strlen(buf) + 1);
 42         strcpy((*p + i)->word, &buf[1]);//#abc\n
 43 
 44         memset(buf, 0, 1024);
 45         fgets(buf, 1024, fp);
 46         (*p + i)->trans = (char*)malloc(strlen(buf) + 1);
 47         memset((*p + i)->trans, 0, strlen(buf) + 1);
 48         strcpy((*p + i)->trans, buf);
 49         i++;
 50     }
 51 
 52 
 53     fclose(fp);
 54 }
 55 
 56 //2、檔案查找
 57 //ch 輸入單詞
 58 //content 單詞對應的翻譯
 59 int SearchWord(char * ch, char * content, dic *p)
 60 {
 61     memset(content, 0, 1024);
 62     for (int i = 0; i < WORDMAX; i++)
 63     {
 64         if (!strcmp(ch, p[i].word))
 65         {
 66             printf("%s\n", p[i].trans); 
 67             return 1;
 68         }
 69     }
 70     return 0;
 71 }
 72 
 73 //3、釋放
 74 void clear(dic * p)
 75 {
 76     for (int i = 0; i < WORDMAX; i++)
 77     {
 78         free(p[i].word);
 79         free(p[i].trans);
 80     }
 81     free(p);
 82 }
 83 
 84 int main()
 85 {
 86     dic * p;
 87     //讀取檔案
 88     ReadFile(&p);
 89 
 90 
 91     //單詞翻譯:
 92     char content[1024];
 93 
 94     char ch[1024];
 95 
 96     while (1)
 97     {
 98         //fgets(ch, 1024, stdin);
 99         gets(ch);
100 
101         if (!strncmp("comm=exit", ch,9))
102         {
103             break;
104         }
105         //結果判斷
106         int flag = SearchWord(ch, content, p);
107         if (flag)
108             printf("%s\n", content);
109         else
110             printf("未找到該單詞的解釋!\n");
111     }
112 
113     //釋放堆空間
114     clear(p);
115 
116     return EXIT_SUCCESS;
117 }      

dict.h如下:

1 #pragma once
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #define WORDMAX 111104
 6 
 7 
 8 struct dicts
 9 {
10     char * word;
11     char * trans;
12 };
13 typedef struct dicts dic;
14 
15 void ReadFile(dic ** p);
16 int SearchWord(char * ch, char * content, dic * p);
17 void clear(dic *p);