我的代码有问题。
我需要使用 输入多项式作为系数-指数对的列表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;
}