Comp 272 Program 2 Dordal Feb 5, 2001 Due: Feb 16, 2001 Note that the due date is a Friday; programs must be emailed to me by then. All you should send is your 2.cpp file, and optionally any output. I don't need your project (.dsw) file, or the date files. Your program is to read in a sequence of records, in text format. Each record consists of one line, containing a name and a date. We shall assume these represent enrollment dates, in some sort of database. Records are in the following format; items in brackets are optional. [firstname [middlename]] lastname [month day [year]] Names have only letters a-z,A-Z in them; the date fields have only digits. Only the last name is required. You can identify name fields because they begin with letters; if there is only one name field it is lastname. If there are two namefields they are "firstname lastname". If all three were included, then middlename was provided. Here are some examples peter lars dordal 1 27 1999 fred smith 2 13 2000 // no middlename bush 3 26 1998 // no first or middle name gore 4 1 // no year nader // no month or day or year phoebe b beebee // full name but no date j fred muggs 3 14 // full name, partial date Note that it is illegal to have four or more names, or a month with no day. Note also that you have to read all the names before you can figure out which is the lastname. If the date fields are missing entirely, then today's date is to be used. The Date class contains a today() function for obtaining this date: d = Date::today(); If the month and day are given but the year is missing, then the year is to be a constant THISYEAR, equal (for now) to 2001. Your program is to * read in the records into an array * for each record construct an object of class type Enroll, below * correctly handle all the cases of potentially missing optional data * sort the records by date * print them out Your main task will be to write the constructor for Enroll that constructs from a string "line" you've just read in, sorting out all the optional stuff. To assist you, I've provided you with a file 2.cpp, which includes the function string getword (string & line); The parameter string "line" is initially the entire line as returned by getline() (which for convenience is also in this file). getword() extracts the next "word" (either name or digits); the variable "line" is then set to the remainder of the line. getword() returns an empty string when there is no more data on the line, in which case you're done with that line. 2.cpp also includes a preliminary outline of the program. You will also need to copy the date.h and date.cpp and setup.h files; you can either #include "date.h" // note "" instead of <> and add date.cpp to your project, or just #include "date.cpp" and ignore date.h and separate compilation. You will need a class Name, to hold the name fields (anything not provided is to be represented as the empty string), and a class Enroll, containing the Name and Date. Here are the classes (also in 2.cpp); note that they consist of constructors and (field) accessors only. You will need to add to these. class Name { private: string _fname; string _mname; string _lname; public: Name(string first, string mid, string last) : _fname(first), _mname(mid), _lname(last) {} string fname() {return _fname;} string mname() {return _mname;} string lname() {return _lname;} }; class Enroll { private: Name _name; Date _enrolldate; public: Enroll(Name n, Date d) : _name(n), _enrolldate(d) {}; : _name(fname, mname, lname), _enrolldate(d) {} Enroll(string line); // this is the one you actually *use* string fname() {return _name.fname();} string mname() {return _name.mname();} string lname() {return _name.lname();} Date enrolldate () {return _enrolldate;} }; The function Date::today() returns todays' date. If d1 and d2 are dates, d1.compare(d2) returns a value <0 if d1 comes before d2, and a value >0 if d1 comes after d2, and the value 0 if d1=d2. Note that you can *not* write "d1 #include #include using namespace std; #include "date.cpp" #include "getword.cpp" const int THISYEAR = 2001; class Name { private: string _fname; string _mname; string _lname; public: Name(string f, string m, string l) : _fname(f), _mname(m), _lname(l) {} Name() {_fname = _mname = _lname = "";} string fname() {return _fname;} string mname() {return _mname;} string lname() {return _lname;} }; class Enroll { private: Name _name; Date _enrolldate; public: Enroll(Name n, Date d) : _name(n), _enrolldate(d) {} Enroll(string s); // the big one string fname() {return _name.fname();} string mname() {return _name.mname();} string lname() {return _name.lname();} Date enrolldate () {return _enrolldate;} }; void main() { string line; vector A; while (!cin.eof()) { getline(cin, line); if (line == "") break; // making the eof test redundant A.push_back(Enroll(line)); // slick, huh? } // now sort it and print it out }