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
|
#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;
class Polynomial{
int degree;
char var;
double *coeff;
public:
explicit Polynomial(int n,char c): degree(n), var(c), coeff(new double[n]) {};
~Polynomial(){delete[] coeff;}
Polynomial(const Polynomial &p);
Polynomial &operator =(const Polynomial &p);
double operator [](int i) const;
double &operator [](int i);
double operator ()(double n);
friend const Polynomial operator +(const Polynomial &p1,const Polynomial &p2);
friend const Polynomial operator -(const Polynomial &p1,const Polynomial &p2);
friend const Polynomial operator *(const Polynomial &p1,const Polynomial &p2);
friend const Polynomial operator /(const Polynomial &p1,const Polynomial &p2);
friend const Polynomial operator %(const Polynomial &p1,const Polynomial &p2);
friend const Polynomial operator +=(Polynomial &p1,const Polynomial &p2);
friend const Polynomial operator -=(Polynomial &p1,const Polynomial &p2);
friend const Polynomial operator *=(Polynomial &p1,const Polynomial &p2);
friend const Polynomial operator /=(Polynomial &p1,const Polynomial &p2);
friend const Polynomial operator %=(Polynomial &p1,const Polynomial &p2);
friend ostream &operator <<(ostream &cout,const Polynomial &p);
friend bool operator ==(const Polynomial &p1,const Polynomial &p2);
friend bool operator !=(const Polynomial &p1,const Polynomial &p2);
};
double Polynomial::operator [](int i) const{
if(i<1 || i>degree) throw "Wrong index!";
return coeff[i];
};
double &Polynomial::operator [](int i){
if(i<1 || i>degree) throw "Wrong index!";
return coeff[i];
};
double Polynomial::operator ()(double n){
double sum(0);
for(int i=0;i<degree+1;i++)sum+=coeff[i]*pow(sum,i);
};
const Polynomial operator +(const Polynomial &p1,const Polynomial &p2){
if(p1.var == p2.var){
int degree3(0);
if(p1.degree<p2.degree) degree3 = p2.degree;
if(p1.degree>p2.degree) degree3 = p1.degree;
Polynomial p3(degree3,p1.var);
p3.coeff = new double [p3.degree];
for(int i=0;i<p3.degree;i++) p3.coeff[i]=0;
for(int i=0;i<p3.degree;i++) p3.coeff[i]=p1.coeff[i]+p2.coeff[i];
return p3;
}
else throw "These polynomials dont have the same variables.";
};
Polynomial::Polynomial(const Polynomial &p){
degree = p.degree; var = p.var;
coeff = new double[degree];
for(int i=0;i<degree;i++) p.coeff[i]=0;
copy(p.coeff,p.coeff + p.degree,coeff);
};
Polynomial &Polynomial::operator =(const Polynomial &p){
if(degree<p.degree || degree>p.degree) throw "Polynomiali nisu istog degreea.";
degree=p.degree; var=p.var;
coeff=new double[degree];
for(int i=0;i<p.degree;i++) p.coeff[i]=0;
copy(p.coeff,p.coeff + p.degree,coeff);
};
ostream &operator <<(ostream &cout,const Polynomial &p){
cout<<p.coeff[0]<<" + "<<p.coeff[1]<<"*"<<p.var;
for(int i=2;i<p.degree-1;i++)
cout<<" + "<<*(p.coeff+i)<<"*"<<p.var<<"^"<<i;
return cout<<" + "<<*(p.coeff + p.degree);
};
int main(){
Polynomial p(2,'x');
p[1]=2;p[2]=3;
Polynomial q(2,'x');
p[1]=3; p[2]=4;
cout<<p+q;
getch();
return 0;
}
|