home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CL187A.ZIP / EXAMP211.CPP < prev    next >
C/C++ Source or Header  |  1994-03-15  |  4KB  |  171 lines

  1. // Container Lite (CL v 1.87a)
  2. // (C) Copyright John Webster Small 1994
  3. // All rights reserved
  4. // examp211.cpp -- link with cl.obj
  5. // rework of examp205.cpp to show off Smalltalk like iterators
  6.  
  7. //#define CL_NO_TEMPLATES
  8.  
  9. #include "cl.h"
  10.  
  11. class Employee  {
  12.     char *name;
  13.     unsigned salary;
  14.     int cmp(const Employee& e) const;
  15.     static    Employee * THIS;
  16.     static    ostream& SHOW(ostream& os)
  17.     {
  18.        return (os << "employee name: "
  19.           << setw(20)
  20.           << (THIS->name? THIS->name : "n/a")
  21.           << "\tsalary: " << THIS->salary);
  22.     }
  23. public:
  24.     Employee(const char * name = 0,
  25.         unsigned salary = 0)
  26.     {
  27.         this->name = (name? strdup(name) : 0);
  28.         this->salary = salary;
  29.     }
  30.     Employee(const Employee& e)
  31.     {
  32.         name = (e.name? strdup(e.name) : 0);
  33.         salary = e.salary;
  34.     }
  35.     Employee& operator=(const Employee& e)
  36.     {
  37.         delete name;
  38.         name = (e.name? strdup(e.name) : 0);
  39.         salary = e.salary;
  40.         return *this;
  41.     }
  42.     int operator==(const Employee& e) const
  43.         { return !cmp(e); }
  44.     int operator>(const Employee& e) const
  45.         { return (cmp(e) > 0); }
  46.     ~Employee() { delete name; }
  47.     ostream& (*show())(ostream&)
  48.         { THIS = this; return SHOW; }
  49.     friend ostream& operator<<
  50.         (ostream& os, Employee& e)
  51.     { return os << &e.name << endm << e.salary; }
  52.     friend istream& operator>>
  53.         (istream& is, Employee& e)
  54.     { return is >> &e.name >> nextm >> e.salary; }
  55.  
  56.  
  57.  
  58.     Employee * operator()(const char * name)
  59.     {
  60.         delete this->name;
  61.         this->name = (name? strdup(name) : 0);
  62.         salary = 0;
  63.         return this;
  64.     }
  65.     static int cmpName(const Employee * E1,
  66.         const Employee * E2)
  67.         { return strcmp(E1->name,E2->name); }
  68.     static int detectName(Employee * E,
  69.         void * name)
  70.     { return (name? !strcmp(E->name,(char *)name) : 0); }
  71.     static int detectSalaryGE(Employee * E,
  72.         void * salary)
  73.     { return (E->salary >= *(unsigned *)salary); }
  74.     static void * collectSalaryGE(Employee * E,
  75.         void * salary)
  76.     { return ((E->salary >= (*(unsigned *)salary))?
  77.         (void *) new unsigned(E->salary) : 0); }
  78.  
  79.  
  80. };
  81.  
  82. Employee * Employee::THIS;
  83.  
  84. int Employee::cmp(const Employee& e) const
  85. {
  86.     if (!name)
  87.         if (!e.name)
  88.             return 0;
  89.         else
  90.             return -1;
  91.     else
  92.         if (!e.name)
  93.             return 1;
  94.         else
  95.            return strcmp(name,e.name);
  96. }
  97.  
  98.  
  99. #include "cl.h"
  100.  
  101. #if defined(CL_NO_TEMPLATES)
  102.     #define   ITEM      Employee
  103.     #define   CL_WELL_ENDOWED
  104.     #define   CL        CL_Employee
  105.     #include "cl.hf"
  106. #else
  107.     CL_WELL_ENDOWED(Employee)
  108.     #define   CL_Employee       CL<Employee>
  109. #endif
  110.  
  111. #if defined(CL_NO_TEMPLATES)
  112.     #define   ITEM     unsigned
  113.     #define   CL_WELL_ENDOWED
  114.     #define   CL       CL_unsigned
  115.     #include "cl.hf"
  116. #else
  117.     CL_WELL_ENDOWED(unsigned)
  118.     #define   CL_unsigned    CL<unsigned>
  119. #endif
  120.  
  121.  
  122. #define EmployeeFile "employs.tmp"
  123.  
  124. main()
  125. {
  126.     CL_Employee cE(CL_ANDS);
  127.     cE.ins(new Employee("Doe, John",1000));
  128.     Employee E("Mitchell, Allen",100);
  129.     cE.insQNew(&E);
  130.     cE.push(new Employee("Morgan, Maria",10000));
  131.     cE.insQ(new Employee("Zorro", 200));
  132.     cE.save(EmployeeFile);
  133.     cE.allClr();
  134.     cE.load(EmployeeFile);
  135.     cE.sort();
  136.     cout << "\nEmployees in alphabetical order: "
  137.         << endl;
  138.     while (cE.nextAsg(&E))
  139.         cout << E.show() << endl;
  140.     Employee *Eptr;
  141.     unsigned i, salary = 500;
  142.     char name[] = "Morgan, Maria";
  143.     cout << "\nFind employee: "
  144.         << name << endl;
  145.     if (cE.detect(Eptr,Employee::detectName,name))
  146.         cout << Eptr->show() << endl;
  147.     if (cE.detect(i,Employee::detectName,name))
  148.         cout << cE[i]->show() << endl;
  149.     cout << "\nFind first employe with salary >= " << salary << endl;
  150.     if (cE.detect(Eptr,Employee::detectSalaryGE,&salary))
  151.         cout << Eptr->show() << endl;
  152.     cout << "\nFind all employees with salaries >= " << salary << endl;
  153.     CL_Employee cE2;
  154.     if (cE.select(cE2,Employee::detectSalaryGE,&salary))
  155.         while (++cE2)
  156.             cout << ((Employee *)cE2)->show() << endl;
  157.     cout << "\nList all salaries >= " << salary << endl;
  158.     CL_unsigned cs;
  159.     if (cE.collect((cl&)cs,Employee::collectSalaryGE,&salary))
  160.         while (++cs)
  161.             cout << *(unsigned *)cs << endl;
  162.     cE2.allClr();
  163.     cout << "\nFind all employees with name >= "
  164.         << name << endl;
  165.     cE.setCmP(Employee::cmpName);
  166.     if (cE.findAll(cE2,E(name),cl::GE))
  167.         while (++cE2)
  168.             cout << ((Employee *)cE2)->show() << endl;
  169.     return 0;
  170. }
  171.