include iostream include cmath using namespace std int add int int sa

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
#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;
}