#include "wielomian.h" #include #include #include #include using namespace std; Wielomian::Wielomian() { //cout << "Konstruktor domyslny" << endl; wsp = new double[1]; st = 0; wsp[0]=0; } Wielomian::Wielomian(int stopien) { wsp = new double[stopien+1]; st = stopien; } Wielomian::Wielomian(int st, const double *wsp) { //cout << "Konstruktor" << endl; this->wsp = new double[st+1]; this->st = st; double *p=this->wsp + st; while(st-- >= 0 ) *p-- = *wsp++; } Wielomian::Wielomian(const Wielomian &w) { //cout << "Konstruktor kopiujacy" << endl; wsp = new double[w.st+1]; st = w.st; double *p1=this->wsp+st; double *p2=w.wsp + st; while(p1 >= wsp) *p1--=*p2--; } Wielomian &Wielomian::RazyStala(double stala) { for (int i=0; i<=st; i++) wsp[i] *= stala; return *this; } Wielomian& Wielomian::Wpisz() { //cout << "Stopien = "; //cin >> st; //delete [] wsp; //wsp = new double[st+1]; //for(int i=st; i> 0 ; i--) //{ // cout << "x^" << i << " * "; // cin >> wsp[i]; //} //cout << "+ "; //cin >> wsp[0]; cin >> *this; // mamy przeciez przeciazony operator >> return *this; } void Wielomian::Wypisz() { //for(int i=st; i >0;i--) //{ // if(wsp[i]!=0) // { // cout << showpos << setprecision(2) << wsp[i] << "x" << noshowpos << i ; // } //} //cout << showpos << setprecision(2)<< *wsp << endl; cout << *this; // mamy przeciazony operator << } Wielomian::~Wielomian() { //cout << "Destruktor" << endl; delete [] wsp; } Wielomian Dodaj(const Wielomian &w1,const Wielomian &w2) { Wielomian w=w1; int min_st = w2.st; if(w2.st>w1.st) { min_st=w1.st; w=w2; // oparator przypisania } for(int i=0;i<=min_st;i++) w.wsp[i]=w1.wsp[i]+w2.wsp[i]; return w; } Wielomian &Wielomian::operator =(const Wielomian &w) { // cout << "Operator przypisania" << endl; if (&w != this) { delete [] this->wsp; st=w.st; wsp = new double[w.st+1]; double *p1=this->wsp+st; double *p2=w.wsp + st; while(p1 >= wsp) *p1--=*p2--; } return *this; } Wielomian Wielomian::operator +(const Wielomian &w) { Wielomian w1(w); int min_st = this->st; if(st>w.st) { min_st=w.st; w1=*this; } for(int i=0;i<=min_st;i++) w1.wsp[i]=this->wsp[i]+w.wsp[i]; return w1; } ostream &operator <<(ostream &o,const Wielomian &w) { for(int i=w.st; i >0;i--) { if(w.wsp[i]!=0) { o << showpos << setprecision(4) << w.wsp[i] << "x" << noshowpos << i ; } } o << showpos << setprecision(4)<< w.wsp[0] ; return o; } istream &operator>>(istream &o,Wielomian &w) { cout << "Stopien = "; o >> w.st; delete [] w.wsp; w.wsp = new double[w.st+1]; for(int i=w.st; i> 0 ; i--) { cout << "x^" << i << " * "; cin >> w.wsp[i]; } cout << "+ "; o >> w.wsp[0]; return o; } Wielomian Wielomian::operator*(float x) { Wielomian w(*this); for(int i=0;i<=st;i++) { w.wsp[i]*=x; } return w; } Wielomian Wielomian::Pochodna(int ktora) { if (ktora == 1) { if(st == 0) { Wielomian w; return w; } Wielomian w(st-1); for(int i=1;i<=st;i++) w.wsp[i-1]=wsp[i]*(i); return w; } else return Pochodna().Pochodna(ktora-1); } double Wielomian::operator ()(double x) { double *w=wsp+st; double y =*w; while(w>wsp) y=x*y+*--w; return y; } Wielomian Wielomian::operator *() { return Pochodna(); } double &Wielomian::operator[](int index) { if(index > st) { double* nwsp = new double[index+1]; memset(nwsp,0,sizeof(*nwsp)*(index+1)); memcpy(nwsp,wsp,sizeof(*nwsp)*(st+1)); st=index; delete [] wsp; wsp = nwsp; } return wsp[index]; } Wielomian::Wielomian(const double x) { st=0; wsp = new double[1]; *wsp = x; } Wielomian& Wielomian::operator *=(const Wielomian &w) { Wielomian nw(w.st + this->st); memset(nw.wsp,0,sizeof(*nw.wsp)*(nw.st+1)); for(int i=0;i<=w.st;i++) { for(int j=0;j<=this->st;j++) { nw.wsp[i+j]+=(w.wsp[i]*this->wsp[j]); } } *this = nw; return *this; } Wielomian Wielomian::operator*(const Wielomian &w) { //int ns = w.st + this->st; //Wielomian nw(ns); // //memset(nw.wsp,0,sizeof(*nw.wsp)*(ns+1)); //for(int i=0;i<=w.st;i++) //{ // for(int j=0;j<=this->st;j++) // { // nw.wsp[i+j]+=(w.wsp[i]*this->wsp[j]); // } //} //return nw; Wielomian w1(*this); return w1*=w; }