SlideShare a Scribd company logo
1 of 13
Download to read offline
This project is the first projects you will be working on this quarter. There are a number of
objectives to this assignment. First, to start gaining experience in developing programs in C++.
Second, in case you haven’t already you are going to work in pairs to get the sense of
collaborative development of software. Third, because you are allowed to use any references you
find online, this assignment will help you get a sense for just how many sources for
programming in C++ are available via the web. Project specification: In this project you are
going to implement a word search engine using C++. The search engine would help users to
count number of occurrence of a word among given set of text files. Input specification: The
program takes as an input a set of files, containing of multiple words. Words can appear multiple
times in a single file or in different files. The input files should be stored in a dedicated directory
(e.g. /cs/class/cs24/project1/input) and loaded in memory upon program execution. Program
functionality: Upon execution, the program should load all the words from the input files in the
memory. The program should run until the user explicitly specifies “exit”. The following
functionality should be supported: 1. Count how many times a user specified word appears in the
files. 3. Display the name of input files and corresponding number of occurrences of the
specified word. Upon execution of you program, you should specify as input parameter the path
to a directory containing the input files. Below you can find the format specification:
$./wordsearch Examples for execution format: $./wordsearch /cs/class/cs24/project1/input Enter
word: cat If executed as specified above, the program should return the name of files that contain
word “cat” and the number of occurrences of the word in each file Implementation requirements:
First, you need to implement a class for an abstract data type, in which you are going to store
files in memory. This step is very specific, depending on the functionality of the program you are
developing. For the current implementation you are required to use linked lists. A basic layout of
the data structure you would need to implement is illustrated on the figure below: You need to
declare a class Word that would store a word and a pointer to the beginning of a linked list and
another class File that would store a file-name and number of times that the word occurred in this
file. The process of “loading files in the memory” consists of (i) creating an object of type Word
for each new word that occurs in the set of input files, (ii) appending this object to a linked list
(e.g. the green linked list from the picture), (iii) creating an object File for each occurrence of the
word in a file and (iv) updating the corresponding (blue) linked list with the object File. Once
you have the files loaded in such structure, the searching would be as easy as finding the queried
word in the “green” linked list and tracing in which files does it occur by going over the
corresponding “blue” linked list. To start gaining experience in how to structure your C++
projects, you are required to split the different aspects of this program and implement them in
separate files. Then you will have to put them all back together to achieve the program
functionality. By now, you should be familiar with the concept of .h and .cpp files (if not, it is a
good moment to familiarize yourselves). You will need to create several .h and .cpp files for this
project. We will help guiding you through this process. First you need to identify the important
object-types and methods you will need for your implementation. In this project, your main
object types are going to be class Word and class File. Go ahead and create a file called
itemtype.h and declare class File in it. Then create another file called itemtype.cpp and define
your class File in it. These two files are going to be related to objects from the “blue” lists on the
picture. The next step is to implement the functionality for creating the “blue” lists. For this
purpose, you need to create two more files – list.h and list.cpp. In list.h declare all the methods
needed for building a “blue” list and in list.cpp define these methods. Next, you need to
implement the functionality related to objects like these in the “green” list. Create two more files
– word.h and word.cpp, declare class Word and all methods to this class in word.h and define
them in word.cpp. Now what's left is to put it all together by writing a main function that utilizes
both file and word. To do so, create wordsearch.h and wordsearch.cpp, declare your main
function in wordsearch.h and define it in wordsearch.cpp. So all in all you will need eight files:
wordsearch.h, wordsearch.cpp, itemtype.h, itemtype.cpp, list.h, list.cpp, word.h, word.cpp.
Instructions for compilation: Your program should compile on a CSIL machine with the
following command without any errors or warnings. $ g++ -o wordsearch wordsearch.cpp
itemtype.cpp list.cpp word.cpp
Solution
Given below are the files needed for the question. The images mentioned in the question are not
uploaded. So based only on the description, the file structures have been come up. Please don't
forget to rate the answer if it helped. Thank you.
list.h
#ifndef list_h
#define list_h
#include
using namespace std;
template
struct Node
{
T data;
struct Node* next;
Node(const T &data, Node* next = nullptr)
{
this->data = data;
this->next = next;
}
};
template
class LinkedList
{
Node* head;
Node* tail;
int count;
public:
LinkedList();
void append(const T &data);
T& get(int i); //get item at index i
int size(); // number of items in list
void print() const;
~LinkedList();
};
#include "list.cpp"
#endif /* list_h */
list.cpp
#ifndef list_cpp
#define list_cpp
#include "list.h"
template
LinkedList::LinkedList()
{
head = tail = nullptr;
count = 0;
}
template
void LinkedList::append(const T &data)
{
Node* n = new Node(data, nullptr);
if(head == nullptr)
head = tail = n;
else
{
tail->next = n;
tail = n;
}
count++;
}
template
int LinkedList::size()
{
return count;
}
template
T& LinkedList::get(int index)
{
Node *n = head;
for(int i = 0; n != nullptr && i < index; i++)
n = n->next;
return n->data;
}
template
void LinkedList::print() const
{
Node *curr = head;
while(curr != nullptr)
{
cout << curr->data << endl;
curr = curr->next;
}
cout << endl;
}
template
LinkedList::~LinkedList()
{
Node *curr = head;
Node *next;
while(curr != nullptr)
{
next = curr->next;
delete curr;
curr = next;
}
}
#endif
itemtype.h
#ifndef itemtype_h
#define itemtype_h
#include
using namespace std;
class File
{
string filepath;
int count;
public:
File(){}
File(string filepath);
void incrementCount();
int getCount();
string getFilepath();
friend ostream& operator << (ostream &out, const File &f);
};
#endif /* itemtype_h*/
itemtype.cpp
#include "itemtype.h"
File::File(string filepath)
{
this->filepath = filepath;
count = 1;
}
void File::incrementCount()
{
count++;
}
int File::getCount()
{
return count;
}
string File::getFilepath()
{
return filepath;
}
ostream& operator << (ostream &out, const File &f)
{
cout << f.filepath << " [" << f.count << "]";
return out;
}
word.h
#ifndef word_h
#define word_h
#include
using namespace std;
#include "itemtype.h"
#include "list.h"
class Word
{
string word;
int find(string filepath);
public:
Word(){};
Word(string word);
LinkedList files;
void addFile(string filepath);
string getWord();
friend ostream& operator << (ostream &out, const Word &w);
};
#endif /* word_h */
word.cpp
#include "word.h"
Word::Word(string word )
{
this->word = word;
}
void Word::addFile(string filepath)
{
int idx = find(filepath);
if(idx == -1)
files.append(File(filepath));
else
{
File & file = files.get(idx);
file.incrementCount();
}
}
int Word::find(string filepath)
{
int sz = files.size();
for(int i =0; i < sz; i ++)
{
if(files.get(i).getFilepath() == filepath)
return i;
}
return -1;
}
string Word::getWord()
{
return word;
}
ostream& operator << (ostream &out, const Word &w)
{
cout << "Word: " << w.word << endl;
w.files.print();
return out;
}
wordsearch.h
#ifndef wordsearch_h
#define wordsearch_h
#include "word.h"
class WordSearch
{
LinkedList words;
public:
WordSearch(){}
bool loadFiles(char* directory);
void parseFile(string filepath);
int findWord(string word);
void addWord(string word, string filepath);
void printWordStats(string word);
};
int main(int argc, char *argv[]);
#endif /* wordsearch_h */
wordsearch.cpp
#include "wordsearch.h"
#include
#include
using namespace std;
bool WordSearch::loadFiles(char* directory)
{
DIR *dir;
struct dirent *ent;
if ((dir = opendir(directory)) != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
if(ent->d_type == DT_REG)
{
string fpath(directory);
fpath =fpath + "/" + ent->d_name;
parseFile(fpath);
}
}
closedir (dir);
return true;
}
else
{
cout << "Could not open directory: " << directory << endl;
return false;
}
}
void WordSearch::parseFile(string filepath)
{
ifstream infile(filepath.c_str());
string word;
if(!infile.is_open())
{
cout << "Error opening file: " << filepath << endl;
return;
}
int i = 0;
while(infile >> word)
{
addWord(word, filepath);
}
infile.close();
}
int WordSearch::findWord(string word)
{
int sz = words.size();
for(int i =0; i < sz; i ++)
{
if(words.get(i).getWord() == word)
return i;
}
return -1;
}
void WordSearch::addWord(string word, string filepath)
{
int idx = findWord(word);
if(idx == -1)
{
Word w(word);
words.append(w);
idx = findWord(word);
}
Word &w = words.get(idx);
w.addFile(filepath);
}
void WordSearch::printWordStats(string word)
{
int idx = findWord(word);
if(idx == -1)
cout << word << " not found!" << endl;
else
{
cout << words.get(idx) << endl;
}
}
int main(int argc, char *argv[])
{
WordSearch ws;
if(argc != 2)
{
cout << "Usage: " << argv[0] << " " << endl;
return 1;
}
if(ws.loadFiles(argv[1]))
{
string input = "";
while(true)
{
cout << "Enter a word ( type exit to quit): ";
cin >> input;
if(input == "exit")
break;
ws.printWordStats(input);
}
}
}
There are 2 files text1.txt and text2.txt in the specified directory of the program. The contents of
the input files are as follows-
input file: text1.txt
This project is the first projects you will be working on this quarter.
There are a number of objectives to this assignment
First, to start gaining experience in developing programs in C++.
Second, in case you haven’t already you are going to work in pairs to get the sense of
collaborative development of software. Third, because you are allowed to use any references
you find online
text2.txt
This assignment will help you get a sense for just how many sources for programming in C++
are available via the web.
Project specification: In this project you are going to implement a word search engine using
C++.
The search engine would help users to count number of occurrence of a word among given set
of text files.
Input specification: The program takes as an input a set of files, containing of multiple words.
Words can appear multiple times in a single file or in different files.
The input files should be stored in a dedicated directory (e.g. /cs/class/cs24/project1/input) and
loaded in memory
upon program execution. Program functionality: Upon execution, the program should load all
the words from the input
files in the memory. The program should run until the user explicitly specifies “exit”.
The following functionality should be supported: 1.
Count how many times a user specified word appears in the files. 3. Display the name of input
files and
output
Word: will
/Users/raji/Documents/sample/text1.txt [1]
/Users/raji/Documents/sample/text2.txt [1]
Enter a word ( type exit to quit): the
Word: the
/Users/raji/Documents/sample/text1.txt [2]
/Users/raji/Documents/sample/text2.txt [8]
Enter a word ( type exit to quit): The
Word: The
/Users/raji/Documents/sample/text2.txt [5]
Enter a word ( type exit to quit): program
Word: program
/Users/raji/Documents/sample/text2.txt [4]
Enter a word ( type exit to quit): Count
Word: Count
/Users/raji/Documents/sample/text2.txt [1]
Enter a word ( type exit to quit): to
Word: to
/Users/raji/Documents/sample/text1.txt [5]
/Users/raji/Documents/sample/text2.txt [2]
Enter a word ( type exit to quit): exit

More Related Content

Similar to This project is the first projects you will be working on this quart.pdf

Php interview-questions and answers
Php interview-questions and answersPhp interview-questions and answers
Php interview-questions and answerssheibansari
 
File handling.pptx
File handling.pptxFile handling.pptx
File handling.pptxVishuSaini22
 
StdAfx.h for Novices
StdAfx.h for NovicesStdAfx.h for Novices
StdAfx.h for NovicesAndrey Karpov
 
Assignment 1 MapReduce With Hadoop
Assignment 1  MapReduce With HadoopAssignment 1  MapReduce With Hadoop
Assignment 1 MapReduce With HadoopAllison Thompson
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing filesMukesh Tekwani
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptxMugilvannan11
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6Jadavsejal
 
Question 1 has already been posted to Chegg and I am waiting for the.pdf
Question 1 has already been posted to Chegg and I am waiting for the.pdfQuestion 1 has already been posted to Chegg and I am waiting for the.pdf
Question 1 has already been posted to Chegg and I am waiting for the.pdfanjandavid
 
C++ Programming Class Creation Program Assignment Instructions
C++ Programming Class Creation Program Assignment InstructionsC++ Programming Class Creation Program Assignment Instructions
C++ Programming Class Creation Program Assignment InstructionsTawnaDelatorrejs
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxRohitKumar639388
 

Similar to This project is the first projects you will be working on this quart.pdf (20)

Php interview-questions and answers
Php interview-questions and answersPhp interview-questions and answers
Php interview-questions and answers
 
File handling.pptx
File handling.pptxFile handling.pptx
File handling.pptx
 
StdAfx.h for Novices
StdAfx.h for NovicesStdAfx.h for Novices
StdAfx.h for Novices
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
Assignment 1 MapReduce With Hadoop
Assignment 1  MapReduce With HadoopAssignment 1  MapReduce With Hadoop
Assignment 1 MapReduce With Hadoop
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
 
A05
A05A05
A05
 
Python training
Python trainingPython training
Python training
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Basic c
Basic cBasic c
Basic c
 
Question 1 has already been posted to Chegg and I am waiting for the.pdf
Question 1 has already been posted to Chegg and I am waiting for the.pdfQuestion 1 has already been posted to Chegg and I am waiting for the.pdf
Question 1 has already been posted to Chegg and I am waiting for the.pdf
 
C tutorials
C tutorialsC tutorials
C tutorials
 
C++ Programming Class Creation Program Assignment Instructions
C++ Programming Class Creation Program Assignment InstructionsC++ Programming Class Creation Program Assignment Instructions
C++ Programming Class Creation Program Assignment Instructions
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptx
 
lab4_php
lab4_phplab4_php
lab4_php
 

More from eyewaregallery

in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfeyewaregallery
 
In addition to the fossil record, what other kinds of evidence reinfo.pdf
In addition to the fossil record, what other kinds of evidence reinfo.pdfIn addition to the fossil record, what other kinds of evidence reinfo.pdf
In addition to the fossil record, what other kinds of evidence reinfo.pdfeyewaregallery
 
How much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdf
How much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdfHow much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdf
How much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdfeyewaregallery
 
I am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdfI am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdfeyewaregallery
 
How do teams bring value to an organizationHow are high performin.pdf
How do teams bring value to an organizationHow are high performin.pdfHow do teams bring value to an organizationHow are high performin.pdf
How do teams bring value to an organizationHow are high performin.pdfeyewaregallery
 
how can we use empricism to figure out if a fact is trueSolutio.pdf
how can we use empricism to figure out if a fact is trueSolutio.pdfhow can we use empricism to figure out if a fact is trueSolutio.pdf
how can we use empricism to figure out if a fact is trueSolutio.pdfeyewaregallery
 
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdf
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdfGene RegulationFunction of transcription factor domains, i.e. DNA.pdf
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdfeyewaregallery
 
For each step of DNA replication, predict the outcome ifconcentra.pdf
For each step of DNA replication, predict the outcome ifconcentra.pdfFor each step of DNA replication, predict the outcome ifconcentra.pdf
For each step of DNA replication, predict the outcome ifconcentra.pdfeyewaregallery
 
Explain THREE unique characteristics of fungi.SolutionThree un.pdf
Explain THREE unique characteristics of fungi.SolutionThree un.pdfExplain THREE unique characteristics of fungi.SolutionThree un.pdf
Explain THREE unique characteristics of fungi.SolutionThree un.pdfeyewaregallery
 
Explain the basic relationship between humans and microorganisms.pdf
Explain the basic relationship between humans and microorganisms.pdfExplain the basic relationship between humans and microorganisms.pdf
Explain the basic relationship between humans and microorganisms.pdfeyewaregallery
 
discuss the rationale for the global harmonization of financial repo.pdf
discuss the rationale for the global harmonization of financial repo.pdfdiscuss the rationale for the global harmonization of financial repo.pdf
discuss the rationale for the global harmonization of financial repo.pdfeyewaregallery
 
Describe the need to multitask in BBC (behavior-based control) syste.pdf
Describe the need to multitask in BBC (behavior-based control) syste.pdfDescribe the need to multitask in BBC (behavior-based control) syste.pdf
Describe the need to multitask in BBC (behavior-based control) syste.pdfeyewaregallery
 
A spinner with 7 equally sized slices is shown below. The dial is spu.pdf
A spinner with 7 equally sized slices is shown below. The dial is spu.pdfA spinner with 7 equally sized slices is shown below. The dial is spu.pdf
A spinner with 7 equally sized slices is shown below. The dial is spu.pdfeyewaregallery
 
ble below to calculate the Gross Domestic Product uning the ral Gro.pdf
ble below to calculate the Gross Domestic Product uning the ral Gro.pdfble below to calculate the Gross Domestic Product uning the ral Gro.pdf
ble below to calculate the Gross Domestic Product uning the ral Gro.pdfeyewaregallery
 
1.The limbic systemA. matures before the prefrontal cortexB. is.pdf
1.The limbic systemA. matures before the prefrontal cortexB. is.pdf1.The limbic systemA. matures before the prefrontal cortexB. is.pdf
1.The limbic systemA. matures before the prefrontal cortexB. is.pdfeyewaregallery
 
Why is leadership a critical factor in implementing change and qua.pdf
Why is leadership a critical factor in implementing change and qua.pdfWhy is leadership a critical factor in implementing change and qua.pdf
Why is leadership a critical factor in implementing change and qua.pdfeyewaregallery
 
Why are there bubbles in beer and champagneSolution Bubbles of .pdf
Why are there bubbles in beer and champagneSolution  Bubbles of .pdfWhy are there bubbles in beer and champagneSolution  Bubbles of .pdf
Why are there bubbles in beer and champagneSolution Bubbles of .pdfeyewaregallery
 
Which of the following is true of a traditional master budget a. It.pdf
Which of the following is true of a traditional master budget a. It.pdfWhich of the following is true of a traditional master budget a. It.pdf
Which of the following is true of a traditional master budget a. It.pdfeyewaregallery
 
Which ethnic groups were a part of the former YugoslaviaCroats, S.pdf
Which ethnic groups were a part of the former YugoslaviaCroats, S.pdfWhich ethnic groups were a part of the former YugoslaviaCroats, S.pdf
Which ethnic groups were a part of the former YugoslaviaCroats, S.pdfeyewaregallery
 
What would the components of the development be in an early learning.pdf
What would the components of the development be in an early learning.pdfWhat would the components of the development be in an early learning.pdf
What would the components of the development be in an early learning.pdfeyewaregallery
 

More from eyewaregallery (20)

in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
 
In addition to the fossil record, what other kinds of evidence reinfo.pdf
In addition to the fossil record, what other kinds of evidence reinfo.pdfIn addition to the fossil record, what other kinds of evidence reinfo.pdf
In addition to the fossil record, what other kinds of evidence reinfo.pdf
 
How much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdf
How much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdfHow much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdf
How much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdf
 
I am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdfI am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdf
 
How do teams bring value to an organizationHow are high performin.pdf
How do teams bring value to an organizationHow are high performin.pdfHow do teams bring value to an organizationHow are high performin.pdf
How do teams bring value to an organizationHow are high performin.pdf
 
how can we use empricism to figure out if a fact is trueSolutio.pdf
how can we use empricism to figure out if a fact is trueSolutio.pdfhow can we use empricism to figure out if a fact is trueSolutio.pdf
how can we use empricism to figure out if a fact is trueSolutio.pdf
 
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdf
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdfGene RegulationFunction of transcription factor domains, i.e. DNA.pdf
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdf
 
For each step of DNA replication, predict the outcome ifconcentra.pdf
For each step of DNA replication, predict the outcome ifconcentra.pdfFor each step of DNA replication, predict the outcome ifconcentra.pdf
For each step of DNA replication, predict the outcome ifconcentra.pdf
 
Explain THREE unique characteristics of fungi.SolutionThree un.pdf
Explain THREE unique characteristics of fungi.SolutionThree un.pdfExplain THREE unique characteristics of fungi.SolutionThree un.pdf
Explain THREE unique characteristics of fungi.SolutionThree un.pdf
 
Explain the basic relationship between humans and microorganisms.pdf
Explain the basic relationship between humans and microorganisms.pdfExplain the basic relationship between humans and microorganisms.pdf
Explain the basic relationship between humans and microorganisms.pdf
 
discuss the rationale for the global harmonization of financial repo.pdf
discuss the rationale for the global harmonization of financial repo.pdfdiscuss the rationale for the global harmonization of financial repo.pdf
discuss the rationale for the global harmonization of financial repo.pdf
 
Describe the need to multitask in BBC (behavior-based control) syste.pdf
Describe the need to multitask in BBC (behavior-based control) syste.pdfDescribe the need to multitask in BBC (behavior-based control) syste.pdf
Describe the need to multitask in BBC (behavior-based control) syste.pdf
 
A spinner with 7 equally sized slices is shown below. The dial is spu.pdf
A spinner with 7 equally sized slices is shown below. The dial is spu.pdfA spinner with 7 equally sized slices is shown below. The dial is spu.pdf
A spinner with 7 equally sized slices is shown below. The dial is spu.pdf
 
ble below to calculate the Gross Domestic Product uning the ral Gro.pdf
ble below to calculate the Gross Domestic Product uning the ral Gro.pdfble below to calculate the Gross Domestic Product uning the ral Gro.pdf
ble below to calculate the Gross Domestic Product uning the ral Gro.pdf
 
1.The limbic systemA. matures before the prefrontal cortexB. is.pdf
1.The limbic systemA. matures before the prefrontal cortexB. is.pdf1.The limbic systemA. matures before the prefrontal cortexB. is.pdf
1.The limbic systemA. matures before the prefrontal cortexB. is.pdf
 
Why is leadership a critical factor in implementing change and qua.pdf
Why is leadership a critical factor in implementing change and qua.pdfWhy is leadership a critical factor in implementing change and qua.pdf
Why is leadership a critical factor in implementing change and qua.pdf
 
Why are there bubbles in beer and champagneSolution Bubbles of .pdf
Why are there bubbles in beer and champagneSolution  Bubbles of .pdfWhy are there bubbles in beer and champagneSolution  Bubbles of .pdf
Why are there bubbles in beer and champagneSolution Bubbles of .pdf
 
Which of the following is true of a traditional master budget a. It.pdf
Which of the following is true of a traditional master budget a. It.pdfWhich of the following is true of a traditional master budget a. It.pdf
Which of the following is true of a traditional master budget a. It.pdf
 
Which ethnic groups were a part of the former YugoslaviaCroats, S.pdf
Which ethnic groups were a part of the former YugoslaviaCroats, S.pdfWhich ethnic groups were a part of the former YugoslaviaCroats, S.pdf
Which ethnic groups were a part of the former YugoslaviaCroats, S.pdf
 
What would the components of the development be in an early learning.pdf
What would the components of the development be in an early learning.pdfWhat would the components of the development be in an early learning.pdf
What would the components of the development be in an early learning.pdf
 

Recently uploaded

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 

Recently uploaded (20)

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 

This project is the first projects you will be working on this quart.pdf

  • 1. This project is the first projects you will be working on this quarter. There are a number of objectives to this assignment. First, to start gaining experience in developing programs in C++. Second, in case you haven’t already you are going to work in pairs to get the sense of collaborative development of software. Third, because you are allowed to use any references you find online, this assignment will help you get a sense for just how many sources for programming in C++ are available via the web. Project specification: In this project you are going to implement a word search engine using C++. The search engine would help users to count number of occurrence of a word among given set of text files. Input specification: The program takes as an input a set of files, containing of multiple words. Words can appear multiple times in a single file or in different files. The input files should be stored in a dedicated directory (e.g. /cs/class/cs24/project1/input) and loaded in memory upon program execution. Program functionality: Upon execution, the program should load all the words from the input files in the memory. The program should run until the user explicitly specifies “exit”. The following functionality should be supported: 1. Count how many times a user specified word appears in the files. 3. Display the name of input files and corresponding number of occurrences of the specified word. Upon execution of you program, you should specify as input parameter the path to a directory containing the input files. Below you can find the format specification: $./wordsearch Examples for execution format: $./wordsearch /cs/class/cs24/project1/input Enter word: cat If executed as specified above, the program should return the name of files that contain word “cat” and the number of occurrences of the word in each file Implementation requirements: First, you need to implement a class for an abstract data type, in which you are going to store files in memory. This step is very specific, depending on the functionality of the program you are developing. For the current implementation you are required to use linked lists. A basic layout of the data structure you would need to implement is illustrated on the figure below: You need to declare a class Word that would store a word and a pointer to the beginning of a linked list and another class File that would store a file-name and number of times that the word occurred in this file. The process of “loading files in the memory” consists of (i) creating an object of type Word for each new word that occurs in the set of input files, (ii) appending this object to a linked list (e.g. the green linked list from the picture), (iii) creating an object File for each occurrence of the word in a file and (iv) updating the corresponding (blue) linked list with the object File. Once you have the files loaded in such structure, the searching would be as easy as finding the queried word in the “green” linked list and tracing in which files does it occur by going over the corresponding “blue” linked list. To start gaining experience in how to structure your C++ projects, you are required to split the different aspects of this program and implement them in separate files. Then you will have to put them all back together to achieve the program
  • 2. functionality. By now, you should be familiar with the concept of .h and .cpp files (if not, it is a good moment to familiarize yourselves). You will need to create several .h and .cpp files for this project. We will help guiding you through this process. First you need to identify the important object-types and methods you will need for your implementation. In this project, your main object types are going to be class Word and class File. Go ahead and create a file called itemtype.h and declare class File in it. Then create another file called itemtype.cpp and define your class File in it. These two files are going to be related to objects from the “blue” lists on the picture. The next step is to implement the functionality for creating the “blue” lists. For this purpose, you need to create two more files – list.h and list.cpp. In list.h declare all the methods needed for building a “blue” list and in list.cpp define these methods. Next, you need to implement the functionality related to objects like these in the “green” list. Create two more files – word.h and word.cpp, declare class Word and all methods to this class in word.h and define them in word.cpp. Now what's left is to put it all together by writing a main function that utilizes both file and word. To do so, create wordsearch.h and wordsearch.cpp, declare your main function in wordsearch.h and define it in wordsearch.cpp. So all in all you will need eight files: wordsearch.h, wordsearch.cpp, itemtype.h, itemtype.cpp, list.h, list.cpp, word.h, word.cpp. Instructions for compilation: Your program should compile on a CSIL machine with the following command without any errors or warnings. $ g++ -o wordsearch wordsearch.cpp itemtype.cpp list.cpp word.cpp Solution Given below are the files needed for the question. The images mentioned in the question are not uploaded. So based only on the description, the file structures have been come up. Please don't forget to rate the answer if it helped. Thank you. list.h #ifndef list_h #define list_h #include using namespace std; template struct Node { T data; struct Node* next;
  • 3. Node(const T &data, Node* next = nullptr) { this->data = data; this->next = next; } }; template class LinkedList { Node* head; Node* tail; int count; public: LinkedList(); void append(const T &data); T& get(int i); //get item at index i int size(); // number of items in list void print() const; ~LinkedList(); }; #include "list.cpp" #endif /* list_h */ list.cpp #ifndef list_cpp #define list_cpp #include "list.h" template LinkedList::LinkedList() { head = tail = nullptr; count = 0;
  • 4. } template void LinkedList::append(const T &data) { Node* n = new Node(data, nullptr); if(head == nullptr) head = tail = n; else { tail->next = n; tail = n; } count++; } template int LinkedList::size() { return count; } template T& LinkedList::get(int index) { Node *n = head; for(int i = 0; n != nullptr && i < index; i++) n = n->next; return n->data; } template void LinkedList::print() const { Node *curr = head; while(curr != nullptr) {
  • 5. cout << curr->data << endl; curr = curr->next; } cout << endl; } template LinkedList::~LinkedList() { Node *curr = head; Node *next; while(curr != nullptr) { next = curr->next; delete curr; curr = next; } } #endif itemtype.h #ifndef itemtype_h #define itemtype_h #include using namespace std; class File { string filepath; int count; public: File(){} File(string filepath); void incrementCount(); int getCount(); string getFilepath(); friend ostream& operator << (ostream &out, const File &f); };
  • 6. #endif /* itemtype_h*/ itemtype.cpp #include "itemtype.h" File::File(string filepath) { this->filepath = filepath; count = 1; } void File::incrementCount() { count++; } int File::getCount() { return count; } string File::getFilepath() { return filepath; } ostream& operator << (ostream &out, const File &f) { cout << f.filepath << " [" << f.count << "]"; return out; } word.h #ifndef word_h #define word_h #include using namespace std; #include "itemtype.h" #include "list.h" class Word {
  • 7. string word; int find(string filepath); public: Word(){}; Word(string word); LinkedList files; void addFile(string filepath); string getWord(); friend ostream& operator << (ostream &out, const Word &w); }; #endif /* word_h */ word.cpp #include "word.h" Word::Word(string word ) { this->word = word; } void Word::addFile(string filepath) { int idx = find(filepath); if(idx == -1) files.append(File(filepath)); else { File & file = files.get(idx); file.incrementCount(); } } int Word::find(string filepath) { int sz = files.size(); for(int i =0; i < sz; i ++) { if(files.get(i).getFilepath() == filepath) return i;
  • 8. } return -1; } string Word::getWord() { return word; } ostream& operator << (ostream &out, const Word &w) { cout << "Word: " << w.word << endl; w.files.print(); return out; } wordsearch.h #ifndef wordsearch_h #define wordsearch_h #include "word.h" class WordSearch { LinkedList words; public: WordSearch(){} bool loadFiles(char* directory); void parseFile(string filepath); int findWord(string word); void addWord(string word, string filepath); void printWordStats(string word); }; int main(int argc, char *argv[]); #endif /* wordsearch_h */ wordsearch.cpp #include "wordsearch.h" #include #include using namespace std; bool WordSearch::loadFiles(char* directory)
  • 9. { DIR *dir; struct dirent *ent; if ((dir = opendir(directory)) != NULL) { while ((ent = readdir (dir)) != NULL) { if(ent->d_type == DT_REG) { string fpath(directory); fpath =fpath + "/" + ent->d_name; parseFile(fpath); } } closedir (dir); return true; } else { cout << "Could not open directory: " << directory << endl; return false; } } void WordSearch::parseFile(string filepath) { ifstream infile(filepath.c_str()); string word; if(!infile.is_open()) { cout << "Error opening file: " << filepath << endl; return; } int i = 0; while(infile >> word) { addWord(word, filepath);
  • 10. } infile.close(); } int WordSearch::findWord(string word) { int sz = words.size(); for(int i =0; i < sz; i ++) { if(words.get(i).getWord() == word) return i; } return -1; } void WordSearch::addWord(string word, string filepath) { int idx = findWord(word); if(idx == -1) { Word w(word); words.append(w); idx = findWord(word); } Word &w = words.get(idx); w.addFile(filepath); } void WordSearch::printWordStats(string word) { int idx = findWord(word); if(idx == -1) cout << word << " not found!" << endl; else { cout << words.get(idx) << endl; } }
  • 11. int main(int argc, char *argv[]) { WordSearch ws; if(argc != 2) { cout << "Usage: " << argv[0] << " " << endl; return 1; } if(ws.loadFiles(argv[1])) { string input = ""; while(true) { cout << "Enter a word ( type exit to quit): "; cin >> input; if(input == "exit") break; ws.printWordStats(input); } } } There are 2 files text1.txt and text2.txt in the specified directory of the program. The contents of the input files are as follows- input file: text1.txt This project is the first projects you will be working on this quarter. There are a number of objectives to this assignment First, to start gaining experience in developing programs in C++. Second, in case you haven’t already you are going to work in pairs to get the sense of collaborative development of software. Third, because you are allowed to use any references you find online text2.txt This assignment will help you get a sense for just how many sources for programming in C++ are available via the web. Project specification: In this project you are going to implement a word search engine using C++.
  • 12. The search engine would help users to count number of occurrence of a word among given set of text files. Input specification: The program takes as an input a set of files, containing of multiple words. Words can appear multiple times in a single file or in different files. The input files should be stored in a dedicated directory (e.g. /cs/class/cs24/project1/input) and loaded in memory upon program execution. Program functionality: Upon execution, the program should load all the words from the input files in the memory. The program should run until the user explicitly specifies “exit”. The following functionality should be supported: 1. Count how many times a user specified word appears in the files. 3. Display the name of input files and output Word: will /Users/raji/Documents/sample/text1.txt [1] /Users/raji/Documents/sample/text2.txt [1] Enter a word ( type exit to quit): the Word: the /Users/raji/Documents/sample/text1.txt [2] /Users/raji/Documents/sample/text2.txt [8] Enter a word ( type exit to quit): The Word: The /Users/raji/Documents/sample/text2.txt [5] Enter a word ( type exit to quit): program Word: program /Users/raji/Documents/sample/text2.txt [4] Enter a word ( type exit to quit): Count Word: Count /Users/raji/Documents/sample/text2.txt [1] Enter a word ( type exit to quit): to Word: to