-4

我的代码有问题。

我需要使用 输入多项式作为系数-指数对的列表operator>>,然后我需要按以下方式打印它:

coefficient*x**exponent+coefficient*x**exponent...

我认为我的代码是正确的,但没有打印任何内容。

标题

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <cctype>
#include <iomanip>
#include "Element.h"

class Polynomial
{
    public:
        Polynomial ( int size = 0) // initializes size of the polynomial
        {
            this->size = size;
            ptr_array = new Element[size] ;
        }

        static void InitializePolynomial ( Polynomial * );
        friend istream &operator>>( istream &, Polynomial & );
        friend ostream &operator<<( ostream &, const Polynomial & );

        int Getsize ( );
        Element GetElement (int);

    private:
        int size; // numbers of the terms in a polynomial
        Element *ptr_array; // A pointer array that stores pointers to the objects of Element
};

#endif 

#ifndef ELEMENT_H
#define ELEMENT_H

#include <iostream>
#include <sstream>
using namespace std;

class Element
{
    public:
        Element( int exponent = 0, double coefficient = 0 )
        {
            this->exponent = exponent;
            this->coefficient = coefficient;
        }

    void SetCoefficient ( double );
    void SetExponent ( int );
    double GetCoefficient ( );
    int GetExponent ( );

    private:
        int exponent;
        double coefficient;
};

#endif

CPP

#include "Polynomial.h"

void Polynomial :: InitializePolynomial ( Polynomial * Poly )
{
    int size;
    cout << "Initialisation of the polynomial" << endl;
    cout << "What is the size of your polynomial ? ";
    cin >> size;
    Poly = new Polynomial (size);
}

Element Polynomial :: GetElement(int i)
{
    return ptr_array[i];
}

istream &operator>>( istream &input, Polynomial &Poly )
{
    int i;

    for (i=0; i<12; i++)
    {
        double tempcoef;
        int tempexp;

        input>>tempcoef; 
        Poly.ptr_array[i].SetCoefficient(tempcoef);
        input.ignore(1,',');

        input>>tempexp; 
        Poly.ptr_array[i].SetExponent(tempexp);
        input.ignore(1,',');
    }

    return input;
}

/*
ostream &operator<<( ostream &output , Polynomial const &Poly )
{
    int i;
    for (i=0; i<12;i++)
    {
        if (i != 0) output<<setfill('+')<<setw(1);
        output<<Poly.ptr_array[i].GetCoefficient();
        output<<setfill('*')<<setw(1);
        output<<Poly.ptr_array[i].GetExponent();
        output<<setfill('*')<<setw(2);
    }

    return output;
}*/

ostream &operator<<(ostream &output, const Polynomial &polynom)
{

    for(int i=0; i< polynom.size ; i++)
    {
        if(pow(polynom.ptr_array[i].GetCoefficient(), polynom.ptr_array[i].GetExponent()) != 0)
        {
            if(i > 0 && (polynom.ptr_array[i].GetCoefficient() >= 0))
            {
                output<<"+";
            }

            if(polynom.ptr_array[i].GetExponent() == 0)
            {
                output<<polynom.ptr_array[i].GetCoefficient();
            }

            else if(polynom.ptr_array[i].GetExponent() == 1)
            {
                output<<polynom.ptr_array[i].GetCoefficient()<<"x";
            }

            else
            {
                output<<polynom.ptr_array[i].GetCoefficient()<< "*x**" << polynom.ptr_array[i].GetExponent();
            }
        }
    }
    output << endl;
    return output;
}

#include "Element.h"

void Element :: SetCoefficient ( double Coeff )
{
    coefficient = Coeff;
}

void Element :: SetExponent ( int Expo )
{
    exponent = Expo;
}

double Element :: GetCoefficient ( )
{
    return coefficient;
}

int Element :: GetExponent ( )
{
    return exponent;
}

主要的

#include "Element.h"
#include "Polynomial.h"

int main ( )
{
    Polynomial FirstPolynomial;

    Polynomial :: InitializePolynomial( &FirstPolynomial );

    cout << "You are creating your polynomial" << endl;
    cout << "Please enter the coefficient et exponent like that " << endl;
    cout << "(Coefficient, Exponent, Coefficient, Exponent ...)" << endl;
    cout << "--> ";

    cin >> FirstPolynomial;
    cout << FirstPolynomial;
}
4

1 回答 1

2

问题很可能出现在InitializePolynomial:您传递了一个指向该函数的指针,但在函数内部它仍然是一个局部变量,这意味着一旦函数返回,对变量所做的更改(如分配给该指针)将反映在参数上。您必须将参数作为参考传递:

static void InitializePolynomial ( Polynomial *& );

实际上,再次阅读您的代码,您遇到的问题比函数中的问题要大得多:您尝试为已经分配的对象分配内存!

当您FirstPolynomialmain函数中声明变量时,编译器将为您分配该对象。您应该main改为正确初始化它:

int main()
{
    int size;
    cout << "Initialisation of the polynomial" << endl;
    cout << "What is the size of your polynomial ? ";
    cin >> size;

    Polynomial FirstPolynomial(size);

    // ...
}

不需要有单独的初始化函数。

于 2012-11-17T22:37:02.220 回答