Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ object oriented programming Write an object-oriented program to create a dic

ID: 3599672 • Letter: C

Question

c++ object oriented programming

Write an object-oriented program to create a dictionary. The dictionary has 26 files (files). You may name the files (tables) as A.txt, B.txt,..., Z.txt. Your dictionary class should have at least the following attributes and methods:

#include<iostream>

#include<string>

#include<vector>

using namespace std;

class Dictionary

{

              private:

                             const      int maxWordsInDict;

                             const     int maxWordsPerFile;

                                           int          totalWordsInDict;

                                           int          numOfWordsInFile[26];

                             static     bool failure;

                             static     bool success;

             

              public:

                             Dictionary();

                             Dictionary(int dictMaxWords, int fileMaxWords);

                             bool AddAWord(string word);

                             bool DeleteAWord(string word);

                             bool SearchForWord(string word);

                             bool PrintAFileInDict(string filename);

                             bool SpellChecking(string fileName);

                             bool InsertWordsIntoDict(string fileName);

                             void ProcessTransactionFile();                  

};

bool Dictionary::failure = false;

bool Dictionary::success = true;

Dictionary::Dictionary()

{

/* This routine sets the following values to the following attributes

                             maxWordsInDict = 10000;

                             maxWordsPerFile = 100;

                             totalWordsInDict = 0;       

                             numOfWordsInFile[0] = 0; referring to A.txt

                             numOfWordsInFile[1] = 0; referring to B.txt

                            

                            

                             numOfWordsInFile[25] = 0; referring to Z.txt

*/

}

             

//------------------------------------------------------------------

Dictionary::Dictionary(int dictMaxWords, int fileMaxWords)

{

/* This routine sets the following values to the following attributes

                             maxWordsInDict = dictMaxWords;

                             maxWordsPerFile = fileMaxWords;

                             totalWordsInDict = 0;       

                             numOfWordsInFile[0] = 0; referring to A.txt

                             numOfWordsInFile[1] = 0; referring to B.txt

                            

                            

                             numOfWordsInFile[25] = 0; referring to Z.txt

*/

}

             

//------------------------------------------------------------------

bool AddAWord(string word)

{

/* This routine opens the appropriate file, (If the first letter of the word is A, it should be in A.txt, or if the first letter of the word starts with B it should be in B.txt, and so on) and tries to add the word that is passed to this function into that file.

Before adding the word to the file, you should call the search function to ensure that the word is not already in the appropriate file

If you could add a word successfully into the appropriate file, you should increment the attribute TotalWordsInDict by 1 as long as you do not exceed the maxWordsInDict.

Further, if you add a word to A.txt you need to increment numOfWordsInFile[0] by 1. Similarly if you add a word to B.txt you need to increment numOfWordsInFile[1] and so on. Incrimination should be done as long as you do not exceed the maximum value.

If the word that you are trying to add, could not be added into the appropriate file for any reason, this routine should return failure

                                                                        return (Dictionary::failure)

otherwise, if the word is successfully added, this routine should return success

                                                                        return (Dictionary::success)

HINT: suppose the word you try to add is "adam". The word "adam" should be added to A.txt. Therefore, you need to open "A.txt" first. To do that you can do the following

string fileName = "#.txt"; // all files should be have ".txt" extension

fileName[0] = toupper(word[0]);      // replaces the # sign with appropriate letter. Note that word is "adam" and. Letter 'a' is the first letter of the word and should be changed to upper case

ofstream fout;

fout.open(fileName.data(), ios::app); // ios::app means appending the word to the file rather than overwriting the old information in the file

fout << word << endl;

*/

}

//------------------------------------------------------------------

bool DeleteAWord(string word)

{

/* This routine opens the appropriate file where the word that you are looking for might be kept. (If the first letter of the word is A, it should be in A.txt, or if the first letter of the word starts with B it should be in B.txt, and so on. Then it places all the words into a vector and looks for the word in the vector. If the word is in the vector, it should be deleted and then the modified vector should be inserted back into the appropriate file

If you could remove a word successfully from the appropriate file, you should decrement the attribute TotalWordsInDict by 1.

Further, if you remove a word from A.txt you need to decrement numOfWordsInFile[0] by 1. Similarly if you remove a word from B.txt you need to decrement numOfWordsInFile[1] and so on.

If the word that you are trying to delete is not in the appropriate file, this routine should return failure

                                                                        return (Dictionary::failure)

otherwise, when the word is successfully deleted, this routine should return success

                                                                        return (Dictionary::success)

Note that before placing anything into the vector; it is a good idea to call the search function to ensure that the word is in the appropriate file

*/

}

//------------------------------------------------------------------

bool SearchForWord(string word)

{

/* This routine opens the appropriate file where the word that you are looking for might be kept. (If the first letter of the word is A, it should be in A.txt, or if the first letter of the word starts with B it should be in B.txt, and so on. If the word cannot be found in the appropriate file, this routine returns failure

                                                                        return (Dictionary::failure)

otherwise, if the word does exist in the appropriate file, it returns success

                                                                        return (Dictionary::success)

*/

}

//------------------------------------------------------------------

bool PrintAFileInDict(string filename)

{

/* This routine opens the file fileName, read the words one by one and print them on the screen. You should only print 5 words per line.

If the fileName could not be opened, this routine returns failure

                                                          return (Dictionary::failure)

otherwise, it returns success

                                                          return (Dictionary::success)

*/

}

//------------------------------------------------------------------

bool SpellChecking(string fileName)

{

/* This routine opens the file fileName, read the words one by one and does the spell checking of the words. Every word is searched and those that are not in the dictionary are reported on the screen

If the fileName could not be opened, this routine returns failure

                                                          return (Dictionary::failure)

otherwise, it returns success

                                                          return (Dictionary::success)

*/

}

//------------------------------------------------------------------

bool InsertWordsIntoDict(string fileName)

{

/* This routine opens the file fileName, read the words one by one and try to insert them into the dictionary. Note that before inserting a word into the dictionary, you should search the dictionary to ensure that the word is not there.

If the fileName could not be opened, this routine returns failure

                                                          return (Dictionary::failure)

otherwise, it returns success

                                                          return (Dictionary::success)

*/

}

//------------------------------------------------------------------

void ProcessTransactionFile()

{

/* In this routine, you need to ask the user to enter a transaction file. A transaction file has two information per line. The first information is a command telling the programmer what to do. The second information is the data associated with the command. For example, a possible transaction file may contain

                                           AddW                                               student

                                           DeleteW                                           play

                                           SearchW                                           medical

                                           PrintF                                               Q.txt

                                           SpellCheck                                       myEssay.txt

                                           InsertF                                              data.txt

                                           ....

                                           ....

                                           ....

AddW should add the word "student" into the dictionary. You should call the AddAWord(string word) function to add the word "student" into the dictionary

DeleteW should remove the word "play" from the dictionary. You should call the DeleteAWord(string word) function to remove the word "play" from the dictionary

SearchW should search for the word "medical" in the dictionary. You should call the SearchForWord(string word) function to search for the word "medical" in the dictionary

PrintF should print all the words in a particular fie on the screen. You should call the function PrintAFileInDict(string fileName) to print the content of file Q.txt on the screen

SpellCheck should do the spell checking of a file. All errors should be reported on the screen. You should call the function SpellChecking(string fileName) to do the spell checking of the file called "myEssay.txt"

InsertF should process the content of a file and insert word by word in the file into the dictionary. You should call the function InsertWordsIntoDict(string fileName) to add the content of the file called "data.txt" into the dictionary

Note that if the transaction file does not exist, you need to print appropriate failure message on the screen

*/

}

//------------------------------------------------------------------

int main()

{

              Dictionary DictForCS211;

              DictForCS211.ProcessTransactionFile();

              return 0;

}

Explanation / Answer

main.cpp
------------------------------------------------------------
#include "Dictionary.h"
using namespace std;

int main()
{
    Dictionary DictForCS211;
    DictForCS211.ProcessTransactionFile();
    return 0;
}

-------------------------------------------------------------------
Dictionary.cpp
-----------------------------------------------
#ifndef DICTIONARY_CPP
#define DICTIONARY_CPP
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include <iostream>
#include "Dictionary.h"
using namespace std;

bool Dictionary::failure = false;
bool Dictionary::success = true;

Dictionary::~Dictionary()
{
}


Dictionary::Dictionary() : maxWordsInDict(10000), maxWordsPerFile(100)
{
    totalWordsInDict = 0;
    for(int i = 0; i < LETTERSOFTHEALPHABET; i++)
        numOfWordsInFile[i] = 0;
}


Dictionary::Dictionary(int dictMaxWords, int fileMaxWords) :
        maxWordsInDict(dictMaxWords), maxWordsPerFile(fileMaxWords)
{
    totalWordsInDict = 0;
    for(int i = 0; i < LETTERSOFTHEALPHABET; i++)
        numOfWordsInFile[i] = 0;
}

bool Dictionary::AddAWord(string word)
{
    string fileName = fileNamer(word);   // Gets the file name
    char alphaIndex = toupper(word[0]) - 'A';   // Gets the correct index for
    // Words in file array
    string lowerCaseWord = wordToLower(word);   // Saves word in all lowercase

    ofstream fout;
    fout.open(fileName.data(), ios::app);   // Output stream that appends to file

    if(totalWordsInDict >= maxWordsInDict)
    {
        // Checks for the maximum number of words in the dictionary
        cout << endl << "Too many words in the dictionary!" << endl;
        return failure;
    }
    else if(SearchForWord(lowerCaseWord))
    {
        // Checks if the word is already in the dictionary
        cout << endl << """ << lowerCaseWord
             << "" already exists in dictionary!" << endl;
        return failure;
    }
    else if(numOfWordsInFile[alphaIndex] >= maxWordsPerFile)
    {
        // Checks if there are too many words inside of the file
        cout << endl << "Too many words in the " << toupper(lowerCaseWord[0])
             << " section!" << endl;
        return failure;
    }

    fout << lowerCaseWord << endl;   // Output

    if(SearchForWord(lowerCaseWord))
    {
        // Double checks to make sure the output was successful
        totalWordsInDict++;
        numOfWordsInFile[alphaIndex]++;
        return success;
    }
    else
        return failure;

}


bool Dictionary::DeleteAWord(string word)
{
    string fileName = fileNamer(word);
    string lowerCaseWord = wordToLower(word);
    string importedWord;
    vector<string> dictionary; // Vector to store the file and parse for word
    ifstream fin;
    ofstream fout;
    fin.open(fileName.data());

    while (fin >> importedWord)
    {
        // Imports words from file one by one and adds to the vector
        dictionary.push_back(importedWord);
    }

    fin.close();

    // Clears the file. The contents are stored in the vector.
    fout.open(fileName.data());
    fout << "";
    fout.close();

    while(dictionary.size() != 0)
    {
        if(lowerCaseWord.compare(dictionary.back()) == 0)
        {

            totalWordsInDict--;
            numOfWordsInFile[toupper(lowerCaseWord[0]) - 'A']--;
        }
        else
        {
            /* If these are not the words you are looking for, add it back into
                the file*/
            AddAWord(dictionary.back());
        }
        dictionary.pop_back();   // Removes the last stored word.
    }

    // Double checks to make sure that the word is no longer in the file.
    if(SearchForWord(lowerCaseWord))
        return failure;
    else
        return success;

}


bool Dictionary::SearchForWord(string word)
{
    string fileName = fileNamer(word);
    string importedWord;
    ifstream fin;
    fin.open(fileName.data());

    while(fin >> importedWord)
    {
        // Imports each word compares to the lowercase version of the given word
        if(importedWord == wordToLower(word))
            return success;
    }

    return failure;
}

bool Dictionary::PrintAFileInDict(string fileName)
{
    ifstream fin;
    fin.open(fileName.data());
    string importedWord;
    int rowCounter = 0;       // Used to make sure a max of 5 words per line

    if(!fin)
    {
        // Checks if the file exists
        cout << "File could not be found." << endl;
        return failure;
    }

    cout << "Words that begin with " << fileName[0] << ": " << endl << endl;

    while(fin >> importedWord)
    {

        cout << setw(15) << left << importedWord << " | ";
        rowCounter++;

        if(rowCounter % 5 == 0)
            cout << endl;
    }
    cout << endl;

    return success;
}


bool Dictionary::SpellChecking(string fileName)
{
    ifstream fin;
    int totalWordsCounter = 0;   // Tracks number of misspelled words
    fin.open(fileName.data());
    string importedWord;

    // Checks if the file exists
    if(!fin)
    {

        cout << "File could not be found." << endl;
        return failure;
    }

    while(fin >> importedWord)
    {

        if(!SearchForWord(importedWord))
        {
            cout << importedWord << endl;
            totalWordsCounter++;
        }
    }

    cout << endl << totalWordsCounter << " misspelled words found!" << endl;

    return success;
}

bool Dictionary::InsertWordsIntoDict(string fileName)
{
    ifstream fin;
    fin.open(fileName.data());
    string importedWord;

    if(!fin)
    {
        // Checks for file existence
        cout << "File could not be found." << endl;
        return failure;
    }

    while(!fin.eof())
    {
        fin >> importedWord;
        if(!SearchForWord(importedWord))
            AddAWord(importedWord);
    }

    return success;
}

string Dictionary::fileNamer(const string word)
{
    // Takes the first letter of the word, capitalizes it, and appends .txt
    return string(1, toupper(word[0])) + ".txt";
}


void Dictionary::ProcessTransactionFile()
{
    ifstream fin;
    fin.open("commands.txt");
    string commands, parameter;

    while(!fin.eof())
    {
        fin >> commands >> parameter;
        printDivider(commands, parameter);   // Prints a divider with the command
        // and parameter for visual clarity
        if(commands == "AddW")
        {
            // AddW - add a word to the dictionary.
            if(AddAWord(parameter))
                cout << """ << parameter << "" added!" << endl << endl;
            else
                cout << """ << parameter << "" not added." << endl << endl;
        }
        else if(commands == "DeleteW")
        {
            // DeleteW - deletes a word from the dictionary.
            if(DeleteAWord(parameter))
                cout << """ << parameter << "" removed!" << endl << endl;
            else
                cout << """ << parameter << "" not removed." << endl << endl;
        }
        else if(commands == "SearchW")
        {
            // SearchW - Checks if a word is already in the dictionary.
            if(SearchForWord(parameter))
                cout << """ << parameter << "" found in the dictionary!"
                     << endl << endl;
            else
                cout << """ << parameter << "" is not in the dictionary."
                     << endl << endl;
        }
        else if(commands == "PrintF")
        {
            // Printf - Prints an entire file (or all words starting with
            // a given letter.
            PrintAFileInDict(parameter);
        }
        else if(commands == "SpellCheck")
        {
            // SpellCheck - Check if words in a given file are in the dictionary
            cout << "The following words are misspelled: " << endl << endl;
            SpellChecking(parameter);
        }
        else if(commands == "InsertF")
        {
            // InsertWordsIntoDict - Import words of a file into the dictionary.
            if(InsertWordsIntoDict(parameter))
                cout << "Successful import of " << parameter << endl << endl;
            else
                cout << "Import was not successful." << endl << endl;

        }
        else
            cout << "Not a valid command! Check the source file. " << endl;
    }
}


void Dictionary::printDivider(const string command, const string parameter)
{
    // Prints 35 dashes
    for(int i = 0; i < 35; i++)
        cout << "-";

    // Prints the command and the parameter given
    cout << setw(10) << right << command << " "
         << setw(10) << left << parameter;

    // Prints another 35 dashes
    for(int i = 0; i < 35; i++)
        cout << "-";

    cout << endl << endl;
}


string Dictionary::wordToLower(const string word)
{
    string newWord = "";

    for (int i = 0; i < word.length(); i++)
    {
        newWord += tolower(word[i]);
    }

    return newWord;
}

#endif

-----------------------------------------------------------------------
Dictionary.h
-----------------------------------------
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <string>
using namespace std;
#define LETTERSOFTHEALPHABET 26  
class Dictionary
{
private:
    const    int maxWordsInDict;
    const   int maxWordsPerFile;

    // Total number of words in a file (0-25 for a-z) and dictionary
    int   totalWordsInDict;
    int   numOfWordsInFile[LETTERSOFTHEALPHABET];

    // Macros for true/false for readability
    static   bool failure;
    static   bool success;

    // Returns the file name for any given word
    string fileNamer(const string);

    // Prints a divider for visual organization
    void printDivider(const string command, const string parameter);

    // Converts every letter in a word to lower case
    string wordToLower(const string);

public:

    Dictionary();
    Dictionary(int dictMaxWords, int fileMaxWords);
    ~Dictionary();   // Destructor
    bool AddAWord(string word);   // Adds a word to the dictionary
    bool DeleteAWord(string word);   // Removes a word from the dictionary
    bool SearchForWord(string word);   // Checks if a word is in the dict.
    bool PrintAFileInDict(string filename);   // Prints all words in a file
    bool SpellChecking(string fileName);   // Checks if a given file has
    // any incorrectly spelled words
    bool InsertWordsIntoDict(string fileName);   // Imports words into the
    // dictionary from a file
    void ProcessTransactionFile();   // Follows instructions from a file
};

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote