0% found this document useful (0 votes)
23 views3 pages

TEST

Uploaded by

Florin Stancu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

TEST

Uploaded by

Florin Stancu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

# include <cmath>

# include <conio.h>
# include <iostream>
using namespace std;
class Polinom
{
int n;
int*a;
public:
Polinom( )
{
n=0; // C. Impl.
a=new int(0);
}
Polinom(const int gr); // C. Init.
Polinom(const int gr, const int* coef); // C. conv./atr.
Polinom(const Polinom&); // C. copiere.
~Polinom( )
{
delete []a; // Destr.
}
Polinom& operator = (const Polinom&);
int operator ~ ( )
{
return n; //Grad ( )
}
int operator ==(const Polinom&);
int operator !=(const Polinom& P)
{
return !(*this==P);
}
double operator ()(double); // double ValPol (double);
int& operator [](int g)
{
return a[n-g]; // int& Coef (int g)
}
Polinom operator - ( );
Polinom operator + (Polinom);
ostream& Write(ostream&);
};
int Polinom::operator == (const Polinom& P)
{
if (n!=P.n) return 0;
for (int i=0; i<=n; i++) if (a[i]!=P.a[i]) return 0;
return 1;
}
Polinom::Polinom(const int gr)
{
n=gr;
a=new int[n+1];
for (int i=0; i<=n; i++) cin >> a[i];
}
Polinom::Polinom(const int gr, const int* coef)
{
// n=gr; a=coef; !!!
n=gr;
a=new int[n+1];
for (int i=0; i<=n; i++) a[i]=coef[i];
}
Polinom::Polinom(const Polinom& P)
{
n=P.n;
a=new int[n+1];
for (int i=0; i<=n; i++) a[i]=P.a[i];
}
Polinom& Polinom:: operator = (const Polinom& P)
{
n=P.n;
if (a!=P.a)
{
delete []a;
a=new int[n+1];
for (int i=0; i<=n; i++) a[i]=P.a[i];
}
return *this;
}
double Polinom::operator () (double x)
{
double* c=new double[n+1];
for (int i=0; i<=n; i++) c[i]=a[n-i];
return poly(x,n,c);
}
Polinom Polinom::operator - ()
{
int* c=new int[n+1];
for (int i=0; i<=n; i++) c[i]=-a[i];
return Polinom(n,c);
}
ostream& Polinom::Write(ostream& s)
{
int i=0;
while (i<n && a[i]==0) i++;
for (; i<=n; i++) s<<a[i]<<"X^"<<n-i<<'+';
s<<"\b\ \n";
return s; // >>
}
ostream& operator << (ostream& s, Polinom P)
{
return P.Write(s);
}
Polinom Polinom::operator + (Polinom P)
{
if (n<P.n) return P+*this;
Polinom S=*this;
for (int i=0; i<=P.n; i++) S.a[n-i]+=P.a[P.n-i];
return S;
}
Polinom operator - (Polinom P, Polinom Q)
{
return P+-Q;
}
int main ()
{
clrscr();
int n;
cout << " grad : ";
cin >>n;
cout << " P : ";
Polinom P(n);
cout << P;
cout << P; //P.Print();
double x=-1;
cout << P(x) << endl;
P[1]=10;
cout << P[1]<< endl;
cout << P;
cout << P(-1) << endl;
Polinom Q=-P;
cout << " a) "<<Q;
cout << " b) "<<Q(-1) << endl;
cout << " c) "<<(-P)(-1) << endl;
if (Q==P) cout << " Egale ";
else cout << " Diferite ";
if (Q==-P) cout << " Q==-P ";
else cout << " Not Q==-P ";
if (Q!=P) cout << " Diferite..";
else cout << " Egale.....";
if (Q!=-P) cout << " Not Q==-P\n";
else cout << " Q==-P \n";
cout << ~P <<endl; // P.Grad();
cout << ~Q <<endl; // P.Grad();
cout << P;
int m;
cout << " grad : ";
cin >>m;
cout << " R : ";
Polinom R(m);
cout << R;
cout << P+R ;
if (P(13)+R(13)==(P+R)(13)) cout << "Ok +\n";
else cout << " + ?\n";
cout << P-R ;
if (P(13)-R(13)==(P-R)(13)) cout << "Ok -\n";
else cout << " - ?\n";
getche();
}

You might also like