#include #include #include using namespace std; #include "tvector.h" // file: getinfo2.cpp // author Dietolf Ramm; date: 11/6/96 3/4/00 // simple info storage and retrieval system using lines of text // stored in vectors of strings // Use a struct to keep some usage statistics const string SENTINEL = "quit"; struct data { string line; int use; }; void Load(tvector & info) // postcondition: info contains a information from file for later search { int oldcapacity; string filename, s; data d; cout << "Enter name of input file: "; getline(cin, filename); ifstream input; input.open(filename.c_str()); while (getline(input, s)) { oldcapacity = info.capacity(); d.line = s; d.use = 0; info.push_back(d); if (oldcapacity != info.capacity()) cout << "Capacity increased to " << info.capacity() << endl; } cout << info.size() << " lines read." << endl; } void Find(tvector & info, string request) // precondtion: info contains all information from file for search // postcondition: lines containing request as a substring are printed { int k; for (k = 0; k < info.size(); k++) if ( info[k].line.find(request) != string::npos) { cout << info[k].line << endl; info[k].use++; } } void Report(const tvector & info) // postcondition: lines that were referenced printed out with frequency { int k; for (k = 0; k < info.size(); k++) if ( info[k].use > 0 ) { cout << "(" << info[k].use << ")" << endl; cout << info[k].line << endl; } } int main() { tvector info; info.reserve(50); string request; Load(info); cout << "Enter search string: " ; getline(cin, request); while (request != SENTINEL) { Find(info, request); cout << "Enter search string: " ; getline(cin, request); } Report(info); return 0; } /* Sample output: prompt> getinfo2 Enter name of input file: /u/dr/a./nums Capacity increased to 100 Capacity increased to 200 Capacity increased to 400 Capacity increased to 800 644 lines read. Enter search string: Karl 688-1871 Karl Ramm, 407 Gregson St, Apt 2, Durham, NC 27701, N1XPB 660-6905 Karl Ramm, OIT, karl@oit.duke.edu 617-623-6763 Karl Ramm, 390 Broadway, Apt 25, Sommerville, Mass 02145 617-253-0129 office Karl Ramm, kcr@mit.edu /mit/is/dcns/ops/syssupport/kcr Enter search string: Gregson 688-1871 Karl Ramm, 407 Gregson St, Apt 2, Durham, NC 27701, N1XPB Enter search string: Munich [011 49] 89 343277 Sieglind Ramm, BiedersteinerStr 3, 80802 Munich Enter search string: Sieglind [011 49] 89 343277 Sieglind Ramm, BiedersteinerStr 3, 80802 Munich Enter search string: quit (2) 688-1871 Karl Ramm, 407 Gregson St, Apt 2, Durham, NC 27701, N1XPB (1) 660-6905 Karl Ramm, OIT, karl@oit.duke.edu (1) 617-623-6763 Karl Ramm, 390 Broadway, Apt 25, Sommerville, Mass 02145 (1) 617-253-0129 office Karl Ramm, kcr@mit.edu /mit/is/dcns/ops/syssupport/kcr (2) [011 49] 89 343277 Sieglind Ramm, BiedersteinerStr 3, 80802 Munich */