Вы находитесь на странице: 1из 5

#include <iostream.

h> class poly { double *coef; int deg; public: poly(int _deg = 0); poly(const poly &); poly(int k, double v); ~poly() {delete coef;} poly & operator = (const poly &); friend int operator == (const poly &,const poly &); double value(double); double & operator [](int k)const {return coef[k];} poly operator * (const poly &)const; poly operator / (const poly &)const; poly operator % (const poly &)const; poly operator + (const poly &)const; poly operator - (const poly &)const; poly operator - ()const; poly poly poly poly operator operator operator operator * / + (double); (double f){return (*this) * (1/f);} (double f){return (*this) + poly(0,f);} (double f){return (*this) - poly(0,f);}

friend poly operator * (double f, poly a){return a * f;} friend poly operator + (double f, poly a){return a + f;} friend poly operator - (double f, poly a){return -a + f;} friend ostream & operator << (ostream &, const poly &); friend istream & operator >> (istream &, poly &); void set(int m, double v[]);

};

void poly::set(int m, double v[]) { this->~poly(); deg = m; coef = new double[deg+1]; for(int k = 0; k <= deg; k++) coef[k] = v[k]; } poly::poly(int _deg) { deg = _deg; if (deg < 0) deg = 0; coef = new double[deg+1]; for(int k = 0; k <= deg; k++) coef[k] = 0; } poly::poly(const poly &a) { deg = a.deg; coef = new double[deg+1]; for(int k = 0; k <= deg; k++) coef[k] = a[k]; } poly& poly:: operator = (const poly &b)

if (this != &b) { this->~poly(); deg = b.deg; coef = new double[deg+1]; for(int k = 0; k <= deg; k++) coef[k] = b[k]; } return *this;

poly::poly(int k, double v) { deg = k; coef = new double[k+1]; for(int j = 0; j < deg; j++) coef[j] = 0; coef[deg] = v; } poly poly:: operator * (double f) { poly c(*this); for(int k = 0; k <= deg; k++) c[k] *= f; while (c.deg && c[c.deg] == 0) c.deg--; return c; } poly poly::operator + (const poly &b)const { if (deg <= b.deg) { poly c(b); for(int k = 0; k <= deg; k++) c[k] += coef[k]; while (c.deg && c[c.deg] == 0) c.deg--; return c; } else return b + *this; } poly poly::operator - ()const { poly c(*this); for(int k = 0; k <= deg; k++) c[k] = -c[k]; return c; } poly poly::operator - (const poly &b)const { return *this + -b; } poly poly::operator * (const poly &b)const { poly c(deg + b.deg); for (int k = 0; k <= deg; k++) for (int j = 0; j <= b.deg; j++) c[k+j] = c[k+j] + coef[k]*b[j]; while (c.deg && c[c.deg] == 0) c.deg--; return c; }

poly poly::operator / (const poly &b)const { if (deg < b.deg) return poly(0,0); poly c(deg - b.deg), a(*this); int jc = c.deg, ja = deg; while (jc >= 0) { c[jc] = a[ja]/b[b.deg]; a = a - b*poly(jc,c[jc]); jc--; ja--; } return c; } poly poly::operator % (const poly &b)const { poly a(*this); if (deg >= b.deg) { a = a - (a/b)*b; while (a[a.deg]==0) a.deg--; } return a; } int operator == (const poly &a,const poly &b) { if (a.deg != b.deg) return 0; int k = 0; while (k <= a.deg && a[k] == b[k]) k++; return k > a.deg; } double poly::value(double x) { double val = coef[deg]; for (int k = deg-1; k >=0; k--) val = val*x + coef[k]; return val; } ostream & operator << (ostream & pout, const poly &p) { int k = 0; while (k <= p.deg && p[k]==0) k++; if (k > p.deg) { pout << 0; return pout;} if (k == 0) pout << p[k]; else { if (p[k] == 1) pout <<"x"; else if (p[k] == -1) pout <<"-x"; else pout << p[k] << "x"; if (k > 1) pout <<"^"<< k; } while (++k <= p.deg) if (p[k] > 0) { pout << " + "; if (p[k] != 1) pout << p[k]; pout <<"x";if (k > 1) pout <<"^"<< k; }

else if (p[k] < 0) { pout << " - "; if (p[k] != -1) pout << -p[k]; pout <<"x"; if (k > 1) pout <<"^"<< k; } return pout;

istream & operator >> (istream & pin, poly &p) { do { cout << "Bac cua da thuc: "; pin >> p.deg; } while (p.deg < 0); p.coef = new double[p.deg+1]; cout << "Nhap lien tiep "<< p.deg+1 << " he so tu bac 0 den bac "<<p.deg <<": "; for (int k = 0; k <= p.deg; k++) pin >> p[k]; while (p[p.deg] == 0) p.deg--; return pin; } void main() { poly a, b; cout <<"\nNhap da thuc thu nhat\n"; cin >> a; cout <<"\nNhap da thuc thu hai\n"; cin >> b; double x;cout <<"\nNhap mot so thuc x = "; cin >> x; cout << "\nKet qua cac phep toan:\n"; cout << "\n("<< a << ") + (" << b << ") = " << a+b << endl; cout << "\n("<< a << ") - (" << b << ") = " << a-b << endl; cout << "\n("<< a << ") * (" << b << ") = " << a*b << endl; cout << "\n("<< a << ") / (" << b << ") = " << a/b << endl; cout << "\n("<< a << ") % (" << b << ") = " << a%b << endl; cout << "\n("<<a << ") + (" << x << ") = " << a+x << endl; cout << "\n("<<a << ") - (" << x << ") = " << a-x << endl; cout << "\n("<<a << ") * (" << x << ") = " << a*x << endl; cout << "\n("<<a << ") / (" << x << ") = " << a/x << endl; cout << "\nGia tri cua " << a << " tai x = " << x <<" bang "<<a.value(x)<< endl; }

ket qua
Nhap da thuc thu nhat Bac cua da thuc: 3 Nhap lien tiep 4 he so tu bac 0 den bac 3: -1 0 0 1 Nhap da thuc thu hai Bac cua da thuc: 2 Nhap lien tiep 3 he so tu bac 0 den bac 2: 1 1 1 Nhap mot so thuc x = 2

Ket qua cac phep toan: (-1 + x^3) + (1 + x + x^2) = x + x^2 + x^3 (-1 + x^3) - (1 + x + x^2) = -2 - x - x^2 + x^3 (-1 + x^3) * (1 + x + x^2) = -1 - x - x^2 + x^3 + x^4 + x^5 (-1 + x^3) / (1 + x + x^2) = -1 + x (-1 + x^3) % (1 + x + x^2) = 0 (-1 + x^3) + (2) = 1 + x^3 (-1 + x^3) - (2) = -3 + x^3 (-1 + x^3) * (2) = -2 + 2x^3 (-1 + x^3) / (2) = -0.5 + 0.5x^3 Gia tri cua -1 + x^3 tai x = 2 bang 7

Вам также может понравиться