乘法
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Multiply
{
private int len1, len2;
private int[] result;
public string Result
{
get
{
StringBuilder sbResult = new StringBuilder();
int lable = 0;
if (result[0] == 0)
{
lable = 1;
}
for (; lable < result.Length; lable++)
{
sbResult.Append(result[lable]);
}
return sbResult.ToString();
}
}
public Multiply(string str1, string str2)
{
len1 = str1.Trim().Length;
len2 = str2.Trim().Length;
int num1, num2, num3;
int len = len1 + len2;
result = new int[len];
for (int i = len1; i > 0; i--)
{
for (int j = len2; j > 0; j--)
{
num1 = Convert.ToInt32(str1[i - 1].ToString());
num2 = Convert.ToInt32(str2[j - 1].ToString());
num3 = num1 * num2;
SumUp(i, j, num3.ToString(), num3.ToString().Length);
}
}
}
private void SumUp(int i, int j, string num, int len)
{
for (int k = 0; k < len; k++)
{
Carry((i + j - k), Convert.ToInt32(num[len - k - 1].ToString()));
}
}
private void Carry(int s, int v)
{
int vv;
vv = result[s - 1] + v;
if (vv > 10)
{
result[s - 1] = vv % 10;
Carry(s - 1, vv / 10);
}
else
{
result[s - 1] = vv;
}
}
}
}