大整數運算C#實現
閒來無事用c#寫了個大整數運算,然後得知c#4.0後就提供了空間呼叫,不過既然寫了就貼出來給大家看看,歡迎指教
class Comput
{
//64位機字長表示的最大十進位制數長度為19
private readonly int MAX_LEN = 19;
/// <summary>
/// 大數(整型)相加
/// </summary>
/// <param name="strNum1">被加數</param>
/// <param name="strNum2">加數</param>
/// <returns>返回相加結果</returns>
public string BigNumAdd(string strNum1, string strNum2)
{
if (null == strNum1 || null == strNum2 )
{
return string.Empty;
}
bool _isNum1 = Regex.IsMatch(strNum1, @"^[0-9]|^-[0-9]");
bool _isNum2 = Regex.IsMatch(strNum2, @"^[0-9]|^-[0-9]");
if (!_isNum1 || !_isNum2)
{
return "輸入的引數不全是數字";
}
bool _is_Negative_Add = false;
if (0 == string.Compare(strNum1.Substring(0, 1), "-") &&
0 == string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1和strnum2都是負數
_is_Negative_Add = true;
strNum1 = strNum1.Substring(1, strNum1.Length - 1);
strNum2 = strNum2.Substring(1, strNum2.Length - 1);
}
else if (0 == string.Compare(strNum1.Substring(0, 1), "-") &&
0 != string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1是負數,strnum2是正數
return BigNumSub(strNum2, strNum1.Substring(1, strNum1.Length - 1));
}
if (0 != string.Compare(strNum1.Substring(0, 1), "-") &&
0 == string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1是正數,strnum2是負數
return BigNumSub(strNum1, strNum2.Substring(1, strNum2.Length - 1));
}
if (1 < strNum1.Length)
{
strNum1 = strNum1.TrimStart('0');
}
if (1 < strNum2.Length)
{
strNum2 = strNum2.TrimStart('0');
}
long _lngNum1 = 0;
long _lngNum2 = 0;
int _intlen1 = strNum1.Length;
int _intlen2 = strNum2.Length;
if (_intlen1 < MAX_LEN && _intlen2 <MAX_LEN)
{
_lngNum1 = Convert.ToInt64(strNum1);
_lngNum2 = Convert.ToInt64(strNum2);
if (_is_Negative_Add)
{
return string.Format("-{0}", (_lngNum1 + _lngNum2).ToString());
}
else
{
return (_lngNum1 + _lngNum2).ToString();
}
}
char[] _chrs1 = strNum1.ToArray();
char[] _chrs2 = strNum2.ToArray();
int k1 = _chrs1.Length, k2 = _chrs2.Length, k = k1 >= k2 ? k1 + 1 : k2 + 1;
int[] num1 = new int[k];
int[] num2 = new int[k];
if (k1 == k2)
{
for (int i = k - 1; i >= 1; i--)
{
num1[i] = Convert.ToInt32(_chrs1[i - 1]) - 48;
num2[i] = Convert.ToInt32(_chrs2[i - 1]) - 48;
}
}
else if (k1 > k2)
{
for (int i = k - 1; i >= 1; i--)
{
num1[i] = Convert.ToInt32(_chrs1[i - 1]) - 48;
if (i - (k1 - k2) - 1 >= 0)
{
num2[i] = Convert.ToInt32(_chrs2[i - (k1 - k2) - 1]) - 48;
}
}
}
else if (k1 < k2)
{
for (int i = k - 1; i >= 1; i--)
{
num1[i] = Convert.ToInt32(_chrs2[i - 1]) - 48;
if (i - (k2 - k1) - 1 >= 0)
{
num2[i] = Convert.ToInt32(_chrs1[i - (k2 - k1) - 1]) - 48;
}
}
}
string t = string.Empty;
int carry = 0;
int value = 0;
for (int i = k - 1; i >= 0; i--)
{
t = (num1[i] + num2[i] + carry).ToString();
if (t.Length > 1)
{
carry = Convert.ToInt32(t[0]) - 48;
value = Convert.ToInt32(t[1]) - 48;
}
else
{
carry = 0;
value = Convert.ToInt32(t[0]) - 48;
}
num1[i] = value;
}
StringBuilder _strR = new StringBuilder();
for (int i = 0; i < k; i++)
{
_strR.Append(num1[i]);
}
if (_is_Negative_Add)
{
return string.Format("-{0}", _strR.ToString().TrimStart('0'));
}
return _strR.ToString().TrimStart('0');
}
/// <summary>
/// 大數(整型)相減
/// </summary>
/// <param name="strNum1">被減數</param>
/// <param name="strNum2">減數</param>
/// <returns>返回相減結果</returns>
public string BigNumSub(string strNum1, string strNum2)
{
if (null == strNum1 || null == strNum2)
{
return string.Empty;
}
bool _isNum1 = Regex.IsMatch(strNum1, @"^[0-9]|^-[0-9]");
bool _isNum2 = Regex.IsMatch(strNum2, @"^[0-9]|^-[0-9]");
if (!_isNum1 || !_isNum2)
{
return "輸入的引數不全是數字";
}
if (0 == string.Compare(strNum1.Substring(0, 1), "-") &&
0 == string.Compare(strNum2.Substring(0, 1), "-"))
{
//strNum1和strNum2都是負數
string _t = strNum1.Substring(1,strNum1.Length-1);
strNum1 = strNum2.Substring(1, strNum2.Length - 1);
strNum2 = _t;
}
else if (0 == string.Compare(strNum1.Substring(0, 1), "-") &&
0 != string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1是負數,strnum2是正數
return string.Format("-{0}",
BigNumAdd(strNum1.Substring(1, strNum1.Length - 1), strNum2));
}
else if (0 != string.Compare(strNum1.Substring(0, 1), "-") &&
0 == string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1是正數,strnum2是負數
return BigNumAdd(strNum1, strNum2.Substring(1, strNum2.Length - 1));
}
long _lngNum1 = 0;
long _lngNum2 = 0;
int _intlen1 = strNum1.Length;
int _intlen2 = strNum2.Length;
if (_intlen1 < MAX_LEN && _intlen2 < MAX_LEN)
{
_lngNum1 = Convert.ToInt64(strNum1);
_lngNum2 = Convert.ToInt64(strNum2);
return (_lngNum1 - _lngNum2).ToString();
}
if (1 < strNum1.Length)
{
strNum1 = strNum1.TrimStart('0');
}
if (1 < strNum2.Length)
{
strNum2 = strNum2.TrimStart('0');
}
if (0 == string.Compare(strNum1, strNum2))
{
return "0";
}
char[] _chrs1 = strNum1.ToArray();
char[] _chrs2 = strNum2.ToArray();
int k1 = _chrs1.Length, k2 = _chrs2.Length, k = k1 > k2 ? k1 : k2;
int[] num1 = new int[k];
int[] num2 = new int[k];
bool _isSwap = false;
if (k1 == k2)
{
for (int i = 0; i < k; i++)
{
if (Convert.ToInt32(_chrs1[i]) - 48 < Convert.ToInt32(_chrs2[i]) - 48)
{
_isSwap = true;
break;
}
}
if (!_isSwap)
{
for (int i = k - 1; i >= 0; i--)
{
num1[i] = Convert.ToInt32(_chrs1[i]) - 48;
num2[i] = Convert.ToInt32(_chrs2[i]) - 48;
}
}
else
{
for (int i = k - 1; i >= 0; i--)
{
num1[i] = Convert.ToInt32(_chrs2[i]) - 48;
num2[i] = Convert.ToInt32(_chrs1[i]) - 48;
}
}
}
else if (k1 > k2)
{
for (int i = k - 1; i >= 0; i--)
{
num1[i] = Convert.ToInt32(_chrs1[i]) - 48;
if (i - (k1 - k2) >= 0)
{
num2[i] = Convert.ToInt32(_chrs2[i - (k1 - k2)]) - 48;
}
}
}
else if (k1 < k2)
{
_isSwap = true;
for (int i = k - 1; i >= 0; i--)
{
if (i - (k2 - k1) >= 0)
{
num2[i] = Convert.ToInt32(_chrs1[i - (k2 - k1)]) - 48;
}
num1[i] = Convert.ToInt32(_chrs2[i]) - 48;
}
}
int d = 0;
int borrow = 0;
int value = 0;
int index = 0;
for (int i = k - 1; i >= 0; i--)
{
d = num1[i] - num2[i];
if (d < 0)
{
borrow = 1;
index = i - 1;
//處理借位
while (index >= 0)
{
if (num1[index] != 0)
{
num1[index] -= borrow;
break;
}
else
{
num1[index] = 9;
}
index--;
}
value = 10 + num1[i] - num2[i];
}
else
{
value = d;
}
num1[i] = value;
}
StringBuilder _strR = new StringBuilder();
for (int i = 0; i < k; i++)
{
_strR.Append(num1[i]);
}
return _isSwap
? string.Format("-{0}", _strR.ToString().TrimStart('0'))
: _strR.ToString().TrimStart('0');
}
/// <summary>
/// 大數(整數)相乘
/// </summary>
/// <param name="strNum1">被乘數</param>
/// <param name="strNum2">乘數</param>
/// <returns>返回相乘結果</returns>
public string BigNumMul(string strNum1, string strNum2)
{
if (null == strNum1 || null == strNum2)
{
return string.Empty;
}
bool _isNum1 = Regex.IsMatch(strNum1, @"^[0-9]|^-[0-9]");
bool _isNum2 = Regex.IsMatch(strNum2, @"^[0-9]|^-[0-9]");
if (!_isNum1 || !_isNum2)
{
return "輸入的引數不全是數字";
}
strNum1 = strNum1.TrimStart('0');
if (string.IsNullOrEmpty(strNum1.Trim()))
{
return "0";
}
bool _hasFlag = false;
if (0 == string.Compare(strNum1.Substring(0, 1), "-") &&
0 == string.Compare(strNum2.Substring(0, 1), "-"))
{
//strNum1和strNum2都是負數
strNum1 = strNum1.Substring(1, strNum1.Length - 1);
strNum2 = strNum2.Substring(1, strNum2.Length - 1);
}
else if (0 == string.Compare(strNum1.Substring(0, 1), "-") &&
0 != string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1是負數,strnum2是正數
_hasFlag = true;
strNum1 = strNum1.Substring(1, strNum1.Length - 1);
}
else if (0 != string.Compare(strNum1.Substring(0, 1), "-") &&
0 == string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1是正數,strnum2是負數
_hasFlag = true;
strNum2 = strNum2.Substring(1, strNum2.Length - 1);
}
long _lngNum1 = 0;
long _lngNum2 = 0;
int _intlen1 = strNum1.Length;
int _intlen2 = strNum2.Length;
if (_intlen1 + _intlen2 <= MAX_LEN )
{
_lngNum1 = Convert.ToInt64(strNum1);
_lngNum2 = Convert.ToInt64(strNum2);
if (_hasFlag)
{
return string.Format("-{0}", (_lngNum1 * _lngNum2).ToString());
}
else
{
return (_lngNum1 * _lngNum2).ToString();
}
}
if (1 < strNum1.Length)
{
strNum1 = strNum1.TrimStart('0');
}
if (1 < strNum2.Length)
{
strNum2 = strNum2.TrimStart('0');
}
//轉換引數型別
char[] chrs1 = strNum1.ToArray();
int[] nums1 = new int[chrs1.Length];
for (int i = 0; i < chrs1.Length; i++)
{
nums1[i] = Convert.ToInt32(chrs1[i]) - 48;
}
char[] chrs2 = strNum2.ToArray();
int[] nums2 = new int[chrs2.Length];
for (int i = 0; i < chrs2.Length; i++)
{
nums2[i] = Convert.ToInt32(chrs2[i]) - 48;
}
int k1 = 0, k2 = 0;
if (nums1.Length >= nums2.Length)
{
k1 = nums1.Length;
k2 = nums2.Length;
}
else
{
k1 = nums2.Length;
k2 = nums1.Length;
}
//初始化相乘項積的長度陣列
IList<int[]> _result = new List<int[]>();
for (int i = 1; i <= k2; i++)
{
_result.Add(new int[k1 + 1 + k2]);
}
//計算相乘結果
string t = string.Empty;
int carry = 0;
int value = 0;
int k = 0;
for (int i = k2 - 1; i >= 0; i--)
{
for (int j = k1 - 1; j >= 0; j--)
{
t = (nums2[i] * nums1[j] + carry).ToString();
if (t.Length > 1)
{
carry = Convert.ToInt32(t[0]) - 48;
value = Convert.ToInt32(t[1]) - 48;
}
else
{
carry = 0;
value = Convert.ToInt32(t[0]) - 48;
}
_result[k2 - 1 - i][j + 1 + k2 - k] = value;
if (j == 0)
{
//最後一個進位須保留
_result[k2 - 1 - i][j + 1 + k2 - k - 1] = carry;
}
}
carry = 0;
k++;
}
//for (int i = 0; i <= _result.Count - 1; i++)
//{
// for (int j = 0; j < _result[i].Length; j++)
// {
// Console.Write(_result[i][j]);
// }
// Console.WriteLine();
//}
//將相乘結果進行求和
carry = 0;
k = k1 + k2 + 1;
int[] _tmp = _result[0];
for (int i = 1; i <= _result.Count - 1; i++)
{
for (int j = k - 1; j >= 0; j--)
{
t = (_tmp[j] + _result[i][j] + carry).ToString();
if (t.Length > 1)
{
carry = Convert.ToInt32(t[0]) - 48;
value = Convert.ToInt32(t[1]) - 48;
}
else
{
carry = 0;
value = Convert.ToInt32(t[0]) - 48;
}
_tmp[j] = value;
}
}
StringBuilder _str = new StringBuilder();
for (int i = 0; i < k; i++)
{
_str.Append(_tmp[i]);
// Console.Write(_tmp[i]);
}
//Console.WriteLine();
if (_hasFlag)
{
return string.Format("-{0}", _str.ToString().TrimStart('0'));
}
return _str.ToString().TrimStart('0');
}
/// <summary>
/// 大數(整數)相除
/// </summary>
/// <param name="strNum1">被除數</param>
/// <param name="strNum2">除數</param>
/// <returns>返回相除結果(商和餘)</returns>
public string[] BigNumDiv(string strNum1, string strNum2)
{
string[] _r = new string[2];
if (null == strNum1 || null == strNum2)
{
_r[0] = string.Empty;
_r[1] = string.Empty;
return _r;
}
bool _isNum1 = Regex.IsMatch(strNum1, @"^[0-9]|^-[0-9]");
bool _isNum2 = Regex.IsMatch(strNum2, @"^[0-9]|^-[0-9]");
if (!_isNum1 || !_isNum2)
{
_r[0] = "輸入的引數不全是數字";
_r[1] = "";
return _r;
}
bool _hasFlag = false;
if (0 == string.Compare(strNum1.Substring(0, 1), "-") &&
0 == string.Compare(strNum2.Substring(0, 1), "-"))
{
//strNum1和strNum2都是負數
strNum1 = strNum1.Substring(1, strNum1.Length - 1);
strNum2 = strNum2.Substring(1, strNum2.Length - 1);
}
else if (0 == string.Compare(strNum1.Substring(0, 1), "-") &&
0 != string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1是負數,strnum2是正數
_hasFlag = true;
strNum1 = strNum1.Substring(1, strNum1.Length - 1);
}
else if (0 != string.Compare(strNum1.Substring(0, 1), "-") &&
0 == string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1是正數,strnum2是負數
_hasFlag = true;
strNum2 = strNum2.Substring(1, strNum2.Length - 1);
}
_r = BigNumDiv_(strNum1, strNum2);
if (_hasFlag)
{
_r[0] = string.Format("-{0}", _r[0]);
}
return _r;
}
/// <summary>
/// 大數(整數)相除
/// </summary>
/// <param name="strNum1">被除數</param>
/// <param name="strNum2">除數</param>
/// <returns>返回相除結果(商和餘)</returns>
private string[] BigNumDiv_(string strNum1, string strNum2)
{
string[] _r = new string[2];
if (1 < strNum1.Length)
{
strNum1 = strNum1.TrimStart('0');
}
if (1 < strNum2.Length)
{
strNum2 = strNum2.TrimStart('0');
}
strNum1 = strNum1.TrimStart('0');
if (string.IsNullOrEmpty(strNum1.Trim()))
{
_r[0] = "0";
_r[1] = "0";
return _r;
}
int _intLen1 = strNum1.Length;
int _intLen2 = strNum2.Length;
string _strTemp = string.Empty;
if (_intLen1 < _intLen2)
{
_strTemp = strNum1;
strNum1 = strNum2;
strNum2 = _strTemp;
_intLen1 = strNum1.Length;
_intLen2 = strNum2.Length;
}
else if (_intLen1 == _intLen2)
{
//如果兩數完全相同
if (0 == string.Compare(strNum1, strNum2))
{
_r[0] = "1";
_r[1] = "0";
return _r;
}
Int16 _firstNum1 = 0;
Int16 _firstNum2 = 0;
int i = 0;
bool _isSwap = true;
while (i + 1 <= _intLen1)
{
_firstNum1 = Convert.ToInt16(strNum1.Substring(i, 1));
_firstNum2 = Convert.ToInt16(strNum2.Substring(i, 1));
if (_firstNum1 > _firstNum2)
{
_isSwap = false;
break;
}
i++;
}
if (_isSwap)
{
_strTemp = strNum1;
strNum1 = strNum2;
strNum2 = _strTemp;
_intLen1 = strNum2.Length;
_intLen2 = strNum1.Length;
}
}
strNum2 = strNum2.TrimStart('0');
if (string.IsNullOrEmpty(strNum2.Trim()))
{
_r[0] = "除數不能為0";
_r[1] = "無法計算出餘數";
return _r;
}
long num1 = 0;
long num2 = 0;
if (_intLen1 <= MAX_LEN - 2 && _intLen2 <= MAX_LEN - 2)
{
num1 = Convert.ToInt64(strNum1);
num2 = Convert.ToInt64(strNum2);
_r[0] = (num1 / num2).ToString();
_r[1] = (num1 % num2).ToString();
return _r;
}
//設定試算的範圍
if (_intLen1 >= MAX_LEN)
{
_intLen1 = MAX_LEN - 2;
}
else if(_intLen1==1)
{
_intLen1 = 1;
}
if (_intLen2 > MAX_LEN)
{
_intLen2 = MAX_LEN - 3;
}
else if(_intLen2==MAX_LEN)
{
_intLen2=MAX_LEN-2;
}
else if (_intLen2 == 1)
{
_intLen2 = 1;
}
//定義ax+b=c中的四個變數
string a = string.Empty;
string x = string.Empty;
string b = string.Empty;
string c = string.Empty;
//試算過程
num1 = Convert.ToInt64(strNum1.Substring(0, _intLen1));
num2 = Convert.ToInt64(strNum2.Substring(0, _intLen2));
x = (num1 / num2).ToString();
a = num2.ToString();
b = (num1 % num2).ToString();
c = num1.ToString();
int k = strNum2.Length - _intLen2;
StringBuilder strnum2_ = new StringBuilder("1");
for (int i = 1; i <= k; i++)
{
strnum2_.Append("0");
}
if (1 < strnum2_.Length)
{
a = BigNumMul(a, strnum2_.ToString());
b = BigNumMul(b, strnum2_.ToString());
c = BigNumMul(c, strnum2_.ToString());
//計算“試算的除數”與“實際除數”的差值
string num2__ = BigNumSub(strNum2, a);
//賦於a變數為“真實除數”
a = strNum2;
//賦於b為“試算的餘數”與“試算倍數*差值”的差值
string b_ = BigNumMul(x, num2__);
b = BigNumSub(b, b_);
}
k = strNum1.Length - c.Length;
StringBuilder strnum1_ = new StringBuilder("1");
for (int i = 1; i <= k; i++)
{
strnum1_.Append("0");
}
if (1 < strnum1_.Length)
{
x = BigNumMul(x, strnum1_.ToString());
b = BigNumMul(b, strnum1_.ToString());
c = BigNumMul(c, strnum1_.ToString());
}
//計算“試算的被除數”與“實際除數”的差值
string num1__ = BigNumSub(strNum1, c);
//賦於c變數為“真實被除數”
c = strNum1;
//賦於b值為在b值基出上累加的變數num1__的值
b = BigNumAdd(b, num1__);
//判斷b值(試算餘數)是否小於等於a(實際除數)
if (2 == BigNumCompare(a, b))
{
//如果餘數大於商,則進行遞迴計算
_r = BigNumDiv_(b, a);
//累加商值
_r[0] = BigNumAdd(x, _r[0]);
}
else
{
_r[0] = x;
_r[1] = b;
}
return _r;
}
/// <summary>
/// 大數(整數)比較
/// </summary>
/// <param name="strNum1">大數1</param>
/// <param name="strNum2">大數2</param>
/// <returns>返回比較狀態 0 兩數相等 1=>大數1大於大數2 2=>大數1小於大數2 -1=>出錯</returns>
public int BigNumCompare(string strNum1, string strNum2)
{
if (null == strNum1 || null == strNum2)
{
return -1;
}
int flag = 0;
bool _is_Negative_Compare = false;
if (0 == string.Compare(strNum1.Substring(0, 1), "-") &&
0 == string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1和strnum2都是負數
_is_Negative_Compare = true;
strNum1 = strNum1.Substring(1, strNum1.Length - 1);
strNum2 = strNum2.Substring(1, strNum2.Length - 1);
}
else if (0 == string.Compare(strNum1.Substring(0, 1), "-") &&
0 != string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1是負數,strnum2是正數
flag = 1;
}
if (0 != string.Compare(strNum1.Substring(0, 1), "-") &&
0 == string.Compare(strNum2.Substring(0, 1), "-"))
{
//strnum1是正數,strnum2是負數
flag = 2;
}
if (1 < strNum1.Length)
{
strNum1 = strNum1.TrimStart('0');
}
if (1 < strNum2.Length)
{
strNum2 = strNum2.TrimStart('0');
}
long _lngNum1 = 0;
long _lngNum2 = 0;
int _intlen1 = strNum1.Length;
int _intlen2 = strNum2.Length;
if (_intlen1 <= MAX_LEN - 2 && _intlen2 <= MAX_LEN - 2)
{
_lngNum1 = Convert.ToInt64(strNum1);
_lngNum2 = Convert.ToInt64(strNum2);
if (_lngNum1 > _lngNum2)
{
flag = 1;
}
else if (_lngNum1 < _lngNum2)
{
flag = 2;
}
if (_lngNum1 == _lngNum2)
{
flag = 0;
}
return flag;
}
if (_intlen1 > _intlen2)
{
if (_is_Negative_Compare)
{
flag = 2;
}
else
{
flag = 1;
}
}
else if (_intlen1 < _intlen2)
{
if (_is_Negative_Compare)
{
flag = 1;
}
else
{
flag = 2;
}
}
else if (_intlen2 == _intlen1)
{
flag = 0;
}
return flag;
}
/// <summary>
/// 判斷兩數是否互質
/// </summary>
/// <param name="strNum1">輸入數1</param>
/// <param name="strNum2">輸入數2</param>
/// <returns>true 互質 false 非互質</returns>
public bool IsEachPrime(string strNum1, string strNum2)
{
bool flag = false;
string divisor = string.Empty;
string dividend = string.Empty;
string str1 = "0";
string str2 = "1";
string[] _r = new string[2];
while (true)
{
_r = BigNumDiv(dividend,divisor);
if (0 == string.Compare(str1, _r[1]))
{
flag = false;
break;
}
else if (0 == string.Compare(str2, _r[1]))
{
flag = true;
break;
}
dividend = divisor;
divisor = _r[1];
}
return flag;
}
/// <summary>
/// 計算出兩個數的最大公因子
/// </summary>
/// <param name="strNum1">整數1</param>
/// <param name="strNum2">整數2</param>
/// <returns>返回公因子</returns>
public string gcd(string strNum1, string strNum2)
{
if (null == strNum1 || null == strNum2)
{
return string.Empty;
}
if (1 < strNum1.Length)
{
strNum1 = strNum1.TrimStart('0');
}
if (1 < strNum2.Length)
{
strNum2 = strNum2.TrimStart('0');
}
bool _isNum1 = Regex.IsMatch(strNum1, @"^[0-9]|^-[0-9]");
bool _isNum2 = Regex.IsMatch(strNum2, @"^[0-9]|^-[0-9]");
if (!_isNum1 || !_isNum2)
{
return "輸入的引數不全是數字";
}
string _result = string.Empty;
string divisor = string.Empty;
string dividend = string.Empty;
int _flag = BigNumCompare(strNum1, strNum2);
if (1 == _flag)
{
dividend = strNum1;
divisor = strNum2;
}
else if (2 == _flag)
{
dividend = strNum2;
divisor = strNum1;
}
else if (-1 == _flag)
{
return "比較出錯";
}
else if (0 == _flag)
{
return strNum1;
}
string str1 = "0";
string str2 = "1";
string[] _r = new string[2];
while (true)
{
_r = BigNumDiv(dividend, divisor);
if (0 == string.Compare(str1, _r[1]))
{
_result = divisor;
break;
}
else if (0 == string.Compare(str2, _r[1]))
{
_result = str2;
break;
}
dividend = divisor;
divisor = _r[1];
}
return _result;
}
}
}
相關文章
- java大整數四則運算Java
- 位運算實現整數與位元組陣列轉換陣列
- Shell階段02 shell變數運算(整數運算/小數運算), shell變數案例變數
- 正則實現數學運算
- 漫畫:如何實現大整數相加?
- 或與運算和安全整數(數的影子)
- C++ 大整數類(BigInteger類)實現C++
- C/C++模運算(正負整數)C++
- 集合-運算實現
- C# Math 中的常用的數學運算C#
- 求兩個正整數的最大公約數與最小公倍數--C#實現C#
- C語言程式設計-長整數加法運算C語言程式設計
- 複數的四則運算(C語言實現)C語言
- 位運算-判斷一個數是否為2的整數次方
- 大數運算—大數加法、減法、乘法、除法詳解
- 位運算與SQL實現SQL
- sobel運算元,matlab實現Matlab
- javascript怎麼實現算術加法運算JavaScript
- python實現字串轉換整數Python字串
- 刷題系列 - 實現一個指數運算方法,對遞迴次數和運算時間有約束。遞迴
- C語言用三目運算實現判斷大寫C語言
- C# 位運算及例項計算C#
- Verilog實現加減乘除運算
- 位運算實現加減乘除
- 大整數乘法
- 大整數加法
- 實現將10個整數升序排列
- 二進位制數的運算原理與閘電路實現
- 算數運算子
- C#快速入門教程(8)——整數C#
- VIVADO vhdl verilog 實現矩陣運算矩陣
- verilog實現矩陣卷積運算矩陣卷積
- C# 輸入一個整數,求質因數C#
- Python數學運算Python
- XJSON 是如何實現四則運算的?JSON
- 深入V8引擎-列舉+位運算實現引數配置
- 大文字平行計算實現方式
- 大數相乘(浮點數)實現
- 實現一個原子的正整數類:AtomicPositiveInteger