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

📄 prg11_1.cpp

📁 这是数据结构和算法的国外经典书籍.清华大学出版社出版的<数据结构C++语言描述-应用模板库STL>陈君 译 英文名称是Data Structures with C++ Using STL.
💻 CPP
字号:
#ifdef _MSC_VER
// disable warning messages that identifier was truncated
// to 'number' characters in the debug information
#pragma warning(disable:4786)
#endif	// _MSC_VER

// File: prg11_1.cpp
// input the name of a file containing lowercase letters
// and no punctuation marks. call spellChecker() to check
// its spelling. the function creates a dictionay by
// inserting the whitespace separated lowercase words in
// the file "dict.dat" into a set. the function then
// reads the words in the document, looking each up in
// the set. a word is considered misspelled if it is not
// in the set. in this situation, prompt the user
// to add the word to the dictionary (input 'a'), ignore the
// word because is it actually spelled correctly ('i') or
// confirm it as misspelled (input 'm')

#include <iostream>
#include <fstream>
#include <string>
#include <set>

#include "d_util.h"	// for writeContainer()

using namespace std;

// check filename for spelling errors
void spellChecker(string& filename);

int main()
{
	string fileName;

	cout << "Enter the document to spell check: ";
	cin >> fileName;

	// check the spelling
	spellChecker(fileName);

	return 0;
}

void spellChecker(string& filename)
{
	// sets storing the dictionary and the misspelled words
	set<string> dictionary, misspelledWords;

	// dictionary and document streams
	ifstream dict, doc;
	string word;
	char response;

	// open "dict.dat"
	dict.open("dict.dat");
	if (!dict)
	{
		cerr << "Cannot open \"dict.dat\"" << endl;
		exit(1);
	}

	// open the document file
	doc.open(filename.c_str());
	if (!doc)
	{
		cerr << "Cannot open " << filename << endl;
		exit(1);
	}

	// insert each word from the file "dict.dat" into the dictionary set
	while(true)
	{
		dict >> word;
		if (!dict)
			break;

		// insert into the dictionary
		dictionary.insert(word);
	}

	// read the document word by word and check spelling
	while(true)
	{
		doc >> word;
		if (!doc)
			break;

		// lookup word up in the dictionary. if not present
		// assume word is misspelled. prompt user to add or ignore word
		if (dictionary.find(word) == dictionary.end())
		{
			cout << word << endl;
			cout << "    'a' (add)  'i' (ignore)  'm'  (misspelled) " ;
			cin >> response;
			// if response is 'a' add to dictionary; otherwise add to the
			// set of misspelled words
			if (response == 'a')
				dictionary.insert(word);
			else if (response == 'm')
				misspelledWords.insert(word);
		}
	}

	// display the set of misspelled words
	cout << endl << "Set of misspelled words" << endl;
	writeContainer(misspelledWords.begin(), misspelledWords.end());
	cout << endl;
}

/*
File "spell.tst"
teh message contians the url for the web-page
and a misspeled url for the email adress

Run:

Enter the document to spell check: spell.tst

teh
    'a' (add)  'i' (ignore)  'm'  (misspelled) m
contians
    'a' (add)  'i' (ignore)  'm'  (misspelled) m
url
    'a' (add)  'i' (ignore)  'm'  (misspelled) a
web-page
    'a' (add)  'i' (ignore)  'm'  (misspelled) i
misspeled
    'a' (add)  'i' (ignore)  'm'  (misspelled) m
email
    'a' (add)  'i' (ignore)  'm'  (misspelled) i
adress
    'a' (add)  'i' (ignore)  'm'  (misspelled) m

Set of misspelled words
adress  contians  misspeled  teh
*/

⌨️ 快捷键说明

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