Skip to content

Instantly share code, notes, and snippets.

@fhs
Created December 13, 2011 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fhs/1474277 to your computer and use it in GitHub Desktop.
Save fhs/1474277 to your computer and use it in GitHub Desktop.
Assignment #4 solution
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
const int WordListSize = 10000;
const int ParaWidth = 60; // paragraph width
class WordList {
private:
string words[WordListSize];
int nwords;
public:
WordList(void);
int length();
string at(int);
void setAt(int, string);
bool readFrom(istream&);
bool readFrom(string);
bool writeTo(string);
bool writeParaTo(ostream&);
bool writeParaTo(string);
void addWord(string);
bool hasWord(string);
};
WordList::WordList(void)
{
nwords = 0;
}
int
WordList::length(void)
{
return nwords;
}
string
WordList::at(int i)
{
if(i < 0 || i >= WordListSize)
return "";
return words[i];
}
void
WordList::setAt(int i, string w)
{
if(i < 0 || i >= WordListSize)
return;
words[i] = w;
}
bool
WordList::readFrom(istream &f)
{
string w;
nwords = 0;
f >> w;
while(f.good()){
addWord(w);
f >> w;
}
return true;
}
bool
WordList::readFrom(string filename)
{
ifstream f;
bool b;
f.open(filename.c_str());
if(f.fail())
return false;
b = readFrom(f);
f.close();
return b;
}
bool
WordList::writeTo(string filename)
{
int i;
ofstream f;
f.open(filename.c_str(), ofstream::trunc);
if(f.fail())
return false;
i = 0;
while(f.good() && i < nwords){
f << words[i] << endl;
i++;
}
f.close();
return true;
}
bool
WordList::writeParaTo(ostream &f)
{
int i, n;
string w;
n = 0;
for(i = 0; i < nwords; i++){
w = words[i];
n += w.length()+1;
if(n > ParaWidth){
f << endl;
n = w.length()+1;
}
f << w << " ";
}
if(n > 0)
f << endl;
return true;
}
bool
WordList::writeParaTo(string filename)
{
ofstream f;
bool b;
f.open(filename.c_str());
if(f.fail())
return false;
b = writeParaTo(f);
f.close();
return b;
}
void
WordList::addWord(string w)
{
if(w.length() == 0)
return;
if(nwords >= WordListSize){
cout << "too many words" << endl;
exit(1);
}
words[nwords] = w;
nwords++;
}
bool
WordList::hasWord(string w)
{
int i;
for(i = 0; i < nwords; i++)
if(words[i] == w)
return true;
return false;
}
bool
fileExists(string filename)
{
ifstream f;
bool b;
f.open(filename.c_str());
b = !f.fail();
f.close();
return b;
}
void
doParagraph(WordList &dict)
{
int i, choice, yes;
WordList ignore, para;
string w, s, filename;
cout << "Enter a paragraph (end it with EOF -- CTRL-z or CTRL-d):" << endl;
para.readFrom(cin);
cin.clear();
cout << endl;
for(i = 0; i < para.length(); i++){
w = para.at(i);
if(w.length() == 1 || ignore.hasWord(w) || dict.hasWord(w))
continue;
cout << "Incorrect word '" << w
<< "' [1=accept, 2=change, 3=ignore once, 4=ignore]: ";
cin >> choice;
switch(choice){
case 1:
dict.addWord(w);
break;
case 2:
cout << "Enter new word:";
cin >> s;
para.setAt(i, s);
break;
case 3: // do nothing
break;
case 4:
ignore.addWord(w);
break;
default:
cout << "internal error" << endl;
exit(1);
}
}
cout << endl;
cout << "Corrected paragraph:" << endl;;
para.writeParaTo(cout);
cout << endl;
cout << "Save corrected paragraph? [1=yes, 0=no]: ";
cin >> yes;
if(yes){
for(;;){
cout << "Enter filename: ";
cin >> filename;
if(!fileExists(filename))
break;
cout << "file by that name already exists" << endl;
}
if(!para.writeParaTo(filename))
cout << "failed to write paragraph to " << filename << endl;
}
}
int
main(void)
{
WordList dict, ignore, para;
string dictname;
int dlen, yes;
cout << "Welcome!" << endl;
cout << "Enter dictionary filename: ";
cin >> dictname;
if(!dict.readFrom(dictname)){
cout << "failed to open dictionary file " << dictname << endl;
exit(1);
}
dlen = dict.length();
do{
doParagraph(dict);
cout << "Try another paragraph? [1=yes, 0=no]: ";
cin >> yes;
}while(yes);
if(dict.length() > dlen && !dict.writeTo(dictname)){
cout << "failed to write to dictionary file " << dictname << endl;
exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment