Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <vector>
- #include <fstream>
- #include <cassert>
- using namespace std;
- class Scheduler;
- //Base class contains all the data member information.
- //m_time stores the time for the event.
- //kind defines EVENT or DUTY
- //type defines an EVENT or DUTY as ANNIV, PARTY, SCHOOL, FAMILY
- class Schedule {
- protected:
- tm m_time;
- string type;
- string kind;
- public:
- void schedule();
- char* get_time();
- virtual void set_kind() { kind = "null"; }
- virtual void set_type() { type = "null"; }
- friend istream& operator>>(istream& i, Schedule s);
- friend ostream& operator<<(ostream& o, Schedule* s);
- friend ostream& operator<<(ostream& o, Scheduler& s);
- };
- //Fills a tm struct with randomized value representing a time.
- void Schedule::schedule() {
- m_time.tm_sec = 0;
- m_time.tm_min = 15 * (rand() % 3);
- m_time.tm_hour = rand() % 10 + 7;
- m_time.tm_mday = rand() % 30 + 1;
- m_time.tm_mon = rand() % 11;
- m_time.tm_year = 114;
- m_time.tm_wday = rand() % 7;
- }
- char* Schedule::get_time() {
- return asctime(&m_time); //Returns a string of characters that gives the time in
- // Weekday Month Day HH:MM:SS Year format.
- }
- class Party: public Schedule {
- public:
- void set_kind() { kind = "EVENT"; }
- void set_type() { type = "PARTY"; }
- };
- class Anniv: public Schedule {
- public:
- void set_kind() { kind = "EVENT"; }
- void set_type() { type = "ANNIV"; }
- };
- class School: public Schedule {
- public:
- void set_kind() { kind = "DUTY"; }
- void set_type() { type = "SCHOOL"; }
- };
- class Family: public Schedule {
- public:
- void set_kind() { kind = "DUTY"; }
- void set_type() { type = "FAMILY"; }
- };
- //Class that generates the random schedules and outputs it into a file.
- class Scheduler {
- int entries;
- vector<Schedule*> v; //Stores pointers to Schedule objects.
- public:
- Scheduler(int a) : entries(a) {};
- void create();
- void write(string);
- void read(string);
- void del();
- friend ostream& operator<<(ostream& o, Scheduler& s);
- };
- void Scheduler::create() { //Generates the random Schedule objects.
- for (int i = 0; i < entries; ++i) {
- int r = rand() % 3;
- if (r == 0) {
- v.push_back(new Party());
- } else if (r == 1) {
- v.push_back(new Anniv());
- } else if (r == 2) {
- v.push_back(new School());
- } else if (r == 3) {
- v.push_back(new Family());
- }
- v[i]->schedule(); //Calls schedule() to generate time
- v[i]->set_type(); //Sets the type (PARTY, ANNIV, SCHOOL, FAMILY)
- v[i]->set_kind(); //Sets the kind (EVENT,DUTY)
- }
- }
- void Scheduler::write(string s) { //Writes to output file
- ofstream outfile(s);
- assert(outfile);
- for (int i = 0; i < v.size(); ++i) {
- outfile << v[i] << endl;
- }
- outfile.close();
- cout << endl << "WRITING TO " << s << " COMPLETE..." << endl << endl << endl;
- }
- void Scheduler::read(string s) { //Reads the file and stores it into the vector.
- ifstream infile(s);
- assert(infile); //Checks infile.good();
- v.clear(); //Empties out previous vector
- Schedule sch;
- while(true) {
- if(infile >> sch) { //Overloaded operator below
- v.push_back(&sch);
- continue;
- }
- if(infile.eof()) {
- break;
- } //When end of file is reached, loop breaks.
- cout << "ERROR" << endl; //Catches any problems.
- abort();
- }
- infile.close();
- cout << "READING FROM " << s << " COMPLETED..." << endl << endl << endl;
- cout << "Current contents of " << s << endl << endl << endl << endl;
- }
- void Scheduler::del() { //Deallocate memory
- for (int i = 0; i < v.size(); ++i) {
- delete v[i];
- }
- }
- ostream& operator<<(ostream& o, Schedule* s) {
- o<< s->kind << " "<< s->type << " "
- << s->get_time() << endl;
- return o;
- }
- ostream& operator<<(ostream& o, Scheduler& s) {
- for (int i = 0; i < s.v.size(); ++i) {
- o << s.v[i]->kind << ": " << s.v[i]->type << " " << s.v[i]->get_time()
- << endl;
- }
- return o;
- }
- istream& operator>>(istream& i, Schedule s) {
- i >> s.kind >> s.type;
- return i;
- }
- int main() {
- srand(time(NULL));
- Scheduler s(20);
- s.create();
- cout << s;
- s.write("scheduler.data");
- s.read("scheduler.data");
- cout << s;
- s.del();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement