Well here is our assignment.
Code:
rite a C++ program that will build a Word Concordance for a series of text lines. A Concordance is basically an index of
words in a document, and a listing of their context (line numbers).
Then the program should read a series of lines of input from cin and keep track of the words that appear on the lines, and
the line numbers of where those words appear in the input stream. Upon completing the processing of the input, the
program should display an alphabetized listing of the words that were found, one word per line, and next to each word
will be a list of line numbers of where that word was found in the input.
A word, for the purpose of this program, is defined as a series of non-blank characters, with leading and trailing punctuation characters
(-,.;:?"'!@#$%^&*[]{}|) removed (note that embedded punctuation characters, such as Bob's and ten-thirty should be left in the word).
The words are to be saved without case-sensitivity; for example: Concordance ConCorDance CONCORDance concordance are all to be considered the same word.
The words are to be stored in a statically-declared array. You can assume that the maximum number of unique words that will be submitted is 200.
The words are to be stored in the array in sorted order, and you are to use the array-based binary search method as described in our course notes to locate words in the array.
For each word in the array, the list of line numbers is to be maintained by using a Linked-List Queue data structure, similar to what you used for Program 4.
Although the Queue isn't necessarily the most-appropriate data structure for this job, based on what we've already done with it, it should provide a simple interface to use.
and here is my code
Code:
#include <iostream>
#include <cstdlib>
#include <sstream>
using namespace std;
struct WORD{
string word;
int line;
}aword[200];
int binarySearch (string sortedArray[], int first, int last, string key)
{
while (first <= last) {
int mid = (first + last) / 2; // compute mid point.
if (key > sortedArray[mid])
first = mid + 1; // repeat search in top half.
else if (key < sortedArray[mid])
last = mid - 1; // repeat search in bottom half.
else
return mid; // found it. return position
}
return -(first + 1); // failed to find key
} // end binarySearch
int main() {
istringstream strLine;
string line, word;
int currentLine=0;
int i=0;
while (getline(cin,line)) {
++currentLine;
// clear the strstream and copy the entered line to it
strLine.clear();
strLine.str(line);
// now get each word-sequence from the strLine stream
while (strLine >> word)
{
//cout << currentLine << ": " << word << endl;
aword[i].word=word;
aword[i].line=currentLine;
i++;
}
}
for(i=0;aword[i].word!=""; i++)
{
cout<<aword[i].word<<endl;
}
// _________________________________
string word26[]={"a","f","j","z"};
int sizeValues = sizeof(word26) / sizeof (string);
for(int counter=0;counter<i;counter++)
{
cout << "Searching for " << aword[counter].word<< ": ";
int index = binarySearch(word26,0,sizeValues-1,aword[counter].word);
if (index >=0)
cout << "FOUND " << aword[counter].word << " in position " << index;
else{
cout << "Did not find " << aword[counter].word << "; it should go in position "
<< (-index-1);
//word26[-index-1]=aword[counter].word;
}
cout << endl << endl;
}
return 0;
}//end main
I created a second array word26[] and used to tell me where the values on my structure should go. but i'm stuck.
here is the output I get.
Code:
a
is
b
car
house
zeus
Searching for a: FOUND a in position 0
Searching for is: Did not find is; it should go in position 2
Searching for b: Did not find b; it should go in position 1
Searching for car: Did not find car; it should go in position 1
Searching for house: Did not find house; it should go in position 2
Searching for zeus: Did not find zeus; it should go in position 4