一个分数结构体

提供了各种运算符并实现了IComparable接口,可以像使用系统自带数据类型一样方便地使用分数.

提供将字符串转化为分数以及将分数转化为小数的方法.

PS:求小数转化为分数的算法~~~

// Version 0.9.7
using  System;
using  IntType  =  System.Int64;     // 整形数和小数的类型可根据需要定义
using  RealType  =  System.Double;
namespace  Takamachi660.Math
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class FractionException : System.Exception 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public FractionException(string msg) : base(msg) { }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public FractionException() : base() { }
    }

    [Serializable]
    
public struct Fraction : IComparable<Fraction>, IComparable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private IntType iNum, iDen; //分子/分母,私有字段
ExpandedSubBlockStart.gifContractedSubBlock.gif
        public IntType iNumerator get return iNum; } }   //外部通过只读属性获取分子和分母的值
ExpandedSubBlockStart.gifContractedSubBlock.gif
        public IntType iDenominator get return iDen; } }
        
public Fraction(Fraction value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.iNum = value.iNum;
            
this.iDen = value.iDen;
            Simplify();
        }

        
public Fraction(IntType iNum, IntType iDen)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.iNum = iNum;
            
this.iDen = iDen;
            Simplify();
        }

        
public Fraction(string s)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (string.IsNullOrEmpty(s))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.iNum = 0;
                
this.iDen = 1;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this = ConvertFromString(s);
            }

        }

        
private void DenZeroCheck()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (this.iDen == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw new FractionException("分母为\"0\"");
            }

        }

        
private Fraction Simplify()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            DenZeroCheck();
            
if (iDen < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                iDen 
*= -1;
                iNum 
*= -1;
            }

            IntType cm 
= CommonDivisor(Math.Abs(iNum), iDen);
            
if (cm == 1)
                
return this;
            iNum 
/= cm;
            iDen 
/= cm;
            
return this;
        }

        
public override int GetHashCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Simplify();
            
return ((double)iNum / (double)iDen).GetHashCode();
        }

        
public override bool Equals(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (obj is Fraction)
                
return iNum == ((Fraction)obj).iNum && iDen == ((Fraction)obj).iDen;
            
return false;
        }

        
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string strOutput;
            Simplify();
            
if (iDen != 1)
                strOutput 
= string.Format(@"{0}/{1}", iNum, iDen);
            
else
                strOutput 
= string.Format(@"{0}", iNum);
            
return strOutput;
        }

        
public RealType ToReal()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return (RealType)this.iNum / (RealType)this.iDen;
        }

        
private static IntType CommonDivisor(IntType m, IntType n)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            m 
= m < 0 ? -m : m;
            n 
= n < 0 ? -n : n;
            IntType temp;
            
if (n < m)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                temp 
= n;
                n 
= m;
                m 
= temp;
            }

            
while (m != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                temp 
= n % m;
                n 
= m;
                m 
= temp;
            }

            
return n;
        }

        
private static IntType CommonMultiple(IntType m, IntType n)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            IntType p 
= Math.Abs(n * m);
            n 
= CommonDivisor(m, n);
            
return p / n;
        }

        
public void SetValue(IntType iNum, IntType iDen)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.iNum = iNum;
            
this.iDen = iDen;
            Simplify();
        }

        
public void SetValue(string s)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this = ConvertFromString(s);
        }

        
public static Fraction ConvertFromString(string s)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Fraction output;
            
if (!Fraction.TryParse(s, out output))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
throw new FractionException(string.Format("无法将字符串\"{0}\"转化为分数", s));
            }

            
return output;
        }

        
public static bool TryParse(string s, out Fraction result)  //将字符串转化为分数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        {
            s.Trim();
            
if (s.IndexOf(@"/"< 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                result.iDen 
= 1;
                
if (!IntType.TryParse(s, out result.iNum))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    result.iNum 
= 0;
                    
return false;
                }

                
return true;
            }

            
if (s.IndexOf(@"/"!= s.LastIndexOf(@"/"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                result.iNum 
= 0;
                result.iDen 
= 1;
                
return false;
            }

            
string[] temp = s.Split('/');
            
if (!IntType.TryParse(temp[0], out result.iNum) || !IntType.TryParse(temp[1], out result.iDen))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                result.iNum 
= 0;
                result.iDen 
= 1;
                
return false;
            }

            result.Simplify();
            
return true;
        }

        
//重载比较运算符
        public static bool operator ==(Fraction a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a.GetHashCode() == b.GetHashCode();
        }

        
public static bool operator ==(IntType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//return new Fraction(a, 1) == b;
            return a == b.ToReal();
        }

        
public static bool operator ==(Fraction a, IntType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return b == a;
        }

        
public static bool operator ==(RealType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a == b.ToReal();
        }

        
public static bool operator ==(Fraction a, RealType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return b == a;
        }

        
public static bool operator !=(Fraction a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return !(a == b);
        }

        
public static bool operator !=(IntType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return !(a == b);
        }

        
public static bool operator !=(Fraction a, IntType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return !(a == b);
        }

        
public static bool operator !=(RealType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return !(a == b);
        }

        
public static bool operator !=(Fraction a, RealType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return !(a == b);
        }

        
public static bool operator >(Fraction a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (a.GetHashCode() == b.GetHashCode())
                
return false;
            IntType cm 
= CommonMultiple(a.iDen, b.iDen);//分母的最小公倍数
            IntType an = a.iNum * (cm / a.iDen);//a的分子
            IntType bn = b.iNum * (cm / b.iDen);//b的分子
            return an > bn;
        }

        
public static bool operator >(IntType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//return new Fraction(a, 1) > b;
            return a > b.ToReal();
        }

        
public static bool operator >(Fraction a, IntType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//return a > new Fraction(b, 1);
            return a.ToReal() > b;
        }

        
public static bool operator >(RealType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a > b.ToReal();
        }

        
public static bool operator >(Fraction a, RealType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a.ToReal() > b;
        }

        
public static bool operator <(Fraction a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (a.GetHashCode() == b.GetHashCode())
                
return false;
            IntType cm 
= CommonMultiple(a.iDen, b.iDen);//分母的最小公倍数
            IntType an = a.iNum * (cm / a.iDen);//a的分子
            IntType bn = b.iNum * (cm / b.iDen);//b的分子
            return an < bn;
        }

        
public static bool operator <(IntType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//return new Fraction(a, 1) < b;
            return a < b.ToReal();
        }

        
public static bool operator <(Fraction a, IntType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//return a < new Fraction(b, 1);
            return a.ToReal() < b;
        }

        
public static bool operator <(RealType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a < b.ToReal();
        }

        
public static bool operator <(Fraction a, RealType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a.ToReal() < b;
        }

        
public static bool operator >=(Fraction a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (a.GetHashCode() == b.GetHashCode())
                
return true;
            IntType cm 
= CommonMultiple(a.iDen, b.iDen);//分母的最小公倍数
            IntType an = a.iNum * (cm / a.iDen);//a的分子
            IntType bn = b.iNum * (cm / b.iDen);//b的分子
            return an >= bn;
        }

        
public static bool operator >=(IntType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//return new Fraction(a, 1) >= b;
            return a >= b.ToReal();
        }

        
public static bool operator >=(Fraction a, IntType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//return a >= new Fraction(b, 1);
            return a.ToReal() >= b;
        }

        
public static bool operator >=(RealType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a >= b.ToReal();
        }

        
public static bool operator >=(Fraction a, RealType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a.ToReal() >= b;
        }

        
public static bool operator <=(Fraction a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (a.GetHashCode() == b.GetHashCode())
                
return true;
            IntType cm 
= CommonMultiple(a.iDen, b.iDen);//分母的最小公倍数
            IntType an = a.iNum * (cm / a.iDen);//a的分子
            IntType bn = b.iNum * (cm / b.iDen);//b的分子
            return an <= bn;
        }

        
public static bool operator <=(IntType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//return new Fraction(a, 1) <= b;
            return a <= b.ToReal();
        }

        
public static bool operator <=(Fraction a, IntType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//return a <= new Fraction(b, 1);
            return a.ToReal() <= b;
        }

        
public static bool operator <=(RealType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a <= b.ToReal();
        }

        
public static bool operator <=(Fraction a, RealType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a.ToReal() <= b;
        }

        
//重载四则运算符
        public static Fraction operator +(Fraction a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            a.DenZeroCheck();
            b.DenZeroCheck();
            IntType cm 
= CommonMultiple(a.iDen, b.iDen);//分母的最小公倍数
            IntType an = a.iNum * (cm / a.iDen);//a的分子
            IntType bn = b.iNum * (cm / b.iDen);//b的分子
            return new Fraction(an + bn, cm);
        }

        
public static Fraction operator +(IntType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return (new Fraction(a, 1+ b);
        }

        
public static Fraction operator +(Fraction a, IntType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return b + a;
        }

        
public static Fraction operator -(Fraction obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            obj.iNum 
*= -1;
            
return obj;
        }

        
public static Fraction operator -(Fraction a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a + (-b);
        }

        
public static Fraction operator -(IntType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a + (-b);
        }

        
public static Fraction operator -(Fraction a, IntType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a + (-b);
        }

        
public static Fraction operator *(Fraction a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return new Fraction(a.iNum * b.iNum, a.iDen * b.iDen);
        }

        
public static Fraction operator *(IntType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return new Fraction(a, 1* b;
        }

        
public static Fraction operator *(Fraction a, IntType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return b * a;
        }

        
public static Fraction operator !(Fraction obj)//求倒数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        {
            
if (obj == 0)
                
throw new FractionException("无法求\"0\"的倒数");
            
return new Fraction(obj.iDen, obj.iNum);//交换分子与分母
        }

        
public static Fraction operator /(Fraction a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (b == 0)
                
throw new FractionException("\"0\"作为除数");
            
return a * !b;
        }

        
public static Fraction operator /(IntType a, Fraction b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (b == 0)
                
throw new FractionException("\"0\"作为除数");
            
return a * !b;
        }

        
public static Fraction operator /(Fraction a, IntType b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (b == 0)
                
throw new FractionException("\"0\"作为除数");
            a.iDen 
*= b;
            
return a.Simplify();
        }

        
public static Fraction operator |(Fraction a, Fraction b)//求最大公约数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        {
            
return new Fraction(CommonDivisor(a.iNum, b.iNum), CommonMultiple(a.iDen, b.iDen));
        }

        
public static Fraction operator &(Fraction a, Fraction b)//求最小公倍数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        {
            
return new Fraction(CommonMultiple(a.iNum, b.iNum), CommonDivisor(a.iDen, b.iDen));
        }

        
int IComparable<Fraction>.CompareTo(Fraction other)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Fraction dis 
= this - other;
            
return (int)(dis.iNum / dis.iDen);
        }

        
int IComparable.CompareTo(object other)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (other is IntType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return (int)((this - (IntType)other).ToReal());
            }

            
else if (other is Fraction)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return (this as IComparable<Fraction>).CompareTo((Fraction)other);
            }

            
else if (other is RealType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return (int)(this.ToReal() - (RealType)other);
            }

            
else
                
return 0;
        }

    }

}

 

转载于:https://www.cnblogs.com/takamachi660/archive/2009/08/05/1539344.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值