多项式算子重载

时间:2012-11-17 22:26:02

标签: c++ operator-overloading

我的代码存在问题。

我需要使用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;
}

MAIN

#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;
}

1 个答案:

答案 0 :(得分:2)

问题最有可能发生在InitializePolynomial:你传递一个指向该函数的指针,但在函数内部它仍然是一个局部变量,这意味着对变量的更改(如分配给该指针)将被反映出来一旦函数返回,就在参数上。您必须将参数作为引用传递:

static void InitializePolynomial ( Polynomial *& );

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

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

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

    Polynomial FirstPolynomial(size);

    // ...
}

无需单独的初始化函数。