【求简洁代码】:定义一个大整数类,进行 100 位加减运算。
一、题目:定义一个大整数类,进行100位加减运算。二、我的努力
经过2个星期的努力,终于朴素地完成了。代码很长,不简洁。下面代码只有加法运算(减法运算添上,超出论坛要求的字数)
namespace 书本练习题
{
public class BigInteger
{
public char[] chars;
public BigInteger() //无参构造函数
{
}
public BigInteger(int i) //有参构造函数
{
this.chars = new char[i];
}
/// <summary>
///大整数相加 运算符+重载
/// </summary>
public static BigInteger operator +(BigInteger one, BigInteger two)
{
BigInteger bigIntThree = new BigInteger();
#region //两个数都是正数
if (one.chars[0] == '+' && two.chars[0] == '+') //两个大整数都是正数
{
int temp1 = one.chars.Length - 1;
int temp2 = two.chars.Length - 1;
int temp = (Math.Max(temp1, temp2)) + 1 + 1; //1位用于保存+/-,1位用于保存最高位相加向前进1;
bigIntThree.chars = new char[temp];
bigIntThree.chars[0] = '+';
int x, y, z, n = 0;
for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
{
if (i - ((temp - 1) - temp1) >= 1)
{
x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
}
else
{
x = 0;
}
if (i - ((temp - 1) - temp2) >= 1)
{
y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
}
else
{
y = 0;
}
z = x + y + n;
if (z < 10)
{
bigIntThree.chars[i] = Convert.ToChar(z + 48); ;
n = 0;
}
else
{
bigIntThree.chars[i] = Convert.ToChar(z - 10 + 48); ;
n = z / 10;
}
}
}
#endregion
#region //两个数都是负数
if (one.chars[0] == '-' && two.chars[0] == '-') //两个大整数都是负数
{
int temp1 = one.chars.Length - 1;
int temp2 = two.chars.Length - 1;
int temp = (Math.Max(temp1, temp2)) + 1 + 1;//1位用于保存+/-,1位用于保存最高位相加向前进1;
bigIntThree.chars = new char[temp];
bigIntThree.chars[0] = '-';
int x, y, z, n = 0;
for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
{
if (i - ((temp - 1) - temp1) >= 1)
{
x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
}
else
{
x = 0;
}
if (i - ((temp - 1) - temp2) >= 1)
{
y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
}
else
{
y = 0;
}
z = x + y + n;
if (z < 10)
{
bigIntThree.chars[i] = Convert.ToChar(z + 48); ;
n = 0;
}
else
{
bigIntThree.chars[i] = Convert.ToChar(z - 10 + 48); ;
n = z / 10;
}
}
}
#endregion
#region //第一个是负数,第二个是正数
if (one.chars[0] == '-' && two.chars[0] == '+') //第一个是负数,第二个是正数
{
int temp1 = one.chars.Length - 1;
int temp2 = two.chars.Length - 1;
int temp = (Math.Max(temp1, temp2)) + 1 + 1;//1位用于保存+/-;
bigIntThree.chars = new char[temp];
if (temp1 > temp2)
{
bigIntThree.chars[0] = '-'; //负数大,结果为负数
int x, y, z, n = 0;
for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
{
if (i - ((temp - 1) - temp1) >= 1)
{
x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
}
else
{
x = 0;
}
if (i - ((temp - 1) - temp2) >= 1)
{
y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
}
else
{
y = 0;
}
if ((x - n) >= y)
{
z = x - y - n;
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 0;
}
else
{
z = x + 10 - y - n;
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 1;
}
}
}
else if (temp1 < temp2)
{
bigIntThree.chars[0] = '+'; //正数大,结果为正数
int x, y, z, n = 0;
for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
{
if (i - ((temp - 1) - temp2) >= 1)
{
x = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
}
else
{
x = 0;
}
if (i - ((temp - 1) - temp1) >= 1)
{
y = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
}
else
{
y = 0;
}
if ((x - n) >= y)
{
z = x - y - n;
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 0;
}
else
{
z = x + 10 - y - n;
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 1;
}
}
}
else //数位相等
{
for (int m = 1; m < temp1; m++)
{
if (one.chars[m] > two.chars[m])
{
bigIntThree.chars[0] = '-';
int x, y, z, n = 0;
for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
{
if (i - ((temp - 1) - temp1) >= 1)
{
x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
}
else
{
x = 0;
}
if (i - ((temp - 1) - temp2) >= 1)
{
y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
}
else
{
y = 0;
}
if ((x - n) >= y)
{
z = x - y - n;
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 0;
}
else
{
z = x + 10 - y - n;
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 1;
}
}
break;
}
else if (one.chars[m] < two.chars[m])
{
bigIntThree.chars[0] = '+';
int x, y, z, n = 0;
for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
{
if (i - ((temp - 1) - temp2) >= 1)
{
x = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
}
else
{
x = 0;
}
if (i - ((temp - 1) - temp1) >= 1)
{
y = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
}
else
{
y = 0;
}
if ((x - n) >= y)
{
z = x - y - n;
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 0;
}
else
{
z = x + 10 - y;
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 1;
}
}
break;
}
else
{
;
}
}
}
}
#endregion
#region //第一个是正数,第二个是负数
if (one.chars[0] == '+' && two.chars[0] == '-') //第一个是正数,第二个是负数
{
int temp1 = one.chars.Length - 1;
int temp2 = two.chars.Length - 1;
int temp = (Math.Max(temp1, temp2)) + 1 + 1;//1位用于保存+/-;
bigIntThree.chars = new char[temp];
if (temp1 > temp2)
{
bigIntThree.chars[0] = '+'; //正数大,结果为正数
int x, y, z, n = 0;
for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
{
if (i - ((temp - 1) - temp1) >= 1)
{
x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
}
else
{
x = 0;
}
if (i - ((temp - 1) - temp2) >= 1)
{
y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
}
else
{
y = 0;
}
if ((x - n) >= y)
{
z = x - y - n;
Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 0;
}
else
{
z = x + 10 - y - n;
Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 1;
}
}
}
else if (temp1 < temp2)
{
bigIntThree.chars[0] = '-'; //正数小,结果为负数
int x, y, z, n = 0;
for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
{
if (i - ((temp - 1) - temp2) >= 1)
{
x = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
}
else
{
x = 0;
}
if (i - ((temp - 1) - temp1) >= 1)
{
y = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
}
else
{
y = 0;
}
if ((x - n) >= y)
{
z = x - y - n;
Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 0;
}
else
{
z = x + 10 - y - n;
Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 1;
}
}
}
else //数位相等
{
for (int m = 1; m < temp1; m++)
{
if (one.chars[m] > two.chars[m])
{
bigIntThree.chars[0] = '+';
int x, y, z, n = 0;
for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
{
if (i - ((temp - 1) - temp1) >= 1)
{
x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
}
else
{
x = 0;
}
if (i - ((temp - 1) - temp2) >= 1)
{
y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
}
else
{
y = 0;
}
if ((x - n) >= y)
{
z = x - y - n;
Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 0;
}
else
{
z = x + 10 - y - n;
Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 1;
}
}
break;
}
else if (one.chars[m] < two.chars[m])
{
bigIntThree.chars[0] = '-';
int x, y, z, n = 0;
for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
{
if (i - ((temp - 1) - temp2) >= 1)
{
x = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
}
else
{
x = 0;
}
if (i - ((temp - 1) - temp1) >= 1)
{
y = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
}
else
{
y = 0;
}
if ((x - n) >= y)
{
z = x - y - n;
Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 0;
}
else
{
z = x + 10 - y - n;
Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
bigIntThree.chars[i] = Convert.ToChar(z + 48);
n = 1;
}
}
break;
}
else
{
;
}
}
}
}
#endregion
return bigIntThree;
}
public void show()
{
int x=0;
for (int i = 0; i < chars.Length; i++)
{
Console.Write("{0}", chars[i]);
x++;
}
Console.WriteLine("这个数是{0}位数。", x-1);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入第一个大整数,正数以+开头,负数以-开头:");
string strOneNumber = Console.ReadLine();
BigInteger bigIntOne = new BigInteger();
bigIntOne.chars = strOneNumber.ToCharArray();
bigIntOne.show();
Console.WriteLine("请输入第二个大整数,正数以+开头,负数以-开头:");
string strTwoNumber = Console.ReadLine();
BigInteger bigIntTwo = new BigInteger();
bigIntTwo.chars = strTwoNumber.ToCharArray();
bigIntTwo.show();
//100位大整数+运算
BigInteger bigIntThree;
bigIntThree = bigIntOne + bigIntTwo;
bigIntThree.show();
//100位大整数-运算
BigInteger bigIntFour;
bigIntFour = bigIntOne - bigIntTwo;
bigIntFour.show();
Console.ReadKey();
}
}
}
三、美中不足的地方是:
1)输入大整数,正数要以+开头输入,负数要以-开头输入;
2)最后的计算结果是,相加最高位没有进位也有一个空位,相减高位等于0时也显示。
[ 本帖最后由 lxsxd 于 2014-9-7 22:38 编辑 ]