(C#)分数的重载运算

2018-04-10  本文已影响0人  JCone

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

///

/// 分数的加减乘除运算符重载

///

public class Rfloat

{

    private float fNumerator = 0f;

    private float fDenominator = 0f;

    public Rfloat(float numerator, float denominator)

    {

        fNumerator = numerator;

        fDenominator = denominator;

    }

    public static Rfloat operator +(Rfloat a, Rfloat b)

    {

        var denominator = a.fDenominator * b.fDenominator;

        var numerator = a.fNumerator * b.fDenominator + b.fNumerator * a.fDenominator;

        return new Rfloat(numerator, denominator);

    }

    public static Rfloat operator -(Rfloat a, Rfloat b)

    {

        var denominator = a.fDenominator * b.fDenominator;

        var numerator = a.fNumerator * b.fDenominator - b.fNumerator * a.fDenominator;

        return new Rfloat(numerator, denominator);

    }

    public static Rfloat operator *(Rfloat a, Rfloat b)

    {

        var denominator = a.fDenominator * b.fDenominator;

        var numerator = a.fNumerator * b.fNumerator;

        return new Rfloat(numerator, denominator);

    }

    public static Rfloat operator /(Rfloat a, Rfloat b)

    {

        var denominator = a.fDenominator * b.fNumerator;

        var numerator = a.fNumerator * b.fDenominator;

        return new Rfloat(numerator, denominator);

    }

    public float GetFloat()

    {

        return fNumerator / fDenominator;

    }

}

上一篇 下一篇

猜你喜欢

热点阅读