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

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

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

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


Wielomian Wielomian::Pochodna(int k)
{
   if(k >= st) 
   {
      Wielomian w;
      return w;
   }

	if (k == 1)
	{
		Wielomian w(this->st-1, this->wsp);
		for (int i = st; i > 0; i--)
			w.wsp[i-1] = this->wsp[i] * i;

		return w;
	}
	return Pochodna(1).Pochodna(k-1);
}

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

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

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

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