#include #include #include "apvector.h" const int MAX_BOXES = 100; struct MailInfo { int POBox; int mailCount; // # pieces of mail for this po box }; class MailBoxes { public: MailBoxes(); // initial capacity = MAX_BOXES, zero boxes stored void processMail(istream & input); // process file of po boxes void printAll(ostream & output); private: int boxIndex(int box); // helper function, returns index apvector myBoxes; // list of all poboxes and counts int myBoxCount; // # of itmes stored in myBoxes }; MailBoxes::MailBoxes() : myBoxes(MAX_BOXES), myBoxCount(0) // postcondition: capacity = MAX_BOXES, zero boxes stored { } int MailBoxes::boxIndex(int box) //postcondition: returns the index of the element in myBoxes // whose POBox field equals box if there is such an element; // otherwise, returns -1. { int k; for(k=0; k < myBoxCount; k++) { if (myBoxes[k].POBox == box) { return k; } } return -1; } void MailBoxes::processMail(istream & input) // precondition: input is formatted correctly, is open for reading, // and is positioned at the beginning of the file; // the file contains no more than MAX_BOXES different // P.O. box numbers { int box; while (input >> box) { int index = boxIndex(box); if (index != -1) { myBoxes[index].mailCount++; } else { myBoxes[myBoxCount].POBox = box; myBoxes[myBoxCount].mailCount = 1; myBoxCount++; } } } void MailBoxes::printAll(ostream & output) { int k; for(k=0; k < myBoxCount; k++) { output << "box = [" << myBoxes[k].POBox << "] count = " << myBoxes[k].mailCount << endl; } } int main() { MailBoxes boxes; cout << "after file processing" << endl; ifstream input("mail.dat"); boxes.processMail(input); boxes.printAll(cout); }