[C.C++] 【C语言】简易英语词典

360 0
Honkers 2025-4-2 10:01:10 来自手机 | 显示全部楼层 |阅读模式

一、定义英语单词信息的结构体

添加必要的头文件、宏定义和声明,之后定义英语单词信息结构体。

  1. /* 头文件和宏定义 */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. /* 单词最大长度 和 单词定义最大长度 */
  7. #define MAX_WORD_LENGTH 20
  8. #define MAX_DEFINITION_LENGTH 200
  9. /* 英语单词结构体定义 */
  10. struct EnglishWord
  11. {
  12. char word[MAX_WORD_LENGTH]; // 单词拼写
  13. char definition[MAX_DEFINITION_LENGTH]; // 单词定义
  14. };
  15. /* 函数声明 */
  16. void searchWord(const char *filename);
  17. void memorizeWord(const char *filename);
复制代码

可以更改结构体的信息,以此来添加新功能。

添加另一个定义,之后可以收藏单词,查看收藏单词列表…

  1. /*
  2. 结构体定义表示一个英文单词
  3. 1、单词是否收藏
  4. 2、单词拼写
  5. 3、单词定义
  6. */
  7. struct EnglishWord
  8. {
  9. int isFavorite;
  10. char word[MAX_WORD_LENGTH];
  11. char definition[MAX_DEFINITION_LENGTH];
  12. };
复制代码

二、主函数功能逻辑

包含一个主循环,该循环持续显示菜单并等待用户输入。根据用户的选择,程序将执行相应的操作,如查单词、背单词或退出系统。在选择查询单词时,程序还包含一个嵌套循环,允许用户连续查询单词直到选择退出。程序会持续运行,直到用户选择退出系统。

  1. /* 主函数*/
  2. int main()
  3. {
  4. // 定义
  5. int choice;
  6. while (1)
  7. {
  8. printf("--------英语单词词典--------\n");
  9. printf("\t1. 查单词\n");
  10. printf("\t2、背单词\n");
  11. printf("\t0. 退出系统\n");
  12. printf("请选择操作:");
  13. scanf("%d", &choice);
  14. switch (choice)
  15. {
  16. case 1:
  17. /* 连续查询 */
  18. while (1)
  19. {
  20. char ch;
  21. searchWord("English.txt");
  22. printf("继续搜索单词(y/n): ");
  23. scanf(" %c", &ch); // 在%c前添加一个空格以跳过可能的空白字符
  24. while (getchar() != '\n');
  25. if (ch == 'n')
  26. {
  27. break;
  28. }
  29. }
  30. break;
  31. case 2:
  32. memorizeWord("English.txt");
  33. break;
  34. case 0:
  35. printf("成功退出系统。");
  36. exit(0);
  37. break;
  38. default:
  39. printf("无效选择,请重新输入!");
  40. }
  41. }
  42. return 0;
  43. }
复制代码
  1. 主循环: 主函数采用了一个无限循环 while (1),使得用户可以在执行完一次操作后选择继续进行其他操作或退出系统。
  2. 菜单显示和选择: 主循环内部通过 printf 显示简单的菜单,包括查单词、背单词、退出系统的选项。使用 scanf 获取用户的选择,并通过 switch 语句根据用户的选择执行相应的操作。
  3. 操作执行: switch 语句中根据用户的选择,执行相应的操作。如果用户选择查单词,则进入嵌套循环,调用 searchWord 函数实现连续查询。如果用户选择背单词,则调用 memorizeWord 函数执行背单词操作。如果用户选择退出系统,则输出退出信息并调用 exit(0) 退出程序。
    1. 查单词操作: 在选择查单词时,通过嵌套循环实现连续查询。用户可以在一次查询结束后选择是否继续查询,通过输入 ‘y’ 或 ‘n’ 决定。内部调用 searchWord 函数进行查询,保留用户的选择状态。
    2. 背单词操作: 在选择背单词时,调用 memorizeWord 函数执行背单词操作。用户在背单词的过程中可以通过按下任意键继续背下一个单词,包括 Enter 键。通过嵌套循环和非阻塞键盘输入的方式实现。
    3. 退出系统操作: 在选择退出系统时,输出退出信息并调用 exit(0) 退出程序,结束主循环。

三、查单词函数

这C语言函数用于在给定的文件中搜索指定的英文单词。
英语单词文件已进行资源绑定,可以在我的资源中进行查看。

  1. void searchWord(const char *filename)
  2. {
  3. // 变量定义
  4. FILE *file;
  5. int flag = 0;
  6. char searchWord[MAX_WORD_LENGTH];
  7. struct EnglishWord currentWord;
  8. // 打开文件,并检查是否打开成功
  9. if ((file = fopen(filename, "r")) == NULL)
  10. {
  11. printf("无法打开文件 %s\n", filename);
  12. return;
  13. }
  14. // 获取用户输入待查找的英文单词
  15. printf("请输入待查找的英文单词(小写): ");
  16. scanf("%s", searchWord);
  17. // 遍历文件,查找匹配的英文单词
  18. while (fscanf(file, "%s %[^\n]", currentWord.word, currentWord.definition) != EOF)
  19. {
  20. if (strncmp(currentWord.word, searchWord, strlen(searchWord)) == 0)
  21. {
  22. printf("\t英文单词: %s\n\t解释: %s\n\n", currentWord.word, currentWord.definition);
  23. flag = 1;
  24. }
  25. }
  26. if (0 == flag) {
  27. printf("未找到英文单词:%s\n", searchWord);
  28. }
  29. // 关闭文件
  30. fclose(file);
  31. }
复制代码
  1. 函数声明:

    1. void searchWord(const char *filename);
    复制代码
  2. 参数:

    • filename: 包含英文单词及其解释的文件名。
  3. 局部变量和结构体定义

    1. FILE *file;
    2. int flag = 0;
    3. char searchWord[MAX_WORD_LENGTH];
    4. struct EnglishWord currentWord;
    复制代码
    • file: 文件指针,用于打开和读取文件。
    • flag: 用于标记是否找到匹配的英文单词。
    • searchWord: 存储用户输入的待查找的英文单词。
    • currentWord: 结构体,包含英文单词和对应的解释。
  4. 文件打开检查:

    1. if ((file = fopen(filename, "r")) == NULL)
    2. {
    3. printf("无法打开文件 %s\n", filename);
    4. return;
    5. }
    复制代码
    • 检查文件是否成功打开,如果未成功,则输出错误信息并返回。
  5. 用户输入待查找的英文单词:

    1. printf("请输入待查找的英文单词(小写): ");
    2. scanf("%s", searchWord);
    复制代码
    • 提示用户输入待查找的英文单词。
  6. 遍历文件并比较英文单词:

    1. while (fscanf(file, "%s %[^\n]", currentWord.word, currentWord.definition) != EOF)
    2. {
    3. // 检查是否找到匹配的英文单词
    4. if (strncmp(currentWord.word, searchWord, strlen(searchWord)) == 0)
    5. {
    6. printf("\t英文单词: %s\n\t解释: %s\n\n", currentWord.word, currentWord.definition);
    7. flag = 1;
    8. }
    9. }
    复制代码
    • 使用**fscanf**逐行读取文件内容,比较每一行的英文单词与用户输入的待查找单词。
    • 如果找到匹配,输出英文单词和解释,设置**flag**为1。
  7. 输出结果:

    1. if (0 == flag)
    2. {
    3. printf("未找到英文单词:%s\n", searchWord);
    4. }
    复制代码
    • 如果未找到匹配的英文单词,输出相应提示。
  8. 关闭文件:

    1. fclose(file);
    复制代码
    • 关闭打开的文件,以防止内存泄漏

四、背单词函数

通过逐个从文件中读取英文单词及其解释,使用户能够逐个记忆这些单词,并且能够读取上次结束的位置。
英语单词文件已进行资源绑定,可以在我的资源中进行查看。

  1. void memorizeWord(const char *filename)
  2. {
  3. // 变量定义
  4. char ch;
  5. FILE *file;
  6. FILE *recordFile;
  7. long lastPosition = 0;
  8. struct EnglishWord currentWord;
  9. // 打开文件,并检查是否打开成功
  10. if ((file = fopen(filename, "r")) == NULL)
  11. {
  12. printf("无法打开文件 %s\n", filename);
  13. return;
  14. }
  15. // 获取文件最后载入位置
  16. if ((recordFile = fopen("position_record.txt", "r")) == NULL)
  17. {
  18. printf("单词位置记录失败!");
  19. return;
  20. }
  21. else
  22. {
  23. fscanf(recordFile, "%ld", &lastPosition);
  24. fclose(recordFile);
  25. fseek(file, lastPosition, SEEK_SET);
  26. }
  27. // 逐一输入单词
  28. while (fscanf(file, "%s %[^\n]", currentWord.word, currentWord.definition) != EOF)
  29. {
  30. FILE *recordFile;
  31. // 判断是否继续背单词
  32. printf("按任意键背下一单词(q to quit)");
  33. scanf(" %c", &ch);
  34. if (ch == 'q') { break; }
  35. while ((ch = getchar()) != '\n' && ch != EOF);
  36. printf("\t英文单词: %s\n\t解释: %s\n\n", currentWord.word, currentWord.definition);
  37. // 将本次读取的单词位置保存进新文件
  38. lastPosition = ftell(file);
  39. if ((recordFile = fopen("position_record.txt", "w")) == NULL)
  40. {
  41. printf("单词位置记录失败!");
  42. }
  43. else
  44. {
  45. fprintf(recordFile, "%ld", lastPosition);
  46. fclose(recordFile);
  47. }
  48. }
  49. // 关闭文件
  50. fclose(file);
  51. }
复制代码
  1. 打开文件:

    • 函数接受一个文件名作为参数,并尝试以只读模式打开该文件。
    • 如果文件打开失败,函数打印错误消息并返回。
    1. if ((file = fopen(filename, "r")) == NULL)
    2. {
    3. printf("无法打开文件 %s\n", filename);
    4. return;
    5. }
    复制代码
  2. 获取上次读取的位置:

    • 函数尝试打开名为 “position_record.txt” 的记录文件,该文件保存上次读取的文件位置。
    • 如果记录文件打开失败,函数打印错误消息并返回。
    • 如果成功打开记录文件,函数读取上次读取的文件位置,并将文件指针移到该位置。
    1. if ((recordFile = fopen("position_record.txt", "r")) == NULL)
    2. {
    3. printf("单词位置记录失败!");
    4. return;
    5. }
    6. else
    7. {
    8. fscanf(recordFile, "%ld", &lastPosition);
    9. fclose(recordFile);
    10. fseek(file, lastPosition, SEEK_SET);
    11. }
    复制代码
  3. 逐个输入单词:

    • 使用循环从文件中逐个读取英文单词及其解释,存储在结构体 struct EnglishWord 中。
    • 在每个单词显示后,程序提示用户按任意键继续或按 ‘q’ 键退出。
    • 用户按 ‘q’ 键退出时,循环终止。
    1. while (fscanf(file, "%s %[^\n]", currentWord.word, currentWord.definition) != EOF)
    2. {
    3. // ... (显示单词,等待用户输入)
    4. if (ch == 'q') { break; }
    5. // ... (保存当前读取位置)
    6. }
    复制代码
  4. 记录当前读取位置:

    • 在每次成功读取一个单词后,将当前文件指针位置保存到 “position_record.txt” 文件中,以便下一次程序运行时能够从上次中断的位置继续读取。
    1. lastPosition = ftell(file);
    2. if ((recordFile = fopen("position_record.txt", "w")) == NULL)
    3. {
    4. printf("单词位置记录失败!");
    5. }
    6. else
    7. {
    8. fprintf(recordFile, "%ld", lastPosition);
    9. fclose(recordFile);
    10. }
    复制代码
  5. 关闭文件:

    • 最后关闭打开的文件。
    1. fclose(file);
    复制代码

五、补充

  • 完整程序

    1. /* 相关头文件 */
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <string.h>
    5. #include <ctype.h>
    6. /* 单词最大长度 和 单词定义最大长度 */
    7. #define MAX_WORD_LENGTH 20
    8. #define MAX_DEFINITION_LENGTH 200
    9. // 结构体定义表示一个英文单词
    10. struct EnglishWord
    11. {
    12. char word[MAX_WORD_LENGTH];
    13. char definition[MAX_DEFINITION_LENGTH];
    14. };
    15. // 函数声明
    16. void searchWord(const char *filename);
    17. void memorizeWord(const char *filename);
    18. int main()
    19. {
    20. // 定义
    21. int choice;
    22. printf("--------英语单词词典--------\n");
    23. printf("\t1. 查单词\n");
    24. printf("\t2、背单词\n");
    25. printf("\t0. 退出系统\n");
    26. printf("请选择操作:");
    27. scanf("%d", &choice);
    28. switch(choice) {
    29. case 1:
    30. /* 连续查询 */
    31. while(1) {
    32. char ch;
    33. searchWord("English.txt");
    34. printf("继续搜索单词(y/n): ");
    35. scanf(" %c", &ch); // 在%c前添加一个空格以跳过可能的空白字符
    36. while(getchar() != '\n');
    37. if (ch == 'n') {
    38. break;
    39. }
    40. }
    41. break;
    42. case 2:
    43. memorizeWord("English.txt");
    44. break;
    45. default:
    46. printf("无效选择,请重新输入!");
    47. }
    48. return 0;
    49. }
    50. // 查找英文单词的函数
    51. void searchWord(const char *filename)
    52. {
    53. /* 定义 */
    54. FILE *file;
    55. int flag = 0;
    56. char searchWord[MAX_WORD_LENGTH];
    57. struct EnglishWord currentWord;
    58. /* 打开文件,并检查是否打开成功 */
    59. if ((file = fopen(filename, "r")) == NULL)
    60. {
    61. printf("无法打开文件 %s\n", filename);
    62. return;
    63. }
    64. printf("请输入待查找的英文单词(小写): ");
    65. scanf("%s", searchWord);
    66. /* 遍历文件,同时将格式化文件的内容逐一赋值到 word 和 definition 中 */
    67. while (fscanf(file, "%s %[^\n]", currentWord.word, currentWord.definition) != EOF)
    68. {
    69. // 检查是否找到匹配的英文单词
    70. if (strncmp(currentWord.word, searchWord, strlen(searchWord)) == 0)
    71. {
    72. printf("\t英文单词: %s\n\t解释: %s\n\n", currentWord.word, currentWord.definition);
    73. flag = 1;
    74. }
    75. }
    76. if (0 == flag) {
    77. printf("未找到英文单词:%s\n", searchWord);
    78. }
    79. // 一定要关闭文件,防止内存泄漏
    80. fclose(file);
    81. }
    82. void memorizeWord(const char *filename)
    83. {
    84. char ch;
    85. FILE *file;
    86. FILE *recordFile;
    87. long lastPosition = 0;
    88. struct EnglishWord currentWord;
    89. //
    90. if ((file = fopen(filename, "r")) == NULL)
    91. {
    92. printf("无法打开文件 %s\n", filename);
    93. return;
    94. }
    95. // 获取文件最后载入位置
    96. if ((recordFile = fopen("position_record.txt", "r")) == NULL)
    97. {
    98. printf("单词位置记录失败!");
    99. return;
    100. }
    101. else
    102. {
    103. fscanf(recordFile, "%ld", &lastPosition);
    104. fclose(recordFile);
    105. fseek(file, lastPosition, SEEK_SET);
    106. }
    107. // 逐一输入单词
    108. while (fscanf(file, "%s %[^\n]", currentWord.word, currentWord.definition) != EOF)
    109. {
    110. FILE *recordFile;
    111. /* 判断是否继续背单词 */
    112. printf("按任意键背下一单词(q to quit)");
    113. scanf(" %c", &ch); // 获取用户输入
    114. if (ch == 'q') { break; }
    115. while ((ch = getchar()) != '\n' && ch != EOF);
    116. printf("\t英文单词: %s\n\t解释: %s\n\n", currentWord.word, currentWord.definition);
    117. /* 将本次读取的单词位置保存进新文件 */
    118. lastPosition = ftell(file);
    119. if ((recordFile = fopen("position_record.txt", "w")) == NULL)
    120. {
    121. printf("单词位置记录失败!");
    122. }
    123. else
    124. {
    125. fprintf(recordFile, "%ld", lastPosition);
    126. fclose(recordFile);
    127. }
    128. }
    129. fclose(file);
    130. }
    复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4010
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行