#include <iostream>
#include <iomanip>
#include "wielomian.h"
using namespace std;

Wielomian::Wielomian()
{
	st=0;
	wsp=new double[1];
	*wsp=0;
}

Wielomian::Wielomian(double *w,int st)
{
	this->st=st;
	wsp=new double[st+1];
	for(int i=0; i<=st; i++)
		*(wsp+i)=*(w+i);
}

Wielomian::Wielomian(const Wielomian & w)
{
	int stopien=w.st;
	this->st=stopien;
	wsp=new double[stopien+1];
	for(int i=0; i<=stopien; i++)
		*(wsp+i)=*(w.wsp+i);
}

Wielomian Wielomian::Pochodna(int st)
{
	if(st > 1 ) return Pochodna().Pochodna(st-1);

	Wielomian w;

	if(this->st <= 0 ) return w;

	w.st= this->st -1;
	delete [] w.wsp ;
	w.wsp = new double[w.st+1];
	for(int i=0;i<this->st;i++)
		w.wsp[i] = (i+1)*wsp[i+1];

	return w;

}

void Wielomian::Wyswietl()
{
	for(int i=st; i>0; i--)
		if(wsp[i]!=0) 
		{
			if(wsp[i]!=1.0) cout << showpos << fixed << setprecision(2) << wsp[i] << noshowpos ;
			if(i!=1) cout << " x^"  << i  << " "; else cout << " x " ;
		}
	if (wsp[0]!=0 || st == 0) 	cout << showpos << fixed << setprecision(2) << wsp[0] << noshowpos ;
	cout << endl;
}


Wielomian::~Wielomian()
{
	delete [] wsp;
}

Wielomian Dodaj(const Wielomian &w1,const Wielomian &w2)
{
	const Wielomian *max=&w1;
	const Wielomian *min=&w2;
	if(max->st<min->st)
	{
		max=&w2;
		min=&w1;
	}
	Wielomian w(*max);
	for(int i=0;i<=min->st;i++) w.wsp[i]+=min->wsp[i];
	return w;
}
Wielomian Wielomian::operator+(const Wielomian &w)
{
	return Dodaj(w,*this);
}


Wielomian& Wielomian::Wczytaj()
{
	int st;
	cout << "Stopien wielomianu ?" << endl ;
	cin >> st;
	this->st=st;
	this->wsp=new double[st+1];

	for(int i=st; i>=0; i--)
	{
		cout << "a[" << i << "]= " ;
		cin >> this->wsp[i];
	}
	return *this;
}
