Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <vector>
  4. #include <fstream>
  5. #include <cassert>
  6. using namespace std;
  7.  
  8.  
  9. class Scheduler;
  10.  
  11. //Base class contains all the data member information.
  12. //m_time stores the time for the event.
  13. //kind defines EVENT or DUTY
  14. //type defines an EVENT or DUTY as ANNIV, PARTY, SCHOOL, FAMILY
  15. class Schedule {
  16. protected:
  17.     tm m_time;
  18.     string type;
  19.     string kind;
  20. public:
  21.     void schedule();
  22.     char* get_time();
  23.  
  24.     virtual void set_kind() {   kind = "null"; }
  25.     virtual void set_type() {   type = "null"; }
  26.  
  27.     friend istream& operator>>(istream& i, Schedule s);
  28.     friend ostream& operator<<(ostream& o, Schedule* s);
  29.     friend ostream& operator<<(ostream& o, Scheduler& s);
  30. };
  31.  
  32. //Fills a tm struct with randomized value representing a time.
  33. void Schedule::schedule() {
  34.     m_time.tm_sec = 0;
  35.     m_time.tm_min = 15 * (rand() % 3);
  36.     m_time.tm_hour = rand() % 10 + 7;
  37.     m_time.tm_mday = rand() % 30 + 1;
  38.     m_time.tm_mon = rand() % 11;
  39.     m_time.tm_year = 114;
  40.     m_time.tm_wday = rand() % 7;
  41. }
  42.  
  43. char* Schedule::get_time() {
  44.     return asctime(&m_time); //Returns a string of characters that gives the time in
  45.                             // Weekday Month Day HH:MM:SS Year format.
  46. }
  47.  
  48.  
  49.  
  50. class Party: public Schedule {
  51. public:
  52.     void set_kind() { kind = "EVENT"; }
  53.     void set_type() { type = "PARTY"; }
  54. };
  55.  
  56. class Anniv: public Schedule {
  57. public:
  58.     void set_kind() { kind = "EVENT"; }
  59.     void set_type() { type = "ANNIV"; }
  60. };
  61.  
  62. class School: public Schedule {
  63. public:
  64.     void set_kind() { kind = "DUTY"; }
  65.     void set_type() { type = "SCHOOL"; }
  66. };
  67.  
  68. class Family: public Schedule {
  69. public:
  70.     void set_kind() { kind = "DUTY"; }
  71.     void set_type() { type = "FAMILY"; }
  72.  
  73. };
  74.  
  75. //Class that generates the random schedules and outputs it into a file.
  76. class Scheduler {
  77.     int entries;
  78.     vector<Schedule*> v; //Stores pointers to Schedule objects.
  79.  
  80. public:
  81.     Scheduler(int a) :  entries(a) {};
  82.  
  83.     void create();
  84.     void write(string);
  85.     void read(string);
  86.     void del();
  87.  
  88.     friend ostream& operator<<(ostream& o, Scheduler& s);
  89. };
  90.  
  91. void Scheduler::create() {  //Generates the random Schedule objects.
  92.     for (int i = 0; i < entries; ++i) {
  93.         int r = rand() % 3;
  94.         if (r == 0) {
  95.             v.push_back(new Party());
  96.         } else if (r == 1) {
  97.             v.push_back(new Anniv());
  98.         } else if (r == 2) {
  99.             v.push_back(new School());
  100.         } else if (r == 3) {
  101.             v.push_back(new Family());
  102.         }
  103.         v[i]->schedule(); //Calls schedule() to generate time
  104.         v[i]->set_type(); //Sets the type (PARTY, ANNIV, SCHOOL, FAMILY)
  105.         v[i]->set_kind(); //Sets the kind (EVENT,DUTY)
  106.     }
  107. }
  108.  
  109. void Scheduler::write(string s) { //Writes to output file
  110.     ofstream outfile(s);
  111.     assert(outfile);
  112.     for (int i = 0; i < v.size(); ++i) {
  113.         outfile << v[i] << endl;
  114.     }
  115.     outfile.close();
  116.  
  117.     cout << endl << "WRITING TO " << s << " COMPLETE..." << endl << endl << endl;
  118. }
  119.  
  120. void Scheduler::read(string s) { //Reads the file and stores it into the vector.
  121.     ifstream infile(s);
  122.     assert(infile); //Checks infile.good();
  123.     v.clear(); //Empties out previous vector
  124.     Schedule sch;
  125.  
  126.     while(true) {
  127.         if(infile >> sch) { //Overloaded operator below
  128.             v.push_back(&sch);
  129.             continue;
  130.         }
  131.         if(infile.eof())  {
  132.             break;
  133.         } //When end of file is reached, loop breaks.
  134.  
  135.         cout << "ERROR" << endl; //Catches any problems.
  136.         abort();
  137.     }
  138.     infile.close();
  139.     cout << "READING FROM " << s << " COMPLETED..." << endl << endl << endl;
  140.     cout << "Current contents of " << s << endl << endl << endl << endl;
  141. }
  142.  
  143. void Scheduler::del() { //Deallocate memory
  144.     for (int i = 0; i < v.size(); ++i) {
  145.         delete v[i];
  146.     }
  147. }
  148.  
  149. ostream& operator<<(ostream& o, Schedule* s) {
  150.  
  151.     o<< s->kind << " "<< s->type << " "
  152.             << s->get_time() << endl;
  153.     return o;
  154. }
  155.  
  156. ostream& operator<<(ostream& o, Scheduler& s) {
  157.     for (int i = 0; i < s.v.size(); ++i) {
  158.         o << s.v[i]->kind << ": " << s.v[i]->type << " "  <<  s.v[i]->get_time()
  159.                 << endl;
  160.     }
  161.     return o;
  162. }
  163.  
  164. istream& operator>>(istream& i, Schedule s) {
  165.     i >> s.kind >> s.type;
  166.     return i;
  167. }
  168. int main() {
  169.  
  170.     srand(time(NULL));
  171.     Scheduler s(20);
  172.     s.create();
  173.     cout << s;
  174.  
  175.     s.write("scheduler.data");
  176.  
  177.     s.read("scheduler.data");
  178.     cout << s;
  179.  
  180.     s.del();
  181.  
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement