天天看点

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);