C#版 大数计算器--减法
class BigReduce : BigCalculate
{
public override string Oper(string num1, string num2)
{
bool isMinus = false;
//先判断计算结果是否为正数(即num1是否大于num2),如果相等,直接返回0
//若结果为负数,则互换num1,num2,并在返回值前加'-'
if (num1.Equals(num2))
{
return "0";
}
else if (Max(num1, num2).Equals(num2))//如果预计计算结果为负数,则将num1 num2调换
{
ChangeNum(ref num1, ref num2);
isMinus = true;
}
#region MyRegion
//获得较大数的位数
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--)
{
if (tempNum1[i] >= tempNum2[i] + flag)
{
liCh.Add((char)(tempNum1[i] - tempNum2[i] - flag + '0'));
flag = 0;
}
else
{
liCh.Add((char)(tempNum1[i] - tempNum2[i] - flag + 10 + '0'));
flag = 1;
}
}
//若结果高位有0,则去掉
//若结果为负数,则返回结果加"-"
if (isMinus == true)
{
return "-" + InvertOrder(new string(liCh.ToArray())).TrimStart('0');
}
return InvertOrder(new string(liCh.ToArray())).TrimStart('0');
#endregion
}
}
public override string Oper(string num1, string num2)
{
bool isMinus = false;
//先判断计算结果是否为正数(即num1是否大于num2),如果相等,直接返回0
//若结果为负数,则互换num1,num2,并在返回值前加'-'
if (num1.Equals(num2))
{
return "0";
}
else if (Max(num1, num2).Equals(num2))//如果预计计算结果为负数,则将num1 num2调换
{
ChangeNum(ref num1, ref num2);
isMinus = true;
}
#region MyRegion
//获得较大数的位数
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--)
{
if (tempNum1[i] >= tempNum2[i] + flag)
{
liCh.Add((char)(tempNum1[i] - tempNum2[i] - flag + '0'));
flag = 0;
}
else
{
liCh.Add((char)(tempNum1[i] - tempNum2[i] - flag + 10 + '0'));
flag = 1;
}
}
//若结果高位有0,则去掉
//若结果为负数,则返回结果加"-"
if (isMinus == true)
{
return "-" + InvertOrder(new string(liCh.ToArray())).TrimStart('0');
}
return InvertOrder(new string(liCh.ToArray())).TrimStart('0');
#endregion
}
}