You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++实现文本文件拼写检查及错误单词计数求助

拼写检查功能实现指南(针对你的代码改进)

先梳理你现有代码的核心问题

  • main函数调用spelling(file)时,file变量是空值,应该传入用户输入的目标文件名
  • spelling函数硬编码打开reading.txt,没有使用传入的参数
  • 完全缺失大小写转换、标点去除的逻辑,也没实现字典加载和单词匹配的核心功能
  • 手动写的hash_code可以用,但C++标准库的unordered_set(基于哈希表的集合)更适合新手,无需手动处理哈希冲突

1. 实现单词清洗工具函数

先写一个函数处理文本中的单词:统一转小写、去除首尾的标点/引号等非字母字符,确保和字典格式匹配:

#include <cctype> // 用于字符判断函数

string clean_word(string word) {
    // 转换为全小写
    for (char &c : word) {
        c = tolower(c);
    }
    // 移除开头非字母字符
    size_t start = 0;
    while (start < word.size() && !isalpha(word[start])) {
        start++;
    }
    // 移除结尾非字母字符
    size_t end = word.size() - 1;
    while (end >= start && !isalpha(word[end])) {
        end--;
    }
    // 返回清洗后的有效单词,空字符串直接跳过
    return start > end ? "" : word.substr(start, end - start + 1);
}

2. 用哈希集合加载字典

unordered_set是C++标准库的哈希表实现,查找速度快,刚好适合存储字典单词:

#include <unordered_set> // 引入哈希集合头文件

unordered_set<string> load_dictionary(const string& dict_file) {
    unordered_set<string> dict;
    ifstream dict_stream(dict_file);
    string word;
    // 逐行读取字典文件,清洗后存入哈希集合
    while (dict_stream >> word) {
        string cleaned = clean_word(word);
        if (!cleaned.empty()) {
            dict.insert(cleaned);
        }
    }
    return dict;
}

3. 重写拼写检查函数

实现核心的文件读取、单词检查、错误统计逻辑:

void spelling(const string& target_file) {
    // 加载字典
    unordered_set<string> dictionary = load_dictionary("dictionary.txt");
    if (dictionary.empty()) {
        cout << "字典文件加载失败或为空!" << endl;
        return;
    }

    // 打开目标文件
    ifstream target_stream(target_file);
    if (!target_stream.is_open()) {
        cout << "无法打开目标文件:" << target_file << endl;
        return;
    }

    string word;
    int error_count = 0;
    cout << "拼写错误的单词:" << endl;

    // 逐词读取目标文件
    while (target_stream >> word) {
        string cleaned = clean_word(word);
        if (cleaned.empty()) continue;
        // 检查单词是否在字典中
        if (dictionary.find(cleaned) == dictionary.end()) {
            cout << "- " << cleaned << endl;
            error_count++;
        }
    }

    cout << "\n总计拼写错误单词数:" << error_count << endl;
}

4. 修正main函数逻辑

修复参数传递和输入判断的问题:

int main() {
    string target_file;
    cout << "Enter the spell check file name" << endl;
    cin >> target_file;

    // 保留你原来的文件判断逻辑,也可以改成支持任意文件
    if (target_file != "flatland.txt") {
        cout << "That is not the correct file name. Enter again." << endl;
        cin >> target_file;
        // 可以加循环强制输入正确文件名:
        // while (target_file != "flatland.txt") {
        //     cout << "That is not the correct file name. Enter again." << endl;
        //     cin >> target_file;
        // }
    }

    // 传入正确的文件名调用检查函数
    spelling(target_file);

    return 0;
}

完整整合代码

把所有部分组合起来,确保头文件齐全:

#include <iostream>
#include <string>
#include <fstream>
#include <unordered_set>
#include <cctype>

using namespace std;

string clean_word(string word) {
    for (char &c : word) {
        c = tolower(c);
    }
    size_t start = 0;
    while (start < word.size() && !isalpha(word[start])) {
        start++;
    }
    size_t end = word.size() - 1;
    while (end >= start && !isalpha(word[end])) {
        end--;
    }
    return start > end ? "" : word.substr(start, end - start + 1);
}

unordered_set<string> load_dictionary(const string& dict_file) {
    unordered_set<string> dict;
    ifstream dict_stream(dict_file);
    string word;
    while (dict_stream >> word) {
        string cleaned = clean_word(word);
        if (!cleaned.empty()) {
            dict.insert(cleaned);
        }
    }
    return dict;
}

void spelling(const string& target_file) {
    unordered_set<string> dictionary = load_dictionary("dictionary.txt");
    if (dictionary.empty()) {
        cout << "字典文件加载失败或为空!" << endl;
        return;
    }

    ifstream target_stream(target_file);
    if (!target_stream.is_open()) {
        cout << "无法打开目标文件:" << target_file << endl;
        return;
    }

    string word;
    int error_count = 0;
    cout << "拼写错误的单词:" << endl;

    while (target_stream >> word) {
        string cleaned = clean_word(word);
        if (cleaned.empty()) continue;
        if (dictionary.find(cleaned) == dictionary.end()) {
            cout << "- " << cleaned << endl;
            error_count++;
        }
    }

    cout << "\n总计拼写错误单词数:" << error_count << endl;
}

int main() {
    string target_file;
    cout << "Enter the spell check file name" << endl;
    cin >> target_file;

    if (target_file != "flatland.txt") {
        cout << "That is not the correct file name. Enter again." << endl;
        cin >> target_file;
    }

    spelling(target_file);

    return 0;
}

关键说明

  • unordered_set的平均查找时间是O(1),比手动实现哈希表简单太多,适合新手快速完成功能
  • clean_word是核心逻辑,确保文本单词和字典单词格式统一,避免大小写、标点导致的误判
  • 代码里的注释可以帮你逐行理解每个模块的作用,建议你分步调试,先测试字典加载,再测试单词清洗,最后整合检查逻辑

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 22:00:56