1+ //NOT MINE
2+ public class Polynomial {
3+
4+ private int [] cof ;
5+ private int deg ;
6+
7+ public Polynomial () {
8+ cof =new int [5 ];
9+ deg =-1 ;
10+ }
11+ public void setCoefficient (int deg ,int coficient ) {
12+ if (deg >=cof .length ) {
13+ restructure (deg );
14+ }
15+ cof [deg ]=coficient ;
16+ if (deg >= this .deg ) {
17+ this .deg =deg ;
18+ }
19+ }
20+ public Polynomial add (Polynomial p ) {
21+
22+ Polynomial x =new Polynomial ();
23+
24+ int i =0 ,j =0 ,k =0 ;
25+ while (i < p .cof .length && j < this .cof .length ) {
26+ x .setCoefficient (k , p .cof [i ] + this .cof [i ]);
27+ i ++;
28+ j ++;
29+ k ++;
30+ }
31+ while (i <p .cof .length ) {
32+ x .setCoefficient (k , p .cof [i ]);
33+ k ++;
34+ i ++;
35+ }
36+ while (j <this .cof .length ) {
37+ x .setCoefficient (k , this .cof [j ]);
38+ k ++;
39+ j ++;
40+ }
41+
42+
43+ return x ;
44+
45+ }
46+ public Polynomial subtract (Polynomial p ) {
47+
48+ Polynomial x =new Polynomial ();
49+
50+ int i =0 ,j =0 ,k =0 ;
51+ while (i < p .cof .length && j < this .cof .length ) {
52+ x .setCoefficient (k , this .cof [i ] - p .cof [i ] );
53+ i ++;
54+ j ++;
55+ k ++;
56+ }
57+ while (i <p .cof .length ) {
58+ x .setCoefficient (k , -p .cof [i ]);
59+ k ++;
60+ i ++;
61+ }
62+ while (j <this .cof .length ) {
63+ x .setCoefficient (k , this .cof [j ]);
64+ k ++;
65+ j ++;
66+ }
67+
68+
69+ return x ;
70+
71+ }
72+ public Polynomial multiply (Polynomial p ) {
73+
74+ Polynomial x =new Polynomial ();
75+
76+ for (int i =0 ;i <p .cof .length ;i ++) {
77+ for (int j =0 ;j <this .cof .length ;j ++) {
78+ if (i + j <=x .deg )
79+ x .setCoefficient ( i + j ,x .cof [i +j ]+p .cof [i ] * this .cof [j ]);
80+ else
81+ x .setCoefficient (i + j ,p .cof [i ] * this .cof [j ]);
82+ }
83+ }
84+ return x ;
85+
86+ }
87+ public void print () {
88+ for (int i =0 ;i <cof .length ;i ++) {
89+ if (cof [i ] !=0 )
90+ System .out .print ( cof [i ] +"x" + i +" " );
91+ }
92+ }
93+ private void restructure (int deg ) {
94+
95+ int [] temp =cof ;
96+ cof =new int [deg + 1 ];
97+ for (int i =0 ;i <temp .length ;i ++) {
98+ cof [i ]=temp [i ];
99+ }
100+ }
101+
102+
103+
104+
105+ }
0 commit comments