问题描述
- C#大数相乘问题,用三个数组来存储
-
最好用分治法解决问题,然后能够运算20位左右的大数相乘,控制台和窗口都行
解决方案
http://blog.csdn.net/xianfajushi/article/details/49555767
解决方案二:
比较难写你再给你个思路吧,你完善下。
``` public class dashucheng
{
public string cs;//乘数
public string bcs;//被乘数
public string cheng()//乘操作返回字符串
{
string patten = "^[0-9]{1,}$";
string[] arr = null;
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(patten);//数字匹配
if (reg.IsMatch(cs) && reg.IsMatch(bcs))
{
string a = cs;
string b = bcs;
string result;
int[] c = new int[a.Length];
int[] d = new int[b.Length];
int[] f = new int[100];
arr = new string[a.Length];
int index = 0;
for (int i = 0; i < arr.Length; i++)//将字符串格式乘数存放到数组1
{
arr[i] = a.Substring(index + i, 1);
c[a.Length - i - 1] = int.Parse(arr[i]);
}
arr = new string[b.Length];
for (int i = 0; i < arr.Length; i++)//将字符串格式被乘数存放到数组2
{
arr[i] = a.Substring(index + i, 1);
d[b.Length - i - 1] = int.Parse(arr[i]);
}
result = a;//进行乘操作赋值给result比较难写没写出来,需要自己写
return result;
}
else if (cs == "" || bcs == "")
return "empty";
else
return "error";
}
}
//click事件内添加下列代码
dashucheng xc = new dashucheng();
xc.cs = textBox1.Text;
xc.bcs = textBox2.Text;
try
{
string result;
result =xc.cheng();
if (result != "error" && result != "empty")
textBox3.Text = result;
else
MessageBox.Show(result);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
解决方案三:
比较难写你再给你个思路吧,你完善下。
``` public class dashucheng
{
public string cs;//乘数
public string bcs;//被乘数
public string cheng()//乘操作返回字符串
{
string patten = "^[0-9]{1,}$";
string[] arr = null;
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(patten);//数字匹配
if (reg.IsMatch(cs) && reg.IsMatch(bcs))
{
string a = cs;
string b = bcs;
string result;
int[] c = new int[a.Length];
int[] d = new int[b.Length];
int[] f = new int[100];
arr = new string[a.Length];
int index = 0;
for (int i = 0; i < arr.Length; i++)//将字符串格式乘数存放到数组1
{
arr[i] = a.Substring(index + i, 1);
c[a.Length - i - 1] = int.Parse(arr[i]);
}
arr = new string[b.Length];
for (int i = 0; i < arr.Length; i++)//将字符串格式被乘数存放到数组2
{
arr[i] = a.Substring(index + i, 1);
d[b.Length - i - 1] = int.Parse(arr[i]);
}
result = a;//进行乘操作赋值给result比较难写没写出来,需要自己写
return result;
}
else if (cs == "" || bcs == "")
return "empty";
else
return "error";
}
}
//click事件内添加下列代码
dashucheng xc = new dashucheng();
xc.cs = textBox1.Text;
xc.bcs = textBox2.Text;
try
{
string result;
result =xc.cheng();
if (result != "error" && result != "empty")
textBox3.Text = result;
else
MessageBox.Show(result);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}