#include "wielomian.h" #include #include #include using namespace std; Wielomian::Wielomian() { //cout << "Konstruktor domyslny" << endl; wsp = new double[1]; st = 0; wsp[0]=0; } 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; // jesli mamy 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; 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(2) << w.wsp[i] << "x" << noshowpos << i ; } } o << showpos << setprecision(2)<< 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; }