import java.util.*;
import java.lang.*;
import java.io.*;
class Polynomial{
int N;
double [] a = new double [N+1];
public void printPolynomial(){
if (this.N > 0){
for (int i = N; i >= 0; i = i -1){
if (i
!=0){ if (this.
a[i
]!= 0) System.
out.
println("("+this.
a[i
]+")*X^"+i
+"+");} if (i
==0){ if (this.
a[i
]!= 0) System.
out.
println("("+this.
a[i
]+")");} }
}
if (this.
N == 0) System.
out.
println(+this.
a[0]); }
public void computation(double k){
double s = 0;
if (this.N > 0){
for (int i = 0; i<= N; i++){
s
= s
+ this.
a[i
]*Math.
pow(k,i
); }
System.
out.
println("Значение равно "+s
); }
if (this.
N == 0) System.
out.
println("Значение равно "+this.
a[0]); }
public void sum(Polynomial cir){
double s;
for (int i = cir.N; i >= 0; i = i -1){
if (cir.N >= this.N){
s = 0;
if (i
> this.
N){ if (cir.
a[i
] != 0) System.
out.
println("("+cir.
a[i
]+")*X^"+i
+"+" );} if ((i
<= this.
N) && (i
!= 0)){ s
= this.
a[i
] + cir.
a[i
]; if (s
!= 0) System.
out.
println("("+s
+")*X^"+i
+"+");} if (i
== 0){ s
= this.
a[i
] + cir.
a[i
]; System.
out.
println(+s
);} }
if (cir.N < this.N){
s = 0;
if (i
> cir.
N){ if (this.
a[i
] != 0) System.
out.
println(+this.
a[i
]+"*X^"+i
+"+" );} if ((i
<= cir.
N) && (i
!= 0)){ s
= this.
a[i
] + cir.
a[i
]; if (s
!= 0)System.
out.
println(+s
+"*X^"+i
+"+");} if (i
== 0){ s
= this.
a[i
] + cir.
a[i
]; System.
out.
println(+s
);} }
}
}
public void difference(Polynomial cir){
double s;
for (int i = cir.N; i >= 0; i = i -1){
if (cir.N >= this.N){
s = 0;
if (i
> this.
N){ if (cir.
a[i
] != 0) System.
out.
println("("+cir.
a[i
]+")*X^"+i
+"+" );} if ((i
<= this.
N) && (i
!= 0)){ s
= this.
a[i
] - cir.
a[i
]; if (s
!= 0) System.
out.
println("("+s
+")*X^"+i
+"+");} if (i
== 0){ s
= this.
a[i
] - cir.
a[i
]; System.
out.
println(+s
);} }
if (cir.N < this.N){
s = 0;
if (i
> cir.
N){ if (this.
a[i
] != 0) System.
out.
println(+this.
a[i
]+"*X^"+i
+"+" );} if ((i
<= cir.
N) && (i
!= 0)){ s
= this.
a[i
] - cir.
a[i
]; if (s
!= 0) System.
out.
println(+s
+"*X^"+i
+"+");} if (i
== 0){ s
= this.
a[i
] - cir.
a[i
]; System.
out.
println(+s
);} }
}
}
public void multiplication(Polynomial cir){
double [] g = new double [cir.N + this.N + 1];
double y;
for (int i = 0; i <= cir.N; i++){
y = 1;
for (int j = 0; j <= this.N; j++){
y = cir.a[i] * this.a[j];
g[i + j] = g[i + j] + y;
}
}
for (int i = cir.N + this.N; i >= 0; i = i -1){
if (cir.N + this.N != 0){
if (i
!= 0) if (g
[i
] != 0) System.
out.
println("("+g
[i
]+")*X^+"); if (i
== 0) if (g
[i
] != 0) System.
out.
println("("+g
[i
]+")"); }
if (cir.
N + this.
N == 0) System.
out.
println(+g
[0]); }
}
}
class Ideone
{
{
Polynomial q = new Polynomial ();
Scanner sc
= new Scanner
(System.
in); System.
out.
println("Введите максимальную степень многочлена"); int p = sc.nextInt();
q.N = p;
for (int i = 0; i <= p; i++){
System.
out.
println("Введите коэффициент при X^"+i
); q.a[i] = sc.nextInt();
}
Polynomial t = new Polynomial ();
System.
out.
println("Введите максимальную степень второго многочлена"); int f = sc.nextInt();
t.N = f;
for (int i = 0; i <= f; i++){
System.
out.
println("Введите коэффициент при X^"+i
); t.a[i] = sc.nextInt();
}
System.
out.
println("Введите номер операции"); int m = sc.nextInt();
switch (m){
case 1 : q.printPolynomial();break;
case 2 : System.
out.
println("Введите значение"); double r
= sc.
nextInt(); q.
computation(r
);break; case 3: q.sum(t);break;
case 4: q.difference(t);break;
case 5: q.multiplication(t); break;
}
}
}