#include #include // for ifstream #include "CPstring.h" #include "prompt.h" #include "vector.h" // file: context2.cc // author Dietolf Ramm; date: 10/30/96, 10/31/96 // read text, as words, into a vector // interactively, display requested word with C words before and after. const int C = 3; // number of words to display on either side const string SENTINEL = "quit"; // word to cause exit from request loop void Load(Vector & text, int & size); void Display(const Vector & text, int size, int where); void Find(const Vector & text, int size, string request); int main() { int size; string request; Vector text(500); Load(text, size); cout << endl << "search word: "; cin >> request; while(request != SENTINEL) { Find(text, size, request); cout << endl << "search word: "; cin >> request; } return 0; } void Find(const Vector & text, int size, string request) // postcondition: for each match of request with text, Display invoked { int k; for (k = 0; k < size; k++) if (text[k] == request) Display(text, size, k); } void Display(const Vector & text, int size, int where) // postcondition: located word printed in context { int k, start, finish; start = where - C; if (start < 0) start = 0; finish = where + C; if (finish >= size) finish = size-1; for (k = start; k <= finish; k++) cout << text[k] << " "; cout << endl; } void Load(Vector & text, int & size) // postcondition: text contains words read in with size telling how many { string word; string filename = PromptString("Enter name of input file: "); ifstream input(filename); size = 0; while (input >> word) { text[size] = word; size++; } cout << size << " words read." << endl; } Sample output: context2 Enter name of input file: context2.cc 289 words read. search word: for #include // for ifstream #include "CPstring.h" request) // postcondition: for each match of { int k; for (k = 0; finish = size-1; for (k = start; search word: #include #include #include #include #include // for // for ifstream #include "CPstring.h" #include "prompt.h" ifstream #include "CPstring.h" #include "prompt.h" #include "vector.h" "CPstring.h" #include "prompt.h" #include "vector.h" // file: search word: } cin >> request; } return 0; } } return 0; } void Find(const Vector Display(text, size, k); } void Display(const Vector cout << endl; } void Load(Vector & = word; size++; } cout << size read." << endl; } search word: quit Enter name of input file: honor 192 words read. search word: will University Community: I will not lie, cheat, academic endeavors. I will forthrightly oppose each academic dishonesty. I will communicate directly with or anonymous. I will give prompt written any course. I will let my conscience my written report will name the person search word: quit