【发布时间】:2016-05-19 01:10:28
【问题描述】:
我正在编写一个程序来计算多项式上的运算,并且我已经完成了其他运算 (+ - *) 但坚持除法,我总是收到此错误,如代码所示
static int[] DividePolnomials(int[] pol1, int[] pol2)
{
int tmp = 0;
int power_tmp = 0;
int[] result_tmp;
int[] result;
if (pol1.Length > pol2.Length)
{
result = new int [pol1.Length];
result_tmp = new int[pol1.Length];
for (int i = 0; pol2.Length<=pol1.Length; i++)
{
if (pol2[pol2.Length-i-1] == 0) // here is the problem it gives that the index is outside the bounds
{
continue;
}
else
{
tmp = pol1[pol1.Length - i - 1] / pol2[pol2.Length - i - 1];
power_tmp = (pol1.Length - i - 1) - (pol2.Length - i - 1);
result[power_tmp] = tmp;
result_tmp = Multiply(result, pol2);
pol1 = SubtractPolnomial(pol1, result_tmp);
}
}
}
else
{
result = new int [pol1.Length];
}
return result;
}
由于这个错误,我还没有完成代码中的所有其他场景,但我希望在两个多项式长度相等的情况下获得任何帮助
【问题讨论】:
-
你的意思是整数除法和余数吗?例如
1 / (x + 1)应该返回0(并且x + 1应该是余数) -
我正在尝试应用长除法多项式,因为它在这里 purplemath.com/modules/polydiv2.htm
标签: c# arrays indexoutofboundsexception polynomials