⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 一个拼写检查程序
💻 CPP
字号:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cctype>

#include "dictionary.h"

using namespace std;

void lower(string& s);
string strip_punct(const string& s);
void check_spelling(ifstream& in, Dictionary& dict);


int main(int argc, char* argv[]) {

    // Output usage message if improper command line args were given.
    if (argc != 3) {
        cerr << "Usage: " << argv[0] << " wordlist_filename input_file\n";
        return EXIT_FAILURE;
    }

    ifstream inf(argv[2]);
    if (! inf) {
        cerr << "Could not open " << argv[2] << "\n";
        return EXIT_FAILURE;
    }

    // Read dictionary, but let user know what we are working on.
    cout << "Loading dictionary, this may take awhile...\n";

    Dictionary d(argv[1]);

    check_spelling(inf, d);

    inf.close();

    return EXIT_SUCCESS;
}
/*
finish the implementation of function check_spelling. 
This function already contains code that reads a file
 line by line. It also extracts each word from a line 
 using an instance of class stringstream. Your task is
 to check the spelling of each word. Use the inherited 
 search function of class Dictionary to determine if a 
 word exists in the dictionary. If the word exists in 
 the dictionary, assume that it is spelled correctly. 
 It if does not exist, assume it is misspelled. For each 
 misspelled word, generate and display a list of possible corrections.
*/
void check_spelling(ifstream& in, Dictionary& dict) {

    int line_number = 0;

    while (in) {

        line_number++;

        string line;
        getline(in, line);

        stringstream ss (stringstream::in | stringstream::out);
        ss << line;
        string word;
        while (ss >> word) {

            // TODO: Complete the spell check of each word
			int i = 0;
			while(i < word.size())
			{
				if(word[i] >= 'A' && word[i] <= 'Z')
					word[i] += 32;			//如果此字母是大写,变成小写
				else if(word[i] >= 'a' && word[i] <= 'z')
				{
					i++;					//如果此字母是小写,继续循环
					continue;
				}
				else
				{
					word.erase(i, 1);		//如果此字母不是小写,不是大写,去除此字符
				}
				i++;
			}
			if(!dict.search(word))			//如果单词不对,即在Dictionary中找不到,执行以下操作
			{
				cout << "line " << line_number			//输出错误单词所在的行数以及错误单词
					<< ": '" << word << "'"<< endl;
				cout << "\t suggestions:" << endl;		//提示用户正确单词如下所示
			
				i = 0;					//计数值
				string find_word_1;
				string find_word_2;
				string find_word_3;
				string find_word_4;
				string find_word_5;
				string find_word_6;
				string find_word_7;
				while(i < word.size())			//逐字母扫描错误单词
				{
			//	case 1:		此单词多一个字母:
					find_word_1 = word;			
					find_word_1.erase(i, 1);	//从单词中删除这个字母
					if(dict.search(find_word_1))	//在Dictionary中查找此单词
					{
							if(find_word_1 != find_word_2)	//查看此新单词是否已经输出过
							{
								find_word_2 = find_word_1;	//如果此新单词没有输出过,赋给find_word_2
								cout << "\t\t" << find_word_2 << endl;		//输出此新单词
							}
					}
					for(char cha = 'a'; cha <= 'z'; cha++)		//每个字母
					{
			//	case 2:		此单词少一个字母:
						find_word_3 = word;
						string my_inser(&cha,1);		//把这个字母变成string类型
						find_word_3.insert(i, my_inser);	//给此单词插入一个字母
						if(dict.search(find_word_3))	//在Dictionary中查找此单词
						{
							if(find_word_3 != find_word_4)	//查看此新单词是否已经输出过
							{
								find_word_4 = find_word_3;	//如果此新单词没有输出过,赋给find_word_4
								cout << "\t\t" << find_word_4 << endl;		//输出此新单词
							}
						}
			//	case 3: 	此单词错一个字母
						find_word_5 = word;
						string my_change(&cha,1);		//把这个字母变成string类型
						find_word_5.replace(i, 1, my_change);	//给此单词替换一个字母
						if(dict.search(find_word_5))	//在Dictionary中查找此单词
						{
							if(find_word_5 != find_word_6)	//查看此新单词是否已经输出过
							{
								find_word_6 = find_word_5;	//如果此新单词没有输出过,赋给find_word_6
								cout << "\t\t" << find_word_6 << endl;		//输出此新单词
							}
						}
					}
			//	case 4: 	此单词两个字母位置不对
					find_word_7 = word;
					if(word[i] == word[i+1])
					{					//如果这个字母和它后续的字母相同,直接循环,不操作
						i++;
						continue;
					}
					string first(&word[i+1],1);		//把这个字母变成string类型
					string second(&word[i],1);		//把这个字母变成string类型
					find_word_7.replace(i, 1, first);		//交换两个字母的位置
					find_word_7.replace(i+1, 1, second);
					if(dict.search(find_word_7))		//在Dictionary中查找此单词
						cout << "\t\t" << find_word_7 << endl;		//输出此新单词
					i++;		//循环计数值递增
				}

			}

        }

    }

}

void lower(string& s) {

    // Ensures that a word is lowercase
    for (unsigned int i = 0; i < s.length(); i++) {
        s[i] = tolower(s[i]);
    }
}

string strip_punct(const string& s) {

    // Remove any single trailing
    // punctuation character from a word.
    if (ispunct(s[s.length() - 1]) ) {
        return s.substr (0, s.length() - 1);
    }
    else {
        return s;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -