C++多项式乘法的重载 *

发布于 2024-08-12 18:19:08 字数 1697 浏览 9 评论 0原文

所以我一直在开发一个多项式类,其中用户输入: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

再可℃爱ぅ一点好了 2024-08-19 18:19:08

您似乎没有在 operator * () 中将 temp.coefficient[i+j] 初始化为零。

temp.coefficient = new int [count];
std::memset (temp.coefficient, 0, count * sizeof(int));

You don't appear to initialise the temp.coefficient[i+j] to zero in your operator * ().

temp.coefficient = new int [count];
std::memset (temp.coefficient, 0, count * sizeof(int));
夏夜暖风 2024-08-19 18:19:08

将 -842150450 转换为十六进制,以找到在调试版本中的 CRT。这有助于找到代码中的错误:

    temp.coefficient = new int [count];
    // Must initialize the memory
    for (int ix = 0; ix < count; ++ix) temp.coefficient[ix] = 0;

顺便说一句,还有很多其他错误,祝您好运修复它们。

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:

    temp.coefficient = new int [count];
    // Must initialize the memory
    for (int ix = 0; ix < count; ++ix) temp.coefficient[ix] = 0;

There are plenty other bugz btw, good luck fixing them.

眼睛会笑 2024-08-19 18:19:08

会给

temp.coefficient = new int [count];

你一个零数组吗?

否则,在 for 循环中,您将向垃圾中添加内容。

Does

temp.coefficient = new int [count];

give you an array of zeroes?

Otherwise in your for loop you're adding stuff to garbage.

柠栀 2024-08-19 18:19:08

替换

temp.coefficient = new int [count];

temp.coefficient = new int [count]();

以便将数组值初始化为零。

Replace

temp.coefficient = new int [count];

by

temp.coefficient = new int [count]();

in order to zero-initialize the array values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文