home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_11 / janzen / fzy_set.hpp < prev    next >
C/C++ Source or Header  |  1993-09-01  |  5KB  |  216 lines

  1. EXAMPLE TWO (2):
  2.  
  3. #include <iostream.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6.  
  7. class fzy_set
  8. {
  9.     private:
  10.         int dimension;
  11.         fuzzy *fzy_data;
  12.     public:
  13.         fzy_set(const int);
  14.         fzy_set(const fzy_set&);
  15.         ~fzy_set(void);
  16.         fzy_set& operator=(const fzy_set&);
  17.         fuzzy& operator[](const int len);
  18.         fuzzy& operator[](const int len) const;
  19.         friend int operator<(const fzy_set&, 
  20.                              const fzy_set&);
  21.         friend int operator<=(const fzy_set&, 
  22.                               const fzy_set&);
  23.         friend int operator==(const fzy_set&,
  24.                               const fzy_set&);
  25.         friend int operator>=(const fzy_set&, 
  26.                               const fzy_set&);
  27.         friend int operator>(const fzy_set&, 
  28.                              const fzy_set&);
  29.         friend int operator!=(const fzy_set&, 
  30.                               const fzy_set&);
  31.         friend ostream& operator<<(ostream& s, 
  32.                                    fzy_set& z);
  33.         friend fuzzy fzy_set_max(const fzy_set&);
  34.         friend fzy_set operator*(const fzy_set&, 
  35.                                  const class fam_2&);
  36. };
  37.  
  38. fzy_set::fzy_set(const int size = 1) // constructor
  39. {
  40.     dimension = ((size >= 0) ? size : 1);
  41.     fzy_data = new fuzzy[dimension];
  42.     return;
  43. }
  44.  
  45. fzy_set::fzy_set(const fzy_set& fzy_set_in)
  46. {
  47.     fzy_data = new fuzzy[dimension 
  48.              = fzy_set_in.dimension];
  49.     memcpy(fzy_data, fzy_set_in.fzy_data, 
  50.             dimension * sizeof (fuzzy));
  51.     return;
  52. }
  53.  
  54. fzy_set::~fzy_set(void)
  55. {
  56.     delete[] fzy_data;
  57.     return;
  58. }
  59.  
  60. fzy_set& fzy_set::operator=(const fzy_set& r_value)
  61. {
  62.     if (this != &r_value)
  63.     {
  64.         delete[] fzy_data;
  65.         fzy_data = new fuzzy(r_value.dimension);
  66.         memcpy(fzy_data, r_value.fzy_data,
  67.                r_value.dimension 
  68.                 * sizeof(fuzzy));
  69.     }
  70.     return *this;
  71. }
  72.  
  73. fuzzy& fzy_set::operator[](const int len)
  74. {
  75.     return ((len >= 0) && (len <= dimension)) ? 
  76.            fzy_data[len] : fzy_data[0];
  77. }
  78.  
  79. fuzzy& fzy_set::operator[](const int len) const
  80. {
  81.     return ((len >= 0) && (len <= dimension)) ? 
  82.            fzy_data[len] : fzy_data[0];
  83. }
  84.  
  85. int operator<(const fzy_set& value_a, 
  86.               const fzy_set& value_b)
  87. {
  88.     auto int    result  = 0,
  89.                 ctr     = 0;
  90.  
  91.     if (value_a.dimension == value_b.dimension)
  92.     {
  93.         while ((result = (value_a[ctr] < value_b[ctr]))
  94.                && (ctr < value_a.dimension))
  95.         {
  96.             ctr++;
  97.         }
  98.     }
  99.     return result;
  100. }
  101.  
  102. int operator<=(const fzy_set& value_a, 
  103.                const fzy_set& value_b)
  104. {
  105.     auto int    result = 0,
  106.                 ctr = 0;
  107.  
  108.     if (value_a.dimension == value_b.dimension)
  109.     {
  110.         while ((result = (value_a[ctr] 
  111.                 <= value_b[ctr]))
  112.                && (ctr < value_a.dimension))
  113.         {
  114.             ctr++;
  115.         }
  116.     }
  117.     return result;
  118. }
  119.  
  120. int operator==(const fzy_set& value_a, 
  121.                const fzy_set& value_b)
  122. {
  123.     auto int    result = 0,
  124.                 ctr = 0;
  125.  
  126.     if (value_a.dimension == value_b.dimension)
  127.     {
  128.         while ((result = (value_a[ctr] 
  129.                == value_b[ctr]))
  130.                && (ctr < value_a.dimension))
  131.         {
  132.             ctr++;
  133.         }
  134.     }
  135.     return result;
  136. }
  137.  
  138. int operator>=(const fzy_set& value_a, 
  139.                const fzy_set& value_b)
  140. {
  141.     auto int    result = 0,
  142.                 ctr = 0;
  143.  
  144.     if (value_a.dimension == value_b.dimension)
  145.     {
  146.         while ((result 
  147.         = (value_a[ctr] >= value_b[ctr]))
  148.                && (ctr < value_a.dimension))
  149.         {
  150.             ctr++;
  151.         }
  152.     }
  153.     return result;
  154. }
  155.  
  156. int operator>(const fzy_set& value_a, 
  157.               const fzy_set& value_b)
  158. {
  159.     auto int    result = 0,
  160.                 ctr = 0;
  161.  
  162.     if (value_a.dimension == value_b.dimension)
  163.     {
  164.         while ((result = (value_a[ctr] > value_b[ctr]))
  165.                && (ctr < value_a.dimension))
  166.         {
  167.             ctr++;
  168.         }
  169.     }
  170.     return result;
  171. }
  172.  
  173. int operator!=(const fzy_set& value_a, 
  174.                const fzy_set& value_b)
  175. {
  176.     auto int    result = 0,
  177.                 ctr = 0;
  178.  
  179.     if (value_a.dimension == value_b.dimension)
  180.     {
  181.         while ((result = 
  182.              (value_a[ctr] == value_b[ctr]))
  183.                && (ctr < value_a.dimension))
  184.         {
  185.             ctr++;
  186.         }
  187.     }
  188.     return !result;
  189. }
  190.  
  191. ostream& operator<<(ostream& s, fzy_set& z)
  192. {
  193.     static int ctr;
  194.     s << "(";
  195.     fuzzy *set = z.fzy_data;
  196.     int ind = z.dimension - 1;
  197.     for (ctr = 0; ctr < ind; ctr++)
  198.     {
  199.         s << *set++ << "  ";
  200.     }
  201.     return s << *set << ")\n";
  202. }
  203.  
  204. fuzzy fzy_set_max(const fzy_set& set)
  205. {
  206.     auto fuzzy tmp_fuzzy(0.0);
  207.  
  208.     for (int i = 0; i < set.dimension; i++)
  209.     {
  210.         tmp_fuzzy |= set[i];
  211.     }
  212.     return tmp_fuzzy;
  213. }
  214.  
  215.  
  216.