(c++) Prompt user to enter a file name (can assme it is always going to be examp
ID: 3876335 • Letter: #
Question
(c++) Prompt user to enter a file name (can assme it is always going to be example.txt). For each word in that file, spelling will be checked, excluding any punctuation marks at the end and also converting a first letter that is uppercase to a lowercase except for the word "I". The input file, example.txt will be preserved and printed to the console with astericks* surrounding the mispelled words. The valid spelling of a word, other than "I," will be either spelling beginning with a lowercase letter or the spelling beginning with an uppercase letter. For example: "Rice" and "rice" are valid but "rICE" is not.
Use unordered_set under <unordered_set> header to store the dictionary words and vector to store the strings that will be printed to the console. There is a find member function that returns an iterator and an insert function, write code that can read every word from the dictionary file and store it in the multiset. Assume there is a file name dictionary.txt with a collection of words with correct spellings. Write a header file SpellCheck.h that contains the following helper functions:
isWhiteSpece: accepts a char and returns a bool, true if character is white space and otherwise false
dePuntuate: return void, accepts a string as an input and removes its terminating punctuation mark and makes the first letter lowercase
isValidWord: accepts the collection of dictionary spellings and a depunctuated word to checl, return true if the word has a valid spelling and otherwise false
finalPunctuation: returns a bool, true if the final character is a punctuation mark and otherwise false
First, get a vector to store all the display as a collection of words and strings of white space.
Read as much white space as possible from the file that is being spell-checked (example.txt), one char at a time, using int get() const; and int peek() const; and isWhiteSpece abd store all the white space as a string to be printed in the vector stooring the final console display.
Now, there is a word and the word should be read from the stream. Then check if the word has valid spelling by using dePunctuate and isValidWord. If spelling valid, store it including punctuation marks to print in vector. OTHERWISE, check if the word ends in punctuation using finalPunctuation.
pseudo code:
if (('Aa'<= c)&& (c<='Z')){
c+= ('a' - 'A'); } //if c is a char, convert c to lowercase if it is uppercase
REPEAT UNTIL STREAN US EXHAUSED then print all strings from the vector
The desired output given example.txt with dictionary.txt is provided below Enter file to spell check: example.txt That Time I Tried Skateboarding *oNce* upon a time, I was at a *sleepover* at *rfriend's* *plce®. we were evil *kds i and we made some prank callsandafterwards,my friend "uggestedI learn to skatebo ard. I was a little reluctant, but at 2 *a 1 didn't have the wisdom to "jsut" say no Things began all right: I could balance quite "welll when standing on the board as it rested on the grass And I could even balance a little on the board when it was rougly stationary on t he sidewalk. "Naturily, the next thing to try was a little skateboarding from the sidewalk onto the road Gracefully, I pushed myself and the skateboard forward, toawrds the road. Unbekno nst to me, however, was the fact that the curb was very, very steep: there was no gentle g radient but an abrupt drop of about a foot And so, I lost my balance and fe11 offf the b ard. The board glided smothleyinto the street and I, instinctively, flailed my arms bac kwards as I fel1 and managed to land perfectly on my left middle finger. ouch! I told my·frined" that my finger really hurt and he said, that it would be better in the morning. I agreed We went back in the house and eventually slept. It wasn't until the next day, after my parents picked me up, when my finger was "aabsolutelythrobbing that I suggested the p ssibility that my finger was broken A few hours later, after some x-rays indeed ny middle finger was broken. So I ha d to wear this claw-1ike cast on my hand fr about a month. And that was the first and las t time I tried to skatboardExplanation / Answer
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cstdio>
#include "CPUTimer.h"
#include "mem2.h"
#include "speller.h"
#ifdef DOS
#include <sysstat.h>
#else
#include <sys/stat.h>
#endif
int *ptr7;
int used7 = 0;
int maxRAM7 = 0;
using namespace std;
char* readDictionary(char *filename, char **dictionary)
{
int i = 0, fileSize;
char *s;
struct stat statbuf;
stat(filename, &statbuf);
fileSize = statbuf.st_size;
s = new char[fileSize + 1];
ifstream inf(filename);
inf.read(s, fileSize);
inf.close();
s[fileSize] = '';
dictionary[0] = strtok(s, " ");
while(dictionary[i])
dictionary[++i] = strtok(NULL, " ");
return s;
} // readDictionary
void readDocument(char **document, int dictSize, int docSize, int seed)
{
char *s, filename[80];
int i = 0, fileSize;
struct stat statbuf;
sprintf(filename, "Doc-%d-%d-%d.txt", dictSize, docSize, seed);
stat(filename, &statbuf);
fileSize = statbuf.st_size;
s = new char[fileSize + 1];
ifstream inf(filename);
inf.read(s, fileSize);
inf.close();
s[fileSize] = '';
document[0] = strtok(s, " ");
while(document[i])
document[++i] = strtok(NULL, " ");
} // readDocument()
void readWrongs(int *wrongs, int dictSize, int docSize, int seed,
int &wrongCount)
{
char filename[80];
wrongCount = 0;
sprintf(filename, "Wrongs-%d-%d-%d.txt", dictSize, docSize, seed);
ifstream inf(filename);
while(inf >> wrongs[wrongCount++]);
wrongCount--;
} // readWrongs()
void checkAnswers(int wrongs[], int wrongCount, int misspelled[],
int misspelledCount)
{
for(int i = 0; i < wrongCount && i < misspelledCount; i++)
if(wrongs[i] < misspelled[i])
{
cout << "Speller missed misspelled word # " << wrongs[i] << endl;
return;
}
else
if(wrongs[i] > misspelled[i])
{
cout << "Speller thought correctly spelled word # " << misspelled[i]
<< " was wrong ";
return;
}
if(wrongCount != misspelledCount)
cout << "Speller found " << misspelledCount << " misspelled words when "
<< " there were really " << wrongCount << " misspelled words. ";
} // checkAnswers
int main(int argc, char* argv[])
{
char line[80], **dictionary, **document, *s;
int *wrongs, *misspelled, misspelledCount = 0, seed, dictSize, docSize,
wrongCount;
strcpy(line, argv[1]);
strtok(line, "-");
dictSize = atoi(strtok(NULL, "-"));
docSize = atoi(strtok(NULL, "-"));
seed = atoi(strtok(NULL, "."));
dictionary = new char*[dictSize + 3];
s = readDictionary(argv[1], dictionary);
document = new char*[docSize + 3];
readDocument(document, dictSize, docSize, seed);
wrongs = new int[docSize];
readWrongs(wrongs, dictSize, docSize, seed, wrongCount);
misspelled = new int[docSize];
CPUTimer ct;
ct.reset();
Speller *speller = (Speller*) new2(Speller(dictionary, dictSize));
delete [] dictionary;
delete [] s;
speller->check(document, docSize, misspelled, &misspelledCount);
cout << "CPU Time: " << ct.cur_CPUTime() << " Real RAM: " << maxRAM7 << endl;
checkAnswers(wrongs, wrongCount, misspelled, misspelledCount);
delete speller;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.