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 / fuzzy.hpp < prev    next >
C/C++ Source or Header  |  1993-09-01  |  4KB  |  181 lines

  1. EXAMPLE ONE:
  2.  
  3. #include <iostream.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6.  
  7. class fuzzy
  8. {
  9.     private:
  10.         double  truth;
  11.     public:
  12.         inline fuzzy(const double);
  13.         void get(double *);
  14.         double get(void) const;
  15.         fuzzy operator!(void) const;
  16.         fuzzy very(void) const;
  17.         fuzzy somewhat(void) const;
  18.         inline fuzzy& operator|=(fuzzy&);
  19.         inline fuzzy& operator&=(fuzzy&);
  20.         fuzzy& operator=(const fuzzy&);
  21.         fuzzy& operator=(const double);
  22.         friend fuzzy operator|(const fuzzy&, 
  23.                                const fuzzy&);
  24.         friend fuzzy operator&(const fuzzy&,
  25.                                const fuzzy&);
  26.         friend fuzzy fuzzy_implies(const fuzzy&,
  27.                                    const fuzzy&);
  28.         friend fuzzy fuzzy_iff(const fuzzy&,
  29.                                const fuzzy&);
  30.         friend inline int operator<(const fuzzy&,
  31.                                     const fuzzy&);
  32.         friend inline int operator<=(const fuzzy&,
  33.                                      const fuzzy&);
  34.         friend inline int operator==(const fuzzy&,
  35.                                      const fuzzy&);
  36.         friend inline int operator>=(const fuzzy&,
  37.                                      const fuzzy&);
  38.         friend inline int operator>(const fuzzy&,
  39.                                     const fuzzy&);
  40.         friend inline int operator!=(const fuzzy&,
  41.                                      const fuzzy&);
  42.         friend ostream& operator<<(ostream& s, 
  43.                                    const fuzzy& z);
  44. };
  45.  
  46. inline fuzzy::fuzzy(const double temp_truth = 0.0)
  47. {
  48.     truth = ((temp_truth >= 0.0) 
  49.           && (temp_truth <= 1.0)) ?
  50.               temp_truth : 0.0;
  51.     return;
  52. }
  53.  
  54. void fuzzy::get(double *get_truth)
  55. {
  56.     *get_truth = truth;
  57.     return;
  58. }
  59.  
  60. double fuzzy::get(void) const
  61. {
  62.     return truth;
  63. }
  64.  
  65. fuzzy fuzzy::operator!(void) const
  66. {
  67.     return (1.0 - truth);
  68. }
  69.  
  70. fuzzy fuzzy::very(void) const
  71. {
  72.     return (truth * truth);
  73. }
  74.  
  75. fuzzy fuzzy::somewhat(void) const
  76. {
  77.     return sqrt(truth);
  78. }
  79.  
  80. inline fuzzy& fuzzy::operator|=(fuzzy& value)
  81. {
  82.     return (*this = *this | value);
  83. }
  84.  
  85. inline fuzzy& fuzzy::operator&=(fuzzy& value)
  86. {
  87.     return (*this = *this & value);
  88. }
  89.  
  90. fuzzy& fuzzy::operator=(const fuzzy& fuzzy_in)
  91. {
  92.     truth = fuzzy_in.truth;
  93.     return *this;
  94. }
  95.  
  96. fuzzy& fuzzy::operator=(const double dbl_in)
  97. {
  98.     truth = ((dbl_in >= 0.0) && (dbl_in <= 1.0)) ?
  99.               dbl_in : 0.0;
  100.     return *this;
  101. }
  102.  
  103. fuzzy operator|(const fuzzy& value_a, const fuzzy& value_b)
  104. {
  105.     return (value_a.truth > value_b.truth) ?
  106.             value_a : value_b;
  107. }
  108.  
  109. fuzzy operator&(const fuzzy &value_a, 
  110.                 const fuzzy &value_b)
  111. {
  112.     return (value_a < value_b) ? value_a : value_b;
  113. }
  114.  
  115. fuzzy fuzzy_implies(const fuzzy& value_a, 
  116.                     const fuzzy& value_b)
  117. {
  118.     auto double temp_value;
  119.     
  120.     temp_value = 1.0 - value_a.truth + value_b.truth;
  121.     return ((temp_value < 1.0) ? temp_value : 1.0);
  122. }
  123.  
  124. fuzzy fuzzy_iff(const fuzzy& value_a, 
  125.                 const fuzzy& value_b)
  126. {
  127.     return 1.0 - fabs(value_a.truth - value_b.truth);
  128. }
  129.  
  130. inline int operator<(const fuzzy& value_a, 
  131.                      const fuzzy& value_b)
  132. {
  133.     return (value_a.truth < value_b.truth);
  134. }
  135.  
  136. inline int operator<=(const fuzzy& value_a, 
  137.                       const fuzzy& value_b)
  138. {
  139.     return (value_a.truth <= value_b.truth);
  140. }
  141.  
  142. inline int operator==(const fuzzy& value_a,
  143.                       const fuzzy& value_b)
  144. {
  145.     return (value_a.truth == value_b.truth);
  146. }
  147.  
  148. inline int operator>=(const fuzzy& value_a,
  149.                       const fuzzy& value_b)
  150. {
  151.     return (value_a.truth >= value_b.truth);
  152. }
  153.  
  154. inline int operator>(const fuzzy& value_a,
  155.                      const fuzzy& value_b)
  156. {
  157.     return (value_a.truth > value_b.truth);
  158. }
  159.  
  160. inline int operator!=(const fuzzy& value_a,
  161.                       const fuzzy& value_b)
  162. {
  163.     return (value_a.truth != value_b.truth);
  164. }
  165.  
  166. ostream& operator<<(ostream& s, const fuzzy& z)
  167. {
  168.     return s << z.truth;
  169. }
  170.  
  171. istream& operator>>(istream& s, fuzzy& fur)
  172. {
  173.     auto double lint = 0.0;
  174.     
  175.     s >> lint;
  176.     fur = lint;
  177.  
  178.     return s;
  179. }
  180.  
  181.