Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"Poly.h"
- #include<iostream>
- #include<algorithm>
- using namespace std;
- Poly::Poly()
- {
- for (int x = 0; x < 11; x++)
- {
- coeff[x] = 0;
- }
- degree = 0;
- }
- Poly::Poly(int c[], int d)
- {
- for (int x = 0; x < 11; x++)
- {
- coeff[x] = c[x];
- }
- degree = (d >= 0 ? d : 0);
- }
- ostream &operator<<(ostream &output, Poly &p)
- {
- output << p.coeff[p.degree] << "x^" << p.degree;
- for (int x = (p.degree - 1); x >= 0; x--)
- if (x == 0)
- {
- if (p.coeff[x] < 0)
- {
- output << " - " << abs(p.coeff[x]);
- }
- else
- {
- output << " + " << p.coeff[x];
- }
- }
- else if (p.coeff[x] < 0)
- {
- output << " - " << abs(p.coeff[x]) << "x^" << x;
- }
- else
- {
- output << " + " << p.coeff[x] << "x^" << x;
- }
- return output;
- }
- Poly Poly::operator+(Poly &p)
- {
- const int deg = max(degree, p.degree);
- int sumCoeff[11] = { 0 };
- for (int x = 0; x < 11; x++)
- {
- sumCoeff[x] = coeff[x] + p.coeff[x];
- }
- return Poly(sumCoeff, deg);
- }
- Poly Poly::operator-(Poly &p)
- {
- const int deg = max(degree, p.degree);
- int sumCoeff[11] = { 0 };
- for (int x = 0; x < 11; x++)
- {
- sumCoeff[x] = coeff[x] - p.coeff[x];
- }
- return Poly(sumCoeff, deg);
- }
- Poly Poly::operator*(Poly &p)
- {
- int poly[11];
- for (int x = 0; x < 11; x++)
- {
- poly[x] = 0;
- }
- for (int x = degree; x >= 0; x--)
- {
- for (int y = p.degree; y >= 0; y--)
- {
- poly[x + y] += coeff[x] * p.coeff[y];
- }
- }
- for (int z = 10; z > 0; z--)
- {
- if (poly[z] != 0)
- {
- return Poly(poly, z);
- }
- }
- }
- Poly Poly::operator=(Poly &p)
- {
- const int deg = max(degree, p.degree);
- int sumCoeff[11] = { 0 };
- for (int x = 0; x < 11; x++)
- {
- sumCoeff[x] = coeff[x] = p.coeff[x];
- }
- return Poly(sumCoeff, deg);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement