Write an object-oriented program to create a dictionary. The dictionary has 26 f
ID: 3591517 • Letter: W
Question
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
#include
#include
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;
}
/*
Other Comments:
1) Do not worry about punctuations. Your program should only consider words and ignore all punctuations.
2) All words should be in lower case before it is placed in the dictionary
3) Do not write 26 statements to open/close A.txt to Z.txt files.
4) When a file name is a variable, you need to open it as follows:
fin.open(fileName.data());
*/
Place the following in your Transaction file
AddW student
AddW teacher
AddW cs211
InsertF data.txt
PrintF S.txt
DeleteW student
AddW hello
SpellCheck myEssay.txt
DeleteW play
SearchW medical
SearchW cs211
PrintF C.txt
PrintF H.txt
PrintF T.txt
Place the following in your data.txt file
Current approaches to enhancing concurrency in database systems have been focused on developing new transaction models that typically demand changes to either the atomicity consistency or isolation properties of the transactions themselves Indeed much of this work has been successful but most of these attempts suffer from being either computationally infeasible or requiring the transaction designer be sufficiently knowledgeable to determine a priori how the application semantics must interface with the transaction model
Our approach exploits an object-oriented world but is different than these others in that the transaction designer is not expected to have intimate application knowledge this load is placed on the transaction system which must be able to determine by static analysis how to maintain database consistency This paper describes an overall architectural model to facilitate multiversion objects that are explicitly designed to enhance concurrency Within the context of concurrency the key aspects addressed by this paper are as follows
First A transaction model is introduced to show how transactions are managed in our multiversion environment
Second A new correctness criterion is described that emits more histories than conflict serializability and is computationally tractable
Third an architectural model is developed to support multiversioning that provides the well-known ACID transaction properties These build a framework for the implementation of an optimistic concurrency control algorithm Further they exploit related issues required to generate reconciliation procedures which have an important role for providing a reliable system
Place the following in your myEssay.txt file
Concrrent approaches to enhancing concurrency in database systems have been focused on developing new transaction models that typically demand changes to either the atomicity consistency or isolation properties of the transactions themeselves Indeed much of this work has been successful but most of these attempts suffer from being either computationally infeasible or requiring the transaction designer be sufficiently knowledgeable to determine a priori how the application semantics must interface with the transaction model
Our approach exploits an object-oriented world but is diferent than these others in that the transaction designer is not expected to have intimate application knowledge this load is placed on the transaction system wich must be able to determine by static analyziz how to maintain database consistency This paper describes an overall architectural model to facilitate multiversion objects that are explicitly designed to enhanc concurrency Within the context of concurrency the key aspects addressed by this paper are as follows
First A transactian model is introduced to show how transactions are managed in our multiversion environment
Second A new correctness criterion is described that emits more histories than conflict serializability and is computationally tractable
Third an architectural model is developed to support multiversioning that provides the well-known ACID transaction properties These build a framework for the implementation of an optimistic concurrency control algorithm Further they exploit relatted issues required to generate reconciliation procedures which have an important role for providing a reliable system
Explanation / Answer
main.c
#include <iostream>
#include <string>
#include "dictionary.h"
using namespace std;
int main()
{
Dictionary DictForCS211;
DictForCS211.ProcessTransactionFile();
return 0;
}
Dictionary.c
#ifndef DICTIONARY_C
#define DICTIONARY_C
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "dictionary.h"
using namespace std;
bool Dictionary::failure = false; //globally set failure to 0 boolean
bool Dictionary::success = true; //globally set success to 1 boolean
//default constructor
Dictionary::Dictionary(): maxWordsInDict(10000), maxWordsPerFile(100), totalWordsInDict(0)
{
for(int i = 0; i < 26; i++)
{
numOfWordsInFile[i] = 0; //loop through array and set each slot to 0 (0 = a, 26 = z in dictionary)
}
}
//constructor if passed values dictMaxWords and fileMaxWords (2)
Dictionary::Dictionary(int dictMaxWords, int fileMaxWords): maxWordsInDict(dictMaxWords), maxWordsPerFile(fileMaxWords), totalWordsInDict(0)
{
for(int i = 0; i < 26; i++)
{
numOfWordsInFile[i] = 0; //loop through array and set each slot to 0 (0 = a, 26 = z in dictionary)
}
}
bool Dictionary::AddAWord(string word)
{
string fileName = "#.txt"; //make a filename string with a temporary first letter
fileName[0] = toupper(word[0]); //replace # with first letter of word
ofstream fout;
fout.open(fileName.data(), ios::app); //open file corresponding to first letter of word
for(int i = 0; word[i] != ''; i++) //convert word to full lower case
{
word[i] = tolower(word[i]);
}
int ascii = word[0] - 97; //convert first letter of word to ascii value 0 - 25
if(!Dictionary::SearchForWord(word)) //confirm that word is not in the dictionary
{
if(totalWordsInDict <= maxWordsInDict && numOfWordsInFile[ascii] <= maxWordsPerFile) //check both limits
{
fout << word << endl; //push word to dictionary file
numOfWordsInFile[ascii]++; //increment dictionary letter total by 1
totalWordsInDict++; //increment total words in dictionary by 1
fout.close();
return Dictionary::success;
}
else //limit has been reached
{
cout << " ERROR: The word '" << word << "' could not be added! Capacity has been reached. ";
}
}
fout.close();
return Dictionary::failure;
}
bool Dictionary::DeleteAWord(string word)
{
vector<string> wordVector;
string wd;
string fileName = "#.txt";
fileName[0] = toupper(word[0]);
ifstream fin;
fin.open(fileName.data()); //open file corresponding to first letter of word
if(!Dictionary::SearchForWord(word)) //if word doesn't exist in dictionary, return failure
{
cout << " The word '" << word << "' doesn't exist in the dictionary! ";
return Dictionary::failure;
}
while(fin>>wd) //push words into vector one by one
{
for(int i = 0; word[i] != ''; i++) //convert word to full lower case
{
word[i] = tolower(word[i]);
}
wordVector.push_back(wd); //insert into vector
}
fin.close(); //file no longer needed, close
for(int i = 0; i < wordVector.size(); i++) //check every word in the vector for match
{
if(wordVector[i] == word) //if match
{
cout << " Removing " << word << " from dictionary. ";
wordVector.erase(wordVector.begin() + i); //remove word from vector
int ascii = word[0] - 97; //determine ascii value of first letter
numOfWordsInFile[ascii]--; //decrement total number of words for corresponding file letter
totalWordsInDict--; //decrement total words
i--; //jump back one value since a vector slot has been deleted
}
}
ofstream fout;
fout.open(fileName.data());
for(int i = 0; i < wordVector.size(); i++) //push modified vector back into file
{
fout << wordVector[i] << endl;
}
fout.close();
return Dictionary::success;
}
bool Dictionary::SearchForWord(string word)
{
ifstream fin2;
string fileName = "#.txt";
string wd;
fileName[0] = toupper(word[0]);
fin2.open(fileName.data());
if(!fin2) //the file doesn't exist
{
return Dictionary::failure;
}
while(!fin2.eof())
{
fin2 >> wd; //push next word into wd
for(int i = 0; word[i] != ''; i++) //convert word to full lower case
{
word[i] = tolower(word[i]);
}
if(wd == word) //if word matches one already in file return true
{
fin2.close();
return Dictionary::success;
}
}
fin2.close();
return Dictionary::failure; //if file exists but no word match
}
bool Dictionary::SpellChecking(string fileName)
{
string wd;
ifstream fin;
fin.open(fileName.data());
if(!fin) //determine if file exists
{
cout << " ERROR: The file '" << fileName << "' doesn't exist! ";
return Dictionary::failure;
}
while(!fin.eof()) //check file for word match
{
fin >> wd;
for(int i = 0; wd[i] != ''; i++) //convert wd to full lower case
{
wd[i] = tolower(wd[i]);
}
if(!SearchForWord(wd)) //if no match found, display error
cout << " ERROR: The word '" << wd << "' is misspelled! ";
}
fin.close();
return Dictionary::success;
}
bool Dictionary::InsertWordsIntoDict(string fileName)
{
string wd;
ifstream fin;
if(!fin) //determine if file exists
{
cout << " ERROR: The file '" << fileName << "' doesn't exist! ";
return Dictionary::failure;
}
fin.open(fileName.data());
while(!fin.eof()) //push all words to AddAWord for dictionary entry
{
fin >> wd;
AddAWord(wd);
}
fin.close();
return Dictionary::success;
}
bool Dictionary::PrintAFileInDict(string fileName)
{
vector<string> myVector;
string word;
int count = 0; //total words in file
ifstream fin;
fin.open(fileName.data());
if(!fin) //determine if file exists
{
cout << " ERROR: The file '" << fileName << "' doesn't exist! ";
return Dictionary::failure;
}
while(!fin.eof()) //push words into vector
{
fin >> word;
myVector.push_back(word);
count++; //increment count
}
cout << " Words beginning with '" << word[0] << "': ";
for(int i = 1;i <= count; i++) //output words with only 5 words per line
{
cout << myVector[i - 1] << " ";
if(i%5 == 0) //if a 5th word has been output, go to next line
{
cout << " ";
}
}
cout << " "; //space for readability
fin.close();
return Dictionary::success;
}
void Dictionary::ProcessTransactionFile()
{
string transFile; //user's entered file name
string command; //command in transaction file (left column)
string data; //data related to command in transaction file (right column)
cout << "Please enter the name of the transaction file you would like to use: ";
cin >> transFile;
ifstream transFin; //transaction file
transFin.open(transFile.data());
if(!transFin) //the file doesn't exist -> exit
{
cout << " ERROR: The file " << transFile << " doesn't exist! ";
return;
}
else
{
cout << " File opened successfully! ";
}
while(!transFin.eof()) //look at command and proceed to corresponding function with data
{
transFin >> command >> data;
if(command == "AddW")
{
AddAWord(data);
}
else if(command == "DeleteW")
{
DeleteAWord(data);
}
else if(command == "SearchW")
{
if(SearchForWord(data) == false) //determine if word exists and state result outside of inner functions
cout << " The word '" << data << "' doesn't exist in the dictionary! ";
else
cout << " The word '" << data << "' exists in the dictionary. ";
}
else if(command == "PrintF")
{
PrintAFileInDict(data);
}
else if(command == "SpellCheck")
{
SpellChecking(data);
}
else if(command == "InsertF")
{
InsertWordsIntoDict(data);
}
else
{
cout << " ERROR: Command unknown! ";
}
}
transFin.close();
return;
}
#endif
dictionary.h
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <iostream>
#include <string>
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();
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.