12
CSE 332: STL case study STL Case Study: Building a Meeting Schedule • Goals Read in list of (course) meeting times • Days of the week are read in as characters M,T,W,R,F,S,U • Times are read as unsigned decimal integers (11:59pm is 2359) – Sort the list according to day of the week and then start time and then end time – Detect any overlapping meetings and print them out – Print an ordered schedule for the week The STL provides most of the code for the above

CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

Embed Size (px)

DESCRIPTION

CSE 332: STL case study Output Should Show Conflicts, Sorted Schedule STL> cat infile | xargs main CONFLICT: CS242 T CS281 T CS282 M CS242 T CS242 T CS281 T CS201 T CS282 W CS101 W CS242 R CS281 R CS201 R Conflicts show overlapping meeting times on same day Also produce week’s schedule –Listed by day, then time

Citation preview

Page 1: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

STL Case Study: Building a Meeting Schedule

• Goals– Read in list of (course) meeting times

• Days of the week are read in as characters M,T,W,R,F,S,U• Times are read as unsigned decimal integers (11:59pm is 2359)

– Sort the list according to day of the week and then start time and then end time

– Detect any overlapping meetings and print them out– Print an ordered schedule for the week

• The STL provides most of the code for the above

Page 2: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Input File Gives the Possible MeetingsSTL> cat infile

CS101 W 1730 2030CS242 T 1000 1130CS242 T 1230 1430CS242 R 1000 1130CS281 T 1300 1430CS281 R 1300 1430CS282 M 1300 1430CS282 W 1300 1430CS201 T 1600 1730CS201 R 1600 1730

• Lines can be in any order– One line per meeting of a course

• Format for the lines is– The (course) meeting’s name– The day of the week– The start time– The ending time

• Could use getline, istringstream to parse the data in the file

• But for variation we’ll show how to parse them as command line arguments, in Linux shell

Page 3: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Output Should Show Conflicts, Sorted Schedule

STL> cat infile | xargs main

CONFLICT: CS242 T 1230 1430 CS281 T 1300 1430

CS282 M 1300 1430CS242 T 1000 1130CS242 T 1230 1430CS281 T 1300 1430CS201 T 1600 1730CS282 W 1300 1430CS101 W 1730 2030CS242 R 1000 1130CS281 R 1300 1430CS201 R 1600 1730

• Conflicts show overlapping meeting times on same day

• Also produce week’s schedule– Listed by day, then time

Page 4: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Meeting Header File#include <iostream>struct Meeting { enum Day_Of_Week {MO, TU, WE, TH, FR, SA, SU}; static Day_Of_Week day_of_week (char c); Meeting (const char * title, Day_Of_Week day, unsigned int start_time, unsigned int finish_time); Meeting (const Meeting & m); Meeting & operator = (const Meeting & m); bool operator < (const Meeting & m) const; bool operator == (const Meeting & m) const; const char * title_; Day_Of_Week day_; unsigned int start_time_; unsigned int finish_time_;};

ostream & operator << (ostream &os, const Meeting & m);

Page 5: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Meeting Source File (1/4)#include "Meeting.h“

Meeting::Day_Of_Week Meeting::day_of_week (char c) { Meeting::Day_Of_Week result = Meeting::MO; switch (c) { case ’M’: break; case ’T’: result = Meeting::TU; break; case ’W’: result = Meeting::WE; break; case ’R’: result = Meeting::TH; break; case ’F’: result = Meeting::FR; break; case ’S’: result = Meeting::SA; break; case ’U’: result = Meeting::SU; break; default: throw c; } return result;}

Page 6: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Meeting Source File (2/4)Meeting::Meeting (const char * title, Day_Of_Week day, unsigned int start_time, unsigned int finish_time): title_ (title), day_ (day), start_time_ (start_time), finish_time_ (finish_time) {}

Meeting::Meeting (const Meeting & m): title_ (m.title_), day_ (m.day_),start_time_ (m.start_time_), finish_time_ (m.finish_time_){}

Meeting & Meeting::operator = (const Meeting & m) { this->title_ = m.title_; this->day_ = m.day_; this->start_time_ = m.start_time_; this->finish_time_ = m.finish_time_; return *this;}

Page 7: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Meeting Source File (3/4)bool Meeting::operator == (const Meeting & m) const { return (this->day_ == m.day_ && ((this->start_time_ <= m.start_time_ && m.start_time_ <= this->finish_time_) || (m.start_time_ <= this->start_time_ && this->start_time_ <= m.finish_time_)));}

bool Meeting::operator < (const Meeting & m) const { return (day_ < m.day_ || (day_ == m.day_&& start_time_ < m.start_time_) || (day_ == m.day_ && start_time_ == m.start_time_ && finish_time_ < m.finish_time_));}

Page 8: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Meeting Source File (4/4)ostream & operator << (ostream &os, const Meeting & m) { const char * dow = " "; switch (m.day_) { case Meeting::MO: dow="M "; break; case Meeting::TU: dow="T "; break; case Meeting::WE: dow="W "; break; case Meeting::TH: dow="R "; break; case Meeting::FR: dow="F "; break; case Meeting::SA: dow="S "; break; case Meeting::SU: dow="U "; break; } return os << m.title_ << " " << dow << m.start_time_ << " “ << m.finish_time_;}

Page 9: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Main Source File (1/3)#include "Meeting.h“#include <algorithm>#include <vector>#include <iostream>int parse_args (int argc, char * argv[], vector<Meeting>& schedule) { for (int i = 1; i < argc; i+=4) { schedule.push_back ( Meeting(argv [i], Meeting::day_of_week (*argv [i+1]), static_cast<unsigned int>(atoi (argv [i+2])), static_cast<unsigned int>(atoi (argv [i+3])))); } return 0;}

Page 10: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Main Source File (2/3)int print_schedule (vector<Meeting> &schedule) { // Find and print out any conflicts for (vector<Meeting>::iterator j = schedule.begin (); j != schedule.end (); ++j) { j = adjacent_find (j, schedule.end ()); if (j == schedule.end ()) { break; } cout << "CONFLICT:" << endl << " " << *j << endl << " " << *(j+1) << endl << endl; }

// Print out the sorted schedule copy (schedule.begin (), schedule.end (), ostream_iterator<Meeting>(cout, "\n"));

return 0;}

Page 11: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Main Source File (3/3)

int main (int argc, char *argv[]) { vector<Meeting> schedule; if (parse_args (argc, argv, schedule) < 0){ return -1; } sort (schedule.begin (), schedule.end ()); return print_schedule (schedule);}

Page 12: CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters

CSE 332: STL case study

Summary

• This case study shows a few more ways to combine STL features into working programs

• Most of the work goes into the Meeting abstraction• The rest of it is pretty straightforward using the STL

• No additional studio exercises are assigned today, though if you’d like to work through this example (or similar ones, say overlapping rectangles in 2-D

• We’ll also be in the studio space if you’d like help on any previous exercises you’ve not yet completed or on the lab assignment