万分数封装类

  1 using System.Diagnostics;
  2 using System;
  3 using System.Collections.Generic;
  4 
  5 public class WanFraction
  6 {
  7     public WanFraction()
  8     {
  9         this.Numerator = 0;
 10     }
 11 
 12     public WanFraction(WanFraction other)
 13     {
 14         this.Numerator = other.Numerator;
 15     }
 16 
 17     public WanFraction(int intValue)
 18     {
 19         this.Numerator = intValue;
 20     }
 21 
 22     public static WanFraction operator *(WanFraction self, WanFraction other)
 23     {
 24         WanFraction result = new WanFraction(self);
 25         result.Numerator *= other.Numerator;
 26         result.Numerator /= Common.WAN_FRACTION;
 27         return result;
 28     }
 29 
 30     public static WanFraction operator /(WanFraction self, WanFraction other)
 31     {
 32         if (other.Numerator == 0)
 33             return null;
 34         else
 35         {
 36             WanFraction result = new WanFraction();
 37             result.Numerator = self.Numerator * Common.WAN_FRACTION / other.Numerator;
 38             return result;
 39         }
 40     }
 41 
 42     public static WanFraction operator *(int other, WanFraction self)
 43     {
 44         return self * other;
 45     }
 46 
 47     public static WanFraction operator *(WanFraction self, int other)
 48     {
 49         WanFraction result = new WanFraction(self);
 50         result.Numerator *= other;
 51         return result;
 52     }
 53 
 54     public static WanFraction operator /(WanFraction self, int other)
 55     {
 56         if (other == 0)
 57             return null;
 58         else
 59         {
 60             WanFraction result = new WanFraction(self);
 61             result.Numerator /= other;
 62             return result;
 63         }
 64     }
 65 
 66     public static WanFraction operator /(int other, WanFraction self)
 67     {
 68         if (self.Numerator == 0)
 69             return null;
 70         else
 71         {
 72             WanFraction result = new WanFraction(other*Common.WAN_FRACTION);
 73             result /= self;
 74             return result;
 75         }
 76     }
 77 
 78     public static WanFraction operator -(WanFraction self, WanFraction other)
 79     {
 80             WanFraction result = new WanFraction(self);
 81             result.Numerator-= self.Numerator;
 82             return result;
 83     }
 84 
 85     public static WanFraction operator +(WanFraction self, WanFraction other)
 86     {
 87         WanFraction result = new WanFraction(self);
 88         result.Numerator += other.Numerator;
 89         return result;
 90     }
 91 
 92     public static WanFraction operator +(WanFraction self, int other)
 93     {
 94         WanFraction result = new WanFraction(self);
 95         result.Numerator += other*Common.WAN_FRACTION;
 96         return result;
 97     }
 98 
 99     public static WanFraction operator +(int other,WanFraction self)
100     {
101         return self+other;
102     }
103 
104     public int ToInt()
105     {
106         return Numerator/Common.WAN_FRACTION;
107     }
108 
109     public static int Compare(WanFraction a, WanFraction b)
110     {
111         return a.Numerator - b.Numerator;
112     }
113 
114     public static bool operator==(WanFraction a, WanFraction b)
115     {
116         return WanFraction.Compare(a, b) == 0;
117     }
118 
119     public override bool Equals(object other)
120     {
121         if (other is WanFraction)
122             return WanFraction.Compare(this, other as WanFraction) == 0;
123         else
124             return false;
125     }
126 
127     public bool Equals(WanFraction other)
128     {
129         return WanFraction.Compare(this, other) == 0;
130     }
131 
132     public override int GetHashCode()
133     {
134         return this.Numerator;
135     }
136 
137     public static bool operator !=(WanFraction a, WanFraction b)
138     {
139         return WanFraction.Compare(a, b) != 0;
140     }
141 
142     public static bool operator <(WanFraction a, WanFraction b)
143     {
144         return WanFraction.Compare(a, b) < 0;
145     }
146 
147     public static bool operator >(WanFraction a, WanFraction b)
148     {
149         return WanFraction.Compare(a, b) > 0;
150     }
151 
152     public int Numerator
153     {
154         get;
155         protected set;
156     }
157 
158 }

 

posted @ 2014-09-02 13:40  dx2019  阅读(423)  评论(0编辑  收藏  举报