游戏金币单位换算管理类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//金币单位
public enum GoldUnit
{
    N = 0,
    K,
    M,
    B,
    T,
    AA,
    BB,
    CC,
    DD,
    EE,
    FF,
    GG,
    HH,
    II,
    JJ,
    KK,
    LL,
    MM,
    NN,
    OO,
    PP,
    QQ,
    RR,
    SS,
    TT,
    UU,
    VV,
    WW,
    XX,
    YY,
    ZZ,
}

/// <summary>
/// 金币数量
/// </summary>
public class GoldNum
{
    //构造函数
    private string goldCountText;
    public string GoldCountText
    {
        get
        {
            if (goldUnit == 0)
            {
                goldCountText = ((int)goldValue).ToString();
            }
            else
            {
                float tempValue = (float)decimal.Round((decimal)goldValue, 2);
                goldCountText = tempValue + ((GoldUnit)goldUnit).ToString();
            }
            return goldCountText;
        }
        set { goldCountText = value; }
    }

    internal int goldUnit;
    internal float goldValue;
    public GoldNum(string value)
    {
        value = value.Trim();  //删除字符串首部和尾部的空格
        bool isSingleDigit = true;
        for (int i = 30; i >= 0; i--)
        {
            if (value.EndsWith(((GoldUnit)i).ToString()))
            {
                value = value.Trim(((GoldUnit)i).ToString().ToCharArray());
                isSingleDigit = false;
                goldUnit = i;
                goldValue = float.Parse(value);
                break;
            }
        }

        if (isSingleDigit)
        {
            goldUnit = 0;
            goldValue = float.Parse(value);
        }

        while (goldValue > 500 && goldUnit < 30)
        {
            goldUnit++;
            goldValue /= 1000;
        }
        while (goldValue > 0 && goldValue < 1 && goldUnit > 0)
        {
            goldUnit--;
            goldValue *= 1000;
        }
    }

    //运算符重写
    /*operator是C#、C++和pascal的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator和运算符整体上视为一个函数名。
    重载运算符的函数一般格式如下

    函数类型    operator  运算符名称    (形参表列)

    {对运算符的重载处理}

    例如,想将“+”用于Complex(复数)的加法运算,函数的原型可以是这样的:


    Complex operator + (Complex & c1,Complex &c2);

    operator+函数表示对运算符+重载。
    其中,operator是关键字,专门用于定义重载运算符的函数的,运算符名称就是C++提供给用户的预定运算符。

    注意:函数名是由operator和运算符组成。
*/

    public static GoldNum operator +(GoldNum A, GoldNum B)
    {
        int limit = 4;
        int tempUnit = 0;
        float tempValue = 0;

        if (A.goldUnit == B.goldUnit)
        {
            tempUnit = A.goldUnit;
            tempValue = A.goldValue + B.goldValue;
        }
        else if (A.goldUnit > B.goldUnit)
        {
            if (A.goldUnit - B.goldUnit <= limit)
            {
                tempUnit = A.goldUnit;
                tempValue = A.goldValue + B.goldValue / Mathf.Pow(1000, A.goldUnit - B.goldUnit);

            }
            else if (A.goldUnit - B.goldUnit > limit)
            {
                tempUnit = A.goldUnit;
                tempValue = A.goldValue;
            }
        }
        else if (A.goldUnit < B.goldUnit)
        {
            if (B.goldUnit - A.goldUnit <= limit)
            {
                tempUnit = B.goldUnit;
                tempValue = A.goldValue / Mathf.Pow(1000, B.goldUnit - A.goldUnit) + B.goldValue;
            }
            else if (B.goldUnit - A.goldUnit > limit)
            {
                tempUnit = B.goldUnit;
                tempValue = B.goldValue;
            }
        }

        GoldNum result = new GoldNum(tempValue + ((GoldUnit)tempUnit).ToString());
        return result;
    }
    public static GoldNum operator -(GoldNum A, GoldNum B)
    {
        int limit = 4;
        int tempUnit = 0;
        float tempValue = 0;

        if (A.goldUnit == B.goldUnit)
        {
            tempUnit = A.goldUnit;
            tempValue = A.goldValue - B.goldValue;
        }
        else if (A.goldUnit > B.goldUnit)
        {
            if (A.goldUnit - B.goldUnit <= limit)
            {
                tempUnit = A.goldUnit;
                tempValue = A.goldValue - B.goldValue / Mathf.Pow(1000, A.goldUnit - B.goldUnit);
            }
            else if (A.goldUnit - B.goldUnit > limit)
            {
                tempUnit = A.goldUnit;
                tempValue = A.goldValue;
            }
        }
        else if (A.goldUnit < B.goldUnit)
        {
            if (B.goldUnit - A.goldUnit <= limit)
            {
                tempUnit = B.goldUnit;
                tempValue = A.goldValue / Mathf.Pow(1000, B.goldUnit - A.goldUnit) - B.goldValue;
            }
            else if (B.goldUnit - A.goldUnit > limit)
            {
                tempUnit = B.goldUnit;
                tempValue = -B.goldValue;
            }
        }

        GoldNum result = new GoldNum(tempValue + ((GoldUnit)tempUnit).ToString());
        return result;
    }
    public static bool operator >(GoldNum A, GoldNum B)
    {
        GoldNum result = B - A;
        return result.GoldCountText.StartsWith("-");
    }
    public static bool operator <(GoldNum A, GoldNum B)
    {
        GoldNum result = A - B;
        return result.GoldCountText.StartsWith("-");
    }
    public static bool operator >=(GoldNum A, GoldNum B)
    {
        GoldNum result = A - B;
        return !result.GoldCountText.StartsWith("-") || result.GoldCountText == "0";
    }
    public static bool operator <=(GoldNum A, GoldNum B)
    {
        GoldNum result = A - B;
        return result.GoldCountText.StartsWith("-") || result.GoldCountText == "0";
    }
    public static GoldNum operator *(GoldNum A, float B)
    {
        int tempUnit = A.goldUnit;
        float tempValue = A.goldValue * B;
        GoldNum result = new GoldNum(tempValue + ((GoldUnit)tempUnit).ToString());
        return result;
    }
    public static GoldNum operator /(GoldNum A, float B)
    {
        int tempUnit = A.goldUnit;
        float tempValue = A.goldValue / B;
        GoldNum result = new GoldNum(tempValue + ((GoldUnit)tempUnit).ToString());
        return result;
    }
    public static GoldNum operator *(GoldNum A, GoldNum B)
    {
        int tempUnit = A.goldUnit + B.goldUnit;
        float tempValue = A.goldValue * B.goldValue;
        GoldNum result = new GoldNum(tempValue + ((GoldUnit)tempUnit).ToString());
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值