#include <iostream>
#include <cmath>
using namespace std;
int* add(int* a, int sa, int* b, int sb)
{
int* res_k = new int [sa+1];
for (int i=0;i<sa-sb;i++)
res_k[i]=a[i];
for (int i=sa-sb; i <=sa; i++)
{
res_k[i]=a[i] + b[i-sa+sb];
}
return res_k;
}
class Polinom
{
public:
int* koef;
int step;
Polinom()
{
}
Polinom(int* koef, int step)
{
this->koef = koef;
this->step = step;
}
void show()
{
for(int i=0;i<step;i++)
{if (this->koef[i]>0 && i!=0)
cout<<" + ";
cout<<this->koef[i]<<"x^"<<step-i;
}
cout<<" + "<<koef[step];
}
int calc(int x)
{
int res = 0;
for(int i=0;i<=step;i++)
res+=koef[i]*pow(x,step-i);
return res;
}
Polinom operator+(Polinom a)
{
Polinom b;
int* res_k;
if (this->step>a.step)
{
res_k = add(this->koef, this->step, a.koef, a.step);
b.koef = res_k;
b.step = this->step;
}
else
{
res_k = add( a.koef, a.step,this->koef, this->step);
b.koef = res_k;
b.step = a.step;
}
return b;
}
Polinom operator*(Polinom a)
{
int degree = this->step + a.step;
int* res_k = new int [degree + 1];
for (int i = 0; i<degree + 1; i++)
res_k[i]=0;
for (int i = 0; i <= this->step; i++)
for (int j = 0; j <= a.step; j++)
res_k[i + j] += this->koef[i] * a.koef[j];
Polinom c(res_k, degree);
return c;
}
};
int main()
{
int k[] = {2,3,8,0,3};
int l[] = {1,4,7};
Polinom a(k, 4);
Polinom b(l, 2);
(a*b).show();
// cout<<endl;
// cout<<a.calc(-2);
return 0;
}