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; } }