关于C#的大数值处理

using System;
using System.Text.RegularExpressions;

[System.Serializable]
public class Int :IComparable
{
public string Body = "0";

public static readonly Int Zero = new Int("0");

public int Length
{
get
{
if ((int)this.Body[0] == 43 || (int)this.Body[0] == 45)
return this.Body.Length - 1;
return this.Body.Length;
}
}

public int this[int index]
{
get
{
if ((int)this.Body[0] == 43 || (int)this.Body[0] == 45)
return (int)this.Body[index + 1] - 48;
return (int)this.Body[index] - 48;
}
}

public bool IsPositive
{
get
{
return (int)this.Body[0] != 45 || !(new Int(this.Body.Substring(1)) != (Int)0L);
}
}

public Int()
{
}

public Int(string numStr)
{
if (string.IsNullOrEmpty(numStr))
this.Body = "0";
else
this.Body = this.Trim(numStr);
}

public static implicit operator string(Int source)
{
return source.Body;
}

public static implicit operator Int(long real)
{
return new Int(real.ToString());
}

public static implicit operator Int(string real)
{
return new Int(real);
}

public static bool operator >(Int left, Int right)
{
if (left.IsPositive && !right.IsPositive && left.Absolute().Body != "0" && right.Absolute().Body != "0")
return true;
if (left.IsPositive && right.IsPositive)
{
if (left.Length > right.Length)
return true;
if (left.Length != right.Length)
return false;
for (int index = 0; index < left.Length; ++index)
{
if (left[index] > right[index])
return true;
if (left[index] < right[index])
return false;
}
}
else if (!left.IsPositive && !right.IsPositive)
{
if (left.Length < right.Length)
return true;
if (left.Length != right.Length)
return false;
for (int index = 0; index < left.Length; ++index)
{
if (left[index] < right[index])
return true;
if (left[index] > right[index])
return false;
}
}
else if (left.IsPositive && !right.IsPositive && (left.Absolute().Body == "0" && right.Absolute().Body != "0" || left.Absolute().Body != "0" && right.Absolute().Body == "0"))
return true;
return false;
}

public static bool operator <(Int left, Int right)
{
return right > left;
}

public static bool operator >=(Int left, Int right)
{
return left > right || left == right;
}

public static bool operator <=(Int left, Int right)
{
return left < right || left == right;
}

public static bool operator ==(Int left, Int right)
{
return !(left > right) && !(left < right);
}

public static bool operator !=(Int left, Int right)
{
return left > right || left < right;
}

public static bool operator >(Int left, object right)
{
return left > new Int(right.ToString());
}

public static bool operator >(object left, Int right)
{
return new Int(left.ToString()) > right;
}

public static bool operator <(Int left, object right)
{
return left < new Int(right.ToString());
}

public static bool operator <(object left, Int right)
{
return new Int(left.ToString()) < right;
}

public static bool operator >=(Int left, object right)
{
return left >= new Int(right.ToString());
}

public static bool operator >=(object left, Int right)
{
return new Int(left.ToString()) >= right;
}

public static bool operator <=(Int left, object right)
{
return left <= new Int(right.ToString());
}

public static bool operator <=(object left, Int right)
{
return new Int(left.ToString()) <= right;
}

public static bool operator ==(Int left, object right)
{
return left == new Int(right.ToString());
}

public static bool operator ==(object left, Int right)
{
return new Int(left.ToString()) == right;
}

public static bool operator !=(Int left, object right)
{
return left != new Int(right.ToString());
}

public static bool operator !=(object left, Int right)
{
return new Int(left.ToString()) != right;
}
public static string operator +(string left, Int right) {
return string.Format("{0}{1}",left,right.Body);
}

public static string operator + (Int right ,string left )
{
return string.Format("{0}{1}", right.Body,left);
}

public static Int operator +(Int left, Int right)
{
Int Int1 = new Int();
if (left.IsPositive && right.IsPositive)
{
int length = right.Length;
if (left.Length > right.Length)
length = left.Length;
int num1 = 0;
for (int index = 0; index < length; ++index)
{
int num2 = index >= left.Length || index >= right.Length ? (index < left.Length ? (index < right.Length ? 0 : left[left.Length - 1 - index] + num1) : right[right.Length - 1 - index] + num1) : left[left.Length - 1 - index] + right[right.Length - 1 - index] + num1;
num1 = num2 / 10;
int num3;
if (index == 0)
{
Int Int2 = Int1;
num3 = num2 % 10;
string str = num3.ToString();
Int2.Body = str;
}
else
{
Int Int2 = Int1;
string str1 = Int1.Body;
int startIndex = 0;
num3 = num2 % 10;
string str2 = num3.ToString();
string str3 = str1.Insert(startIndex, str2);
Int2.Body = str3;
}
}
Int1.Body = Int1.Body.Insert(0, num1.ToString());
}
else if (left.IsPositive && !right.IsPositive)
Int1 = left - new Int((string)right.Absolute());
else if (!left.IsPositive && right.IsPositive)
Int1 = right - new Int((string)left.Absolute());
else if (!left.IsPositive && !right.IsPositive)
Int1 = left - new Int((string)right.Absolute());
Int1.Trim();
return Int1;
}

public static Int operator -(Int left, Int right)
{
Int Int = new Int();
if (left == right)
return new Int("0");
if (left > Int.Zero && right == Int.Zero)
return left;
if (left == Int.Zero && right < Int.Zero)
return new Int((string)right.Absolute());
if (left == Int.Zero && right > Int.Zero)
return new Int(right.Body.Insert(0, "-"));
if (left > Int.Zero && right > Int.Zero)
{
if (left > right)
{
int num1 = 0;
for (int index = 0; index < left.Length; ++index)
{
int num2 = index >= left.Length || index >= right.Length ? (index < right.Length ? 0 : left[left.Length - 1 - index] - num1) : left[left.Length - 1 - index] - right[right.Length - 1 - index] - num1;
num1 = num2 / 10;
if (num2 < 0)
{
num1 = 1;
num2 = 10 + num2;
}
Int.Body = index != 0 ? Int.Body.Insert(0, num2.ToString()) : num2.ToString();
}
}
else if (left < right)
{
Int = right - left;
Int.Body = Int.Body.Insert(0, "-");
}
}
else if (left < Int.Zero && right < Int.Zero)
Int = new Int((string)right.Absolute()) - new Int((string)left.Absolute());
else if (left > Int.Zero && right < Int.Zero)
Int = left + new Int((string)right.Absolute());
else if (left < Int.Zero && right > Int.Zero)
{
Int = new Int((string)left.Absolute()) + right;
Int.Body = Int.Body.Insert(0, "-");
}
Int.Trim();
return Int;
}

public static Int operator *(Int left, Int right)
{
Int Int = new Int();
if (left == Int.Zero || right == Int.Zero)
return Int.Zero;
if (left.IsPositive && right.IsPositive)
{
for (int index1 = right.Length - 1; index1 >= 0; --index1)
{
int num1 = 0;
string numStr = "";
for (int index2 = left.Length - 1; index2 >= 0; --index2)
{
int num2 = right.Absolute()[index1] * left.Absolute()[index2] + num1;
numStr = numStr.Insert(0, (num2 % 10).ToString());
num1 = num2 / 10;
}
if ((uint)num1 > 0U)
numStr = numStr.Insert(0, num1.ToString());
for (int index2 = 0; index2 < right.Length - 1 - index1; ++index2)
numStr += "0";
Int += new Int(numStr);
}
}
else if (!left.IsPositive && right.IsPositive || left.IsPositive && !right.IsPositive)
{
Int = new Int((string)left.Absolute()) * new Int((string)right.Absolute());
Int.Body = Int.Body.Insert(0, "-");
}
else if (!left.IsPositive && !right.IsPositive)
Int = new Int((string)left.Absolute()) * new Int((string)right.Absolute());
Int.Trim();
return Int;
}

public static Int operator /(Int left, Int right)
{
Int Int1 = new Int();
if (right == Int.Zero)
throw new Exception("除零错误!");
if (left == Int.Zero)
return Int.Zero;
if (right == new Int("1"))
return left;
if (left.IsPositive && right.IsPositive)
{
if (left < right)
{
Int1 = Int.Zero;
}
else
{
string numStr = "";
Int Int2 = new Int(left.Body.Substring(0, right.Length - 1));
for (int index = right.Length - 1; index < left.Length; ++index)
{
Int2 = Int2 * new Int("10") + new Int(left[index].ToString());
Int2.Trim();
int num = 0;
for (num = 0; num < 10; ++num)
{
Int Int3 = right * new Int(num.ToString());
if (Int3 > Int2)
{
numStr += (num - 1);
Int2 -= right * new Int((num - 1).ToString());
break;
}
if (Int3 == Int2)
{
numStr += num;
Int2 = Int.Zero;
break;
}
}
if (num == 10)
{
numStr += (num - 1);
Int2 -= right * new Int((num - 1).ToString());
}
}
Int1 = new Int(numStr);
}
}
else if (!left.IsPositive && right.IsPositive || left.IsPositive && !right.IsPositive)
{
Int1 = new Int((string)left.Absolute()) / new Int((string)right.Absolute());
Int1.Body = Int1.Body.Insert(0, "-");
}
else if (!left.IsPositive && !right.IsPositive)
Int1 = new Int((string)left.Absolute()) / new Int((string)right.Absolute());
Int1.Trim();
return Int1;
}

public static Int operator %(Int left, Int right)
{
Int Int1 = new Int();
Int Int2 = left - left / right * right;
Int2.Trim();
return Int2;
}

public static Int operator +(Int left, object right)
{
return left.Body + new Int(right.ToString());
}

public static Int operator +(object left, Int right)
{
return new Int(left.ToString()) + right.Body;
}

public static Int operator -(Int left, object right)
{
return left - new Int(right.ToString());
}

public static Int operator -(object left, Int right)
{
return new Int(left.ToString()) - right;
}

public static Int operator *(Int left, object right)
{
return left * new Int(right.ToString());
}

public static Int operator *(object left, Int right)
{
return new Int(left.ToString()) * right;
}

public static Int operator /(Int left, object right)
{
return left / new Int(right.ToString());
}

public static Int operator /(object left, Int right)
{
return new Int(left.ToString()) / right;
}

public static Int operator %(Int left, object right)
{
return left % new Int(right.ToString());
}

public static Int operator %(object left, Int right)
{
return new Int(left.ToString()) % right;
}

public Int Absolute()
{
if ((int)this.Body[0] == 43 || (int)this.Body[0] == 45)
return new Int(this.Body.Substring(1));
return this;
}

public void Trim()
{
this.Body = this.Trim(this.Body);
}

public string Trim(string numStr)
{
string str1 = numStr.Trim();
if ((int)str1[0] == 43)
str1 = str1.Substring(1);
string str2 = str1;
if ((int)str1[0] == 45)
str2 = str1.Substring(1);
int index = 0;
while (index < str2.Length && (int)str2[index] == 48)
++index;
if (index == str2.Length)
return "0";
bool flag = true;
if ((int)str1[0] == 45)
flag = false;
int startIndex = 0;
while (startIndex < str2.Length && (int)str2[startIndex] == 48)
++startIndex;
string str3 = str2.Substring(startIndex);
if (!flag)
str3 = str3.Insert(0, "-");
return str3;
}

public override string ToString()
{
return this.Body;
}

public int CompareTo(object obj)
{
return this == (Int)obj ? 0 : this - (Int)obj > 0 ? 1 : -1;
}

public Float ToFloat()
{
return new Float(this.Body);
}

public override bool Equals(object obj)
{
return base.Equals(obj);
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}

[System.Serializable]
public class Float : Int
{
public static int DividResaultPrecision = 50;
private long DotIndex = 0;
private string BodyNoDotNoSignal = "0";

public string IntPart
{
get
{
if (this.DotIndex <= 0L)
return "0";
return this.BodyNoDotNoSignal.Substring(0, (int)this.DotIndex);
}
}

public string FloatPart
{
get
{
if (this.DotIndex == (long)this.BodyNoDotNoSignal.Length)
return "0";
if (this.DotIndex == 0L)
return this.BodyNoDotNoSignal;
if (this.DotIndex >= 0L)
return this.BodyNoDotNoSignal.Substring((int)this.DotIndex);
string str = this.BodyNoDotNoSignal;
for (int index = 0; (long)index < -this.DotIndex; ++index)
str = str.Insert(0, "0");
return str;
}
}

public int IntPartLength
{
get
{
return this.IntPart.Length;
}
}

public int FloatPartLength
{
get
{
if (this.FloatPart.Length == 1 && (int)this.FloatPart[0] == 48)
return 0;
return this.FloatPart.Length;
}
}

public Float()
{
this.DotIndex = 1L;
this.Body = "0";
this.BodyNoDotNoSignal = "0";
}

public Float(string body)
{
if (string.IsNullOrEmpty(body))
this.Body = "0";
else
this.Body = this.Trim(body);
Match match = Regex.Match(this.Body, "[1-9]");
int num = this.Body.IndexOf(‘.‘);
this.DotIndex = num < 0 ? (long)(this.Body.Length - match.Index) : (num <= match.Index ? (long)(num - match.Index + 1) : (long)(num - match.Index));
this.BodyNoDotNoSignal = this.Body.Substring(match.Index);
int startIndex = this.BodyNoDotNoSignal.IndexOf(‘.‘);
if (startIndex >= 0)
this.BodyNoDotNoSignal = this.BodyNoDotNoSignal.Remove(startIndex, 1);
this.BodyNoDotNoSignal = base.Trim(this.BodyNoDotNoSignal);
}

public static implicit operator string(Float source)
{
return source.Body;
}

public static implicit operator Float(long real)
{
return new Float(real.ToString());
}

public static implicit operator Float(string real)
{
return new Float(real);
}

public static implicit operator Float(float real)
{
return new Float(real.ToString());
}

public static bool operator >(Float left, Float right)
{
if (left.IsPositive && right.IsPositive)
{
if (new Int(left.IntPart) > new Int(right.IntPart))
return true;
int length1 = left.FloatPart.Length;
int length2 = right.FloatPart.Length;
string floatPart1 = left.FloatPart;
string floatPart2 = right.FloatPart;
if (length1 > length2)
{
for (int index = length2; index < length1; ++index)
floatPart2 += "0";
}
else if (length1 < length2)
{
for (int index = length1; index < length2; ++index)
floatPart1 += "0";
}
return new Int(floatPart1) > new Int(floatPart2);
}
return left.IsPositive && !right.IsPositive || (left.IsPositive || !right.IsPositive) && (!left.IsPositive && !right.IsPositive && new Float((string)right.Absolute()) > new Float((string)left.Absolute()));
}

public static bool operator <(Float left, Float right)
{
return right > left;
}

public static bool operator ==(Float left, Float right)
{
return !(left > right) && !(left < right);
}

public static bool operator !=(Float left, Float right)
{
return left > right || left < right;
}

public static bool operator >=(Float left, Float right)
{
return left < right || left == right;
}

public static bool operator <=(Float left, Float right)
{
return left > right || left == right;
}

public static bool operator >(Float left, object right)
{
return left > new Float(right.ToString());
}

public static bool operator >(object left, Float right)
{
return new Float(left.ToString()) > right;
}

public static bool operator <(Float left, object right)
{
return left < new Float(right.ToString());
}

public static bool operator <(object left, Float right)
{
return new Float(left.ToString()) < right;
}

public static bool operator ==(Float left, object right)
{
return left == new Float(right.ToString());
}

public static bool operator ==(object left, Float right)
{
return new Float(left.ToString()) == right;
}

public static bool operator !=(Float left, object right)
{
return left != new Float(right.ToString());
}

public static bool operator !=(object left, Float right)
{
return new Float(left.ToString()) != right;
}

public static bool operator >=(Float left, object right)
{
return left >= new Float(right.ToString());
}

public static bool operator >=(object left, Float right)
{
return new Float(left.ToString()) >= right;
}

public static bool operator <=(Float left, object right)
{
return left <= new Float(right.ToString());
}

public static bool operator <=(object left, Float right)
{
return new Float(left.ToString()) <= right;
}

public static Float operator +(Float left, Float right)
{
if (left == (Float)0L)
return right;
if (right == (Float)0L)
return left;
string numStr1 = left.Body;
int startIndex1 = numStr1.IndexOf(‘.‘);
if (startIndex1 >= 0)
numStr1 = numStr1.Remove(startIndex1, 1);
string numStr2 = right.Body;
int startIndex2 = numStr2.IndexOf(‘.‘);
if (startIndex2 >= 0)
numStr2 = numStr2.Remove(startIndex2, 1);
int length1 = left.FloatPart.Length;
if (right.FloatPart.Length > length1)
{
length1 = right.FloatPart.Length;
if (left.FloatPart == "0")
{
for (int length2 = left.FloatPart.Length; length2 <= length1; ++length2)
numStr1 += "0";
}
else
{
for (int length2 = left.FloatPart.Length; length2 < length1; ++length2)
numStr1 += "0";
}
}
else if (right.FloatPart == "0")
{
for (int length2 = right.FloatPart.Length; length2 <= length1; ++length2)
numStr2 += "0";
}
else
{
for (int length2 = right.FloatPart.Length; length2 < length1; ++length2)
numStr2 += "0";
}
Int Int = new Int(numStr1) + new Int(numStr2);
if (Int.Length == length1)
return (Float)Int.Body.Insert(Int.Body.Length - length1, "0.");
if (Int.Length >= length1)
return (Float)Int.Body.Insert(Int.Body.Length - length1, ".");
string str = (string)Int.Absolute();
for (int length2 = str.Length; length2 < length1; ++length2)
str = "0" + str;
if (Int.IsPositive)
return (Float)str.Insert(0, "0.");
return (Float)str.Insert(0, "-0.");
}

public static Float operator -(Float left, Float right)
{
if (right == (Float)0L)
return left;
if (right.IsPositive)
return left + (Float)right.Absolute().Body.Insert(0, "-");
return left + right.Absolute();
}

public static Float operator *(Float left, Float right)
{
string numStr1 = left.Body;
int startIndex1 = numStr1.IndexOf(‘.‘);
if (startIndex1 >= 0)
numStr1 = numStr1.Remove(startIndex1, 1);
string numStr2 = right.Body;
int startIndex2 = numStr2.IndexOf(‘.‘);
if (startIndex2 >= 0)
numStr2 = numStr2.Remove(startIndex2, 1);
int floatPartLength1 = left.FloatPartLength;
if (right.FloatPartLength > floatPartLength1)
{
floatPartLength1 = right.FloatPartLength;
for (int floatPartLength2 = left.FloatPartLength; floatPartLength2 < floatPartLength1; ++floatPartLength2)
numStr1 += "0";
}
else if (right.FloatPartLength < floatPartLength1)
{
for (int floatPartLength2 = right.FloatPartLength; floatPartLength2 < floatPartLength1; ++floatPartLength2)
numStr2 += "0";
}
Int Int = new Int(numStr1) * new Int(numStr2);
int num = floatPartLength1 + floatPartLength1;
if (Int.Length == num)
return (Float)Int.Body.Insert(Int.Body.Length - num, "0.");
if (Int.Length >= num)
return (Float)Int.Body.Insert(Int.Body.Length - num, ".");
string str = (string)Int.Absolute();
for (int length = str.Length; length < num; ++length)
str = "0" + str;
if (Int.IsPositive)
return (Float)str.Insert(0, "0.");
return (Float)str.Insert(0, "-0.");
}

public static Float operator /(Float left, Float right)
{
string numStr1 = left.Body;
int startIndex1 = numStr1.IndexOf(‘.‘);
if (startIndex1 >= 0)
numStr1 = numStr1.Remove(startIndex1, 1);
string numStr2 = right.Body;
int startIndex2 = numStr2.IndexOf(‘.‘);
if (startIndex2 >= 0)
numStr2 = numStr2.Remove(startIndex2, 1);
int floatPartLength1 = left.FloatPartLength;
if (right.FloatPartLength > floatPartLength1)
{
int floatPartLength2 = right.FloatPartLength;
for (int floatPartLength3 = left.FloatPartLength; floatPartLength3 < floatPartLength2; ++floatPartLength3)
numStr1 += "0";
}
else if (right.FloatPartLength < floatPartLength1)
{
for (int floatPartLength2 = right.FloatPartLength; floatPartLength2 < floatPartLength1; ++floatPartLength2)
numStr2 += "0";
}
for (int index = 0; index < Float.DividResaultPrecision; ++index)
numStr1 += "0";
Int Int = new Int(numStr1) / new Int(numStr2);
int num = Float.DividResaultPrecision;
if (Int.Length == num)
return (Float)Int.Body.Insert(Int.Body.Length - num, "0.");
if (Int.Length >= num)
return (Float)Int.Body.Insert(Int.Body.Length - num, ".");
string str = (string)Int.Absolute();
for (int length = str.Length; length < num; ++length)
str = "0" + str;
if (Int.IsPositive)
return (Float)str.Insert(0, "0.");
return (Float)str.Insert(0, "-0.");
}

public static Float operator +(Float left, object right)
{
return left + new Float(right.ToString());
}

public static Float operator +(object left, Float right)
{
return new Float(left.ToString()) + right;
}

public static Float operator -(Float left, object right)
{
return left - new Float(right.ToString());
}

public static Float operator -(object left, Float right)
{
return new Float(left.ToString()) - right;
}

public static Float operator *(Float left, object right)
{
return left * new Float(right.ToString());
}

public static Float operator *(object left, Float right)
{
return new Float(left.ToString()) * right;
}

public static Float operator /(Float left, object right)
{
return left / new Float(right.ToString());
}

public static Float operator /(object left, Float right)
{
return new Float(left.ToString()) / right;
}

public Float Absolute()
{
if ((int)this.Body[0] == 43 || (int)this.Body[0] == 45)
return new Float(this.Body.Substring(1));
return this;
}

public new string Trim(string numStr)
{
string str1 = numStr.Trim();
if ((int)str1[0] == 43)
str1 = str1.Substring(1);
int num = str1.IndexOf(‘.‘);
if (num >= 0)
{
for (int length = str1.Length - 1; length >= num; --length)
{
if ((int)str1[length] != 48)
{
str1 = (int)str1[length] != 46 ? str1.Substring(0, length + 1) : str1.Substring(0, length);
break;
}
}
}
bool flag = false;
string str2 = str1;
string str3 = "";
char ch;
for (int index = 0; index < str2.Length; ++index)
{
if (flag)
{
string str4 = str3;
ch = str2[index];
string str5 = ch.ToString();
str3 = str4 + str5;
}
else if ((int)str2[index] == 45)
{
string str4 = str3;
ch = str2[index];
string str5 = ch.ToString();
str3 = str4 + str5;
}
else if ((int)str2[index] == 48)
{
if (index + 1 < str2.Length && (int)str2[index + 1] == 46 || index + 1 >= str2.Length)
{
string str4 = str3;
ch = str2[index];
string str5 = ch.ToString();
str3 = str4 + str5;
flag = true;
}
}
else
{
string str4 = str3;
ch = str2[index];
string str5 = ch.ToString();
str3 = str4 + str5;
flag = true;
}
}
if (str3.Length == 2 && (int)str3[0] == 45 && (int)str3[1] == 48)
str3 = "0";
return str3;
}

public override string ToString()
{
return this.Body;
}

public Int ToInt()
{
string numStr = this.IntPart;
if (!this.IsPositive)
numStr = numStr.Insert(0, "-");
return new Int(numStr);
}

public override bool Equals(object obj)
{
return base.Equals(obj);
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}

关于C#的大数值处理

上一篇:移植stemwin笔记01 emwin文档说明


下一篇:Win10桌面美化