#include #include // for width() #include // for ifstream/ofstream #include // for tolower #include "apstring.h" #include "apvector.h" // ********************************** // author: Owen Astrachan // // for APCS workshop // // illustrates struct with constructor/function // collection of structs // // this will build towards several uses of templates // // // for reading, the format of a file of people is // // first last // gender // school // id number // // for example: // // Owen Astrachan // Male // College // 101 // Sharon Lee // Female // Highschool // 409 // // ********************************** struct Person { apstring myFirst,myLast; apstring mySchool; apstring myGender; int myNumber; Person(); Person(const apstring & first, const apstring & last, const apstring & school, const apstring & gender, int number); void Print(ostream & out) const; }; ostream& operator << (ostream & out, const Person & p) //postcondition: p printed to out, out returned { p.Print(out); return out; } // ************************************ // implement Person // *********************************** Person::Person() // postcondition: initialized with default values { } Person::Person(const apstring & first, const apstring & last, const apstring & school, const apstring & gender, int number) : myFirst(first), myLast(last), mySchool(school), myGender(gender), myNumber(number) { // initialized using initializer list } void Person::Print(ostream & out) const // postcondition: person printed to stream out { out << setw(20) << myFirst + " " + myLast << setw(15) << mySchool << char(toupper(myGender[0])) << " " << myNumber; } // ********************************** // Collection of people // ********************************** class Collection { public: Collection(); // accessor or const functions void Search(const apstring & name, Collection & match) const; // return matches for name void Print(ostream & out) const; // print collection // mutator functions void Add(const Person & p); // add p to collection void Clear(); // remove everyone private: apvector myList; // the people in collection int myCount; // # people in collection }; Collection::Collection() : myList(10), myCount(0) /// postcondition: collection initialized with zero people { } void Collection::Search(const apstring & name, Collection & match) const // postcondition: all people in collection whose // last name contains string name // are stored in match { int k; for(k=0; k < myCount; k++) { if (myList[k].myLast.find(name) != npos) { match.Add(myList[k]); } } } void Collection::Print(ostream & out) const // postcondition: all elements of collection are printed { int k; for(k=0;k < myCount; k++) { out << myList[k] << endl; } cout << "------ " << myCount << " total people" << endl; } void Collection::Add(const Person & p) // postcondition: p added to collection { if (myCount >= myList.length()) { myList.resize(myList.length()*2); } myList[myCount] = p; myCount++; } void Collection::Clear() { myCount = 0; } int main() { apstring filename = "c:\\ets\\berkworkshop\\98subgroup"; ifstream input; input.open(filename.c_str()); if (! input.good()) { cerr << "could not open " << filename << endl; return(1); } Collection group,matchGroup; apstring name,gender,school,number; int spaceIndex; while (getline(input,name) && getline(input,gender) && getline(input,school) && getline(input,number)) { spaceIndex = name.find(' '); Person p(name.substr(0,spaceIndex), name.substr(spaceIndex+1, name.length()), school, gender, atoi(number.c_str())); group.Add(p); } group.Print(cout); while (true) { cout << endl << "search for: (return to quit) "; getline(cin,name); if (name == "") break; group.Search(name,matchGroup); cout << endl << "matches follow" << endl << endl; matchGroup.Print(cout); } cout << endl << "searching over" << endl; return 0; }