• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Implentation a guest Mar 25th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include"Poly.h"
2. #include<iostream>
3. #include<algorithm>
4. using namespace std;
5.
6. Poly::Poly()
7. {
8.     for (int x = 0; x < 11; x++)
9.     {
10.         coeff[x] = 0;
11.     }
12.     degree = 0;
13. }
14. Poly::Poly(int c[], int d)
15. {
16.     for (int x = 0; x < 11; x++)
17.     {
18.         coeff[x] = c[x];
19.     }
20.     degree = (d >= 0 ? d : 0);
21. }
22.
23. ostream &operator<<(ostream &output, Poly &p)
24. {
25.     output << p.coeff[p.degree] << "x^" << p.degree;
26.
27.     for (int x = (p.degree - 1); x >= 0; x--)
28.         if (x == 0)
29.         {
30.             if (p.coeff[x] < 0)
31.             {
32.                 output << " - " << abs(p.coeff[x]);
33.             }
34.             else
35.             {
36.                 output << " + " << p.coeff[x];
37.             }
38.         }
39.         else if (p.coeff[x] < 0)
40.         {
41.             output << " - " << abs(p.coeff[x]) << "x^" << x;
42.         }
43.         else
44.         {
45.             output << " + " << p.coeff[x] << "x^" << x;
46.         }
47.     return output;
48. }
49.
50. Poly Poly::operator+(Poly &p)
51. {
52.     const int deg = max(degree, p.degree);
53.     int sumCoeff = { 0 };
54.
55.     for (int x = 0; x < 11; x++)
56.     {
57.         sumCoeff[x] = coeff[x] + p.coeff[x];
58.     }
59.     return Poly(sumCoeff, deg);
60. }
61.
62. Poly Poly::operator-(Poly &p)
63. {
64.     const int deg = max(degree, p.degree);
65.     int sumCoeff = { 0 };
66.
67.     for (int x = 0; x < 11; x++)
68.     {
69.         sumCoeff[x] = coeff[x] - p.coeff[x];
70.     }
71.     return Poly(sumCoeff, deg);
72. }
73.
74. Poly Poly::operator*(Poly &p)
75. {
76.     int poly;
77.     for (int x = 0; x < 11; x++)
78.     {
79.         poly[x] = 0;
80.     }
81.     for (int x = degree; x >= 0; x--)
82.     {
83.         for (int y = p.degree; y >= 0; y--)
84.         {
85.             poly[x + y] += coeff[x] * p.coeff[y];
86.         }
87.     }
88.     for (int z = 10; z > 0; z--)
89.     {
90.         if (poly[z] != 0)
91.         {
92.             return Poly(poly, z);
93.         }
94.     }
95. }
96.
97. Poly Poly::operator=(Poly &p)
98. {
99.     const int deg = max(degree, p.degree);
100.     int sumCoeff = { 0 };
101.
102.     for (int x = 0; x < 11; x++)
103.     {
104.         sumCoeff[x] = coeff[x] = p.coeff[x];
105.     }
106.     return Poly(sumCoeff, deg);
107. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top