C++多项式乘法的重载 *
所以我一直在开发一个多项式类,其中用户输入: 1x^0 + 2x^1 + 3x^2... 和 1,2,3 (系数)存储在 int 数组中
我的重载 + 和 - 函数可以工作但是,* 不起作用。无论输入什么,总是显示-842150450
什么时候应该是 (5x^0 + x^1) * (-3x^0 + x^1) = -15x^0 + 2x^1 + 1x^2
或 (x+5)(x-3) = x^2 +2x - 15
我正在使用重载的 * 函数,例如:多项式乘法 = 一 * 二;
我猜问题是 strtol(p, &endptr, 10) 因为它使用了 long int,但是,加法和减法工作完美
我的构造函数
Polynomial::Polynomial(char *s)
{
char *string;
string = new char [strlen(s) + 1];
int length = strlen(string);
strcpy(string, s);
char *copy;
copy = new char [length];
strcpy(copy, string);
char *p = strtok(string, " +-");
counter = 0;
while (p)
{
p = strtok(NULL, " +-");
counter++;
}
coefficient = new int[counter];
p = strtok(copy, " +");
int a = 0;
while (p)
{
long int coeff;
char *endptr;
coeff = strtol(p, &endptr, 10); //stops at first non number
if (*p == 'x')
coeff = 1;
coefficient[a] = coeff;
p = strtok(NULL, " +");
a++;
}
}
和重载的 * 函数
Polynomial Polynomial::operator * (const Polynomial &right)
{
Polynomial temp;
//make coefficient array
int count = (counter + right.counter) - 1;
temp.counter = count;
temp.coefficient = new int [count];
for (int i = 0; i < counter; i++)
{
for (int j = 0; j < right.counter; j++)
temp.coefficient[i+j] += coefficient[i] * right.coefficient[j];
}
return temp;
}
这是我的整个代码: http://pastie.org/721143
So I have been developing a polynomial class where a user inputs: 1x^0 + 2x^1 + 3x^2... and 1,2,3 (the coefficients) are stored in an int array
My overloaded + and - functions work, however, * doesnt work. No matter the input, it always shows -842150450
when is should be (5x^0 + x^1) * (-3x^0 + x^1) = -15x^0 + 2x^1 + 1x^2
or (x+5)(x-3) = x^2 +2x - 15
I'm using the overloaded * function like : Polynomial multiply = one * two;
Im guessing the problem is strtol(p, &endptr, 10) since it uses a long int, however, adding and subtracting works perfectly
My constructor
Polynomial::Polynomial(char *s)
{
char *string;
string = new char [strlen(s) + 1];
int length = strlen(string);
strcpy(string, s);
char *copy;
copy = new char [length];
strcpy(copy, string);
char *p = strtok(string, " +-");
counter = 0;
while (p)
{
p = strtok(NULL, " +-");
counter++;
}
coefficient = new int[counter];
p = strtok(copy, " +");
int a = 0;
while (p)
{
long int coeff;
char *endptr;
coeff = strtol(p, &endptr, 10); //stops at first non number
if (*p == 'x')
coeff = 1;
coefficient[a] = coeff;
p = strtok(NULL, " +");
a++;
}
}
and the overloaded * function
Polynomial Polynomial::operator * (const Polynomial &right)
{
Polynomial temp;
//make coefficient array
int count = (counter + right.counter) - 1;
temp.counter = count;
temp.coefficient = new int [count];
for (int i = 0; i < counter; i++)
{
for (int j = 0; j < right.counter; j++)
temp.coefficient[i+j] += coefficient[i] * right.coefficient[j];
}
return temp;
}
And heres my entire code: http://pastie.org/721143
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您似乎没有在
operator * ()
中将temp.coefficient[i+j]
初始化为零。You don't appear to initialise the
temp.coefficient[i+j]
to zero in youroperator * ()
.将 -842150450 转换为十六进制,以找到在调试版本中的 CRT。这有助于找到代码中的错误:
顺便说一句,还有很多其他错误,祝您好运修复它们。
Convert -842150450 to hex to find back one of the magic values used in the CRT in the debug build. That helps finding the bug in your code:
There are plenty other bugz btw, good luck fixing them.
会给
你一个零数组吗?
否则,在 for 循环中,您将向垃圾中添加内容。
Does
give you an array of zeroes?
Otherwise in your for loop you're adding stuff to garbage.
替换
为
以便将数组值初始化为零。
Replace
by
in order to zero-initialize the array values.