【问题标题】:Polynomial Operator Overloading [closed]多项式运算符重载
【发布时间】:2012-11-17 22:26:02
【问题描述】:

我的代码有问题。

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

【问题讨论】:

  • 逐行执行代码会发生什么?
  • 你不是像昨天那样创建了一个问题并且它被关闭了吗?
  • 在提出问题之前,请尝试找出问题所在,如果仍然无法解决,请提出问题。不要只是在这里扔一些代码并期望人们为您做所有事情。
  • 就像我一直在cin &gt;&gt; FirstPolynomial
  • 伙计,这个问题很大:)

标签: c++ operator-overloading


【解决方案1】:

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

static void InitializePolynomial ( Polynomial *& );

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

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

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

    Polynomial FirstPolynomial(size);

    // ...
}

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

【讨论】:

  • 非常感谢,问题就在这里。
猜你喜欢
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
  • 2012-02-06
相关资源
最近更新 更多