|
|
| Autor |
Nachricht |
Paulchen16 Full Member

Anmeldungsdatum: 18.03.2010 Beiträge: 90
|
Verfasst am: 18 März 2010 - 17:21:07 Titel: C++ Klasse Polynom |
|
|
Ich soll eine Klasse Polynom mit diversen Funktionen implementiern
Könnte die Klassendefinition bzw der Konstruktor und der Destruktor in etwa so ausschaun:
#ifndef POLY_H
#define POLY_H
class Poly
{
public:
Poly(const unsigned int n,const double *a); // Konstruktor
~Poly(); // Destruktor
private:
int degree; // Ordnung des Polynoms
double *coeffs; // Feld der Koeffizienten
};
#endif
//==================================
Poly::Poly(const unsigned int n,const double *a)
{
degree=n;
*coeffs=new double[poly.degree+1];
for(unsigned i=0;i<=degree;i++)
{
coeffs[i]=a[i];
}
}
Poly::~Poly()
{
delete[] coeffs;
} |
|
 |
Jockelx Senior Member

Anmeldungsdatum: 24.06.2005 Beiträge: 3255
|
Verfasst am: 18 März 2010 - 17:55:11 Titel: |
|
|
Hallo,
ist ganz gut, aber beachte die Regel der grossen Drei oder
nutze besser einen vector<double> oder tr1:array statt Destruktor und Array. |
|
 |
Paulchen16 Full Member

Anmeldungsdatum: 18.03.2010 Beiträge: 90
|
Verfasst am: 21 März 2010 - 19:08:20 Titel: |
|
|
Ich glaube wir müüsen lt Angabe dieses Problem so lösen.
Habe allerdings noch eine Frage dazu:
Kann ich in der Funktionenimplementierung in der cpp-file mittels poly.degree bzw poly.coeff auf die Werte zugreifen?
Weil irgendwie funktioniert das alles nicht ganz so wie vorgestellt |
|
 |
Annihilator Senior Member

 Anmeldungsdatum: 18.05.2007 Beiträge: 6395 Wohnort: (hier nicht mehr aktiv)
|
Verfasst am: 21 März 2010 - 19:34:39 Titel: |
|
|
Nicht wenn sie private deklariert sind. Du kannst (und solltest) das über Getter und Setter regeln, also spezielle Methoden, welche das kontrollierte Lesen und Schreiben von Attributen ermöglichen. _________________ Schnauze voll von Error 500 und co?
bildungs-foren.de |
|
 |
Paulchen16 Full Member

Anmeldungsdatum: 18.03.2010 Beiträge: 90
|
Verfasst am: 22 März 2010 - 20:23:15 Titel: |
|
|
so das wäre mein programm bzw das wie ich mir das vorstellen würde
aber es funktioniert einfach nicht
| Code: |
//Poly.cpp
//includes the definition and declaration of some functions using polynoms
#include <iostream>
#include <string>
#include "Poly.h"
using namespace std;
Poly::Poly()
{
}
//==========================================
//constructor
//The constructor takes an array filled with doubles and an int representing
//the degree of the polynom and creates a new polynom
Poly::Poly(const unsigned int n,const double *a)
{
degree=n;
coeffs=new double[n+1];
for(unsigned i=0;i<=n;i++)
{
coeffs[i]=a[i];
}
}
//==========================================
//copy assignment operator
//object assignment is performed with this operator
Poly::Poly& operator=(const Poly& poly)
{
degree=poly.getDegree;
coeffs=new double[degree+1];
double* coeff2=poly.getCoeffs();
for(unsigned i=0;i<=degree;i++)
{
coeffs[i]=coeff2[i];
}
return *this;
}
//getCoeffs
//is an auxilary function which returns the coefficients array
double* Poly::getCoeffs()
{
return coeffs;
}
//===========================================
// destructor
// deletes thze store which was dynamically allocated by the constructor
Poly::~Poly()
{
delete[] coeffs;
}
//============================================
//getDegree
//return the degree of a polynom
int Poly::getDegree()
{
return degree;
}
//============================================
//eval
// takes a double and return the value of the polynom
double Poly::eval(double x)
{
double x=x;
int degree=getDegree();
double* coeff2=getCoeffs();
double value=coeff2[0];
for(unsigned i=1;i<=degree;i++)
{
value=value*x+coeff2[i];
}
return value;
}
//============================================
//add
//takes two polynoms a and b and
//adds the polynom b to the polynom a modifieing a
Poly Poly::add(const Poly&b)
{
Poly d=a+b;
a=d;
return a;
}
//=============================================
//print
// takes a string and prints out the polynom using the string
void Poly::print(char *x)
{
int degree= getDegree();
double* coeff2=getCoeffs();
for unsigned i=0;i<=degree-1;i++)
{
if(coeff2[i]!=0)
{
if(coeff2[i]>0)
{
cout<<" +";
}
if(coeff2[i]==-1)
{
cout<<"-";
}
if(coeff2[i]==1)
{
cout<<"+";
}
else
{
cout<<coeff2[i];
}
cout<<*x<<"^"<<degree-i;
}
}
if(coeff2[degree]!=0)
{
if(coeff2[degree]>0)
{
cout<<" +"<<coeff2[degree]<<*x;
}
else
{
cout<<coeff2[degree]<<*x;
}
}
if(coeff2[degree+1]!=0)
{
if(coeff2[degree+1]>0)
{
cout<<" +"<<coeffs2[degree+1];
}
else
{
cout<<coeff2[degree+1];
}
}
}
//=================================================
//setAccuracy
//takes to doubles a and b and sets de maximal errors that can occur
void Poly::setAccuracy(double a,double b)
{
if(a<b)
{
absoluteErrorMax=a;
relativeErrorMax=b;
}
else
{
absoluteErrorMax=b;
relativeErrorMax=a;
}
}
//================================================
//equals
//is an auxilary function for comparing two arrays using the maximal errors
//which wer set in the setAccuracy function
static bool Poly::equals(const Poly& a,const Poly& b)
{
int degreea=a.getDegree();
double* coeffa=a.getCoeffs();
double* coeffb=b.getCoeffs();
for(int i=0;i<=a.degreea+1;i++)
{
if(fabs(coeffa[i]-coeffb[i])<Poly::absoluteErrorMax)
{
return true;
}
float relativeError;
if(fabs(coeffb[i])>fabs(coeffa[i]))
{
relativeError=fabs((coeffa[i]-coeffb[i])/coeffb[i]);
}
else
{
relativeError=fabs((coeffa[i]-coeffb[i])/coeffb[i]);
}
if(relativeError<=Poly::relativeErrorMax)
{
return true;
}
else
{
return false;
}
}
}
//==================================================
//operator ==
//takes two polynoms and compares them with help of the equals function
Poly::Poly operator==(const Poly& a,const Poly& b)
{
int degreea=a.detDegree();
int degreeb=b.getDegree();
if(a.degree != b.degree)
{
cout<<"not equal";
}
if(Poly::equals(a,b))
{
cout<<"equal";
}
else
{
cout<<"not equal";
}
}
//====================================================
//operator []
//takes an int which is representing an exponent and returns the coefficient
//of this special exponent
Poly::Poly operator[](int n)
{
int degree=getDegree();
double* coeff2=getCoeffs();
return coeff2[degree-n];
}
//=====================================================
//operator +
// takes two polynoms a and b and builds the sum of them and saves it in a
//new polynom d
Poly::Poly operator+(const Poly& a,const Poly& b)
{
int degreea=a.getDegree();
int degreeb=b.getDegree();
double* coeffa=a.getCoeffs();
double* coeffb=b.getCoeffs();
if(degreea<=degreeb)
{
double* coeffd= new double[degreeb+1];
unsigned int x=0; y=degreea+1; z=degreeb+1;
unsigned int m=z-y;
for(x; x<=y; x++)
{
coeffd[z]=coeffb[z]+coeffa[y];
z--;
y--;
}
for(unsigned int n=1; n<=m; n++)
{
coeffd[n]=coeffb[n];
}
}
else
{
double* coeffd= new double[degreea+1];
unsigned int x=0; y=degreeb+1; z=degreea+1;
unsigned int m=z-y;
for(x; x<=y; x++)
{
coeffd[z]=coeffa[z]+coeffb[y];
z--;
y--;
}
for(unsigned int n=1; n<=m; n++)
{
coeffd[n]=coeffa[n];
}
}
return d;
}
// ---------------------------------------------------------
// Poly.h
// class File for manipulation and associated operations with polynoms
//
//
// ----------------------------------------------------------
//class Poly is a class working on polynoms.
//It´ represented by a dynamically expanded array, which consits the coeffincents
//of the Polynom
#ifndef POLY_H
#define POLY_H
class Poly
{
public:
Poly(const unsigned int n,const double *a); // Konstruktor
Poly(const Poly poly&); // Copy-Konstruktor
Poly& operator=(const Poly& poly); //Copy Assignment Konstruktor
double* getCoeffs() const; //Hilfsfunktion zum auslesen der Koeffizienten
~Poly(); // Destruktor
Poly operator+(const Poly& a, const Poly& b); // Summe zweier Polynome
double eval(double x); // Funktionswert
int getDegree(); //Grad des Polynoms
Poly add(const Poly&b); //Hinzufügen eines Polynoms
Poly operator==(const Poly& a,const Poly& b); //Vergleichen von 2 Polynomen
void print(char *x); //Ausdruck des Polynoms
void setAccuracy(double a,double b); //setzt die Genauigkeit
Poly operator[](int n); //bestimmter Koeffizient
private:
int degree; // Ordnung des Polynoms
double *coeffs; // Feld der Koeffizienten
static double absoluteErrorMax; //absolute Genauigkeit
static double relativeErrorMax; //relative Genauigkeit
static bool equals(const Poly& a,const Poly& b); //Hilfsvergleichfkt
};
#endif
| [/code] |
|
 |
Jockelx Senior Member

Anmeldungsdatum: 24.06.2005 Beiträge: 3255
|
Verfasst am: 22 März 2010 - 23:14:57 Titel: |
|
|
| Zitat: | aber es funktioniert einfach nicht
|
Aha. |
|
 |
|