C ++:为什么返回内存地址而不是公式值?

时间:2014-08-01 21:50:23

标签: c++ class polynomials

我正在编写一个类来添加,减去和乘以多项式(现在进行1周!)。无论如何,代码编译,但我看到输出中的内存地址。

我无法弄清楚为什么会这样。

有人能引导我朝着正确的方向前进吗?

先谢谢你一起来看看! 莱恩

代码:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class Poly
{
private:

//  int ord;                            // the order of the polynomial
//  int coeff[100];

public:

    int ord;                            // the order of the polynomial
    int coeff[100];


    int a, b, c;
    Poly();                             // constructor
    Poly addition(Poly b);              // adds 2 polynomials
    Poly subtraction(Poly b);           // subtracts 2 polynomials
    Poly multiplication(Poly b);        // multiplies 2 polynomials
    int evaluate(int);                  // uses Horner's method to compute and return the polynomial evaluated at x
    Poly differentiate();               // 
    void set(int, int);                 // mutator function
    int order();
    void print();                       // prints the results
};

Poly::Poly()                            // the default constructor
{
    for (int i = 0; i < 100; i++)
    {
        coeff[i] = 0;
    }
}

void Poly::set(int a, int b)            // mutator function
{
    // coeff = new Poly[b + 1];
    coeff[b] = a;
    ord = order();
}

int Poly::order()
{
    int d = 0;
    for (int i = 0; i < 100; i++)
        if (coeff[i] != 0) d = i;
        return d;
}

void Poly::print()
{
    int coeff[] = { 0 };

    for (int i = 99; i >= 0; i--)
    {
        if (coeff[i] != 0)
        {
            cout << coeff[i] << "x^" << i << " ";
        }
    }
}

int Poly::evaluate(int x)
{
    int p = 0;
    for (int i = ord; i >= 0; i--)
        p = coeff[i] + (x * p);
    return p;
}

Poly Poly::differentiate()
{
    if (ord == 0)
    {
        Poly t;
        t.set(0, 0);
        return t;
    }

    Poly deriv;
    deriv.ord = ord - 1;

    for (int i = 0; i < ord; i++)
        deriv.coeff[i] = (i + 1) * coeff[i + 1];
    return deriv;
}

Poly Poly::addition(Poly b)
{
    Poly a = *this;
    Poly c;

    for (int i = 0; i <= a.ord; i++)
        c.coeff[i] += a.coeff[i];
    for (int i = 0; i <= b.ord; i++)
        c.coeff[i] += b.coeff[i];

    c.ord = c.order();

    return c;
}

Poly Poly::subtraction(Poly b)
{
    Poly a = *this;
    Poly c;

    for (int i = 0; i <= a.ord; i++)
        c.coeff[i] += a.coeff[i];
    for (int i = 0; i <= b.ord; i++)
        c.coeff[i] -= b.coeff[i];

    c.ord = c.order();

    return c;
}

Poly Poly::multiplication(Poly b)
{
    Poly a = *this;
    Poly c;

    for (int i = 0; i <= a.ord; i++)
    for (int j = 0; j <= b.ord; j++)
        c.coeff[i + j] += (a.coeff[i] * b.coeff[j]);
    c.ord = c.order();
    return c;
}

int main()
{
    Poly a, b, c, d;
    a.set(7, 4);                    //  7x^4
    a.set(1, 2);                    //  x^2

    b.set(6, 3);                    //  6x^3
    b.set(-3, 2);                   //  -3x^2

    c = a.subtraction(b);           //  (7x^4 + x^2) - (6x^3 - 3x^2)

    c.print();

//  cout << "\n";

//  d = c.differentiate().differentiate();

//  d.print();

//  cout << "\n";

//  cout << c.evaluate(2);          //  substitute x with 2
//  cin.get();

    return 0;
}

修改

这就是我所看到的。知道如何解决这个问题???

enter image description here

3 个答案:

答案 0 :(得分:7)

据我所知,你根本不应该看到任何东西。

print()方法以

开头
int coeff[] = { 0 };

这将隐藏成员变量coeff,并将设置为0,

答案 1 :(得分:0)

如果您发布了一些您看到的输出,这将有所帮助。尝试包装括号中输出的每个表达式。例如,cout << (coeff[i])而不是cout << coeff[i]。此外,在print方法中,您编写int coeff[] = {0},尽管已将coeff声明为成员变量。

答案 2 :(得分:0)

您的代码将有效。只需在“Poly :: Print()”方法中进行以下更改

void Poly::print()

    {
        //int coeff[] = { 0 };

        for (int i = 99; i >= 0; i--)
        {
            if (coeff[i] != 0)
            {
                cout << coeff[i] << "x^" << i << " ";
            }
        }
    }

或者您可以复制此代码并将其替换为您当前的“Poly :: Print()”方法。

问题的根本原因是您正在打印一个局部变量数组,您已在“Poly :: print()”方法中声明了该数组。根据你的声明,这个数组只有一个元素。因为

int coeff[] = {0};

上面的代码实际上创建了一个大小为1的数组。