///
///
基類,抽象類別,定義常用方法
///
abstract
class BigCalculate
{
public abstract string Oper(string num1, string num2);
//反轉數字順序
protected string InvertOrder(string str)
{
char[] chTemp = new char[str.Length];
for (int i = 0; i < str.Length; i++)
{
chTemp[i] = str[str.Length - i-1];
}
return new string(chTemp.ToArray());
}
//交換數字
protected void ChangeNum(ref string num1,ref string num2)
{
string strTemp = num1;
num1 = num2;
num2 = strTemp;
}
//獲得較大數
protected string Max(string num1, string num2)
{
if (num1.Length > num2.Length)
{
return num1;
}
else if (num1.Length==num2.Length)
{
for (int i = num1.Length-1; i >=0 ; i--)
{
if (num1[i].CompareTo(num2[i])<0)
{
return num2;
}
}
return num1;
}
return num2;
}
}
--------------------------------------------------------------------------
class
BigAdd:BigCalculate
{
public override string Oper(string num1, string num2)
{
//獲得較大數的位元
int len = num1.Length > num2.Length ? num1.Length :
num2.Length;
//將較小數差位補0
string tempNum1 = new string('0', len - num1.Length) + num1;
string tempNum2 = new string('0', len - num2.Length) + num2;
int flag = 0;//進位符
List liCh = new List();//存放運算後數字
for (int i = len - 1; i >= 0; i--)
{
liCh.Add((char)(tempNum1[i] + tempNum2[i] + flag - '0'));
flag = (liCh[liCh.Count - 1] - '0') / 10;
liCh[liCh.Count - 1] = (char)(((liCh[liCh.Count - 1] - '0') % 10) +
'0');
}
//若運算完成後進位符不為0,及最高位進位,則再加一位
if (flag > 0)
{
liCh.Add((char)(flag + '0'));
}
return InvertOrder(new string(liCh.ToArray()));
}
}