class Polynome {
// polynomes a coefficients entiers
// par puissances croissantes
int val;
Polynome suite;
Polynome(int x) {
val=x;
suite= null;
}
}
int degre() {
if (suite== null)
return 0;
else
return 1+ suite.degre();
}
Si p représente le polynôme 1+x2, alors
p.degre()vaut 2.
toString
public String toString() {
String s=""+ val;
int i=1;
Polynome p= suite;
while (p != null) {
s=s+" + "+ p.val+"x^"+ i;
i++;
p=p.suite;
}
return s;
}
Si p représente 1+x2, alors
System.out.println("p= "+ p);
produit
p= 1 + 0x^1 + 1x^2
static Polynome plus(Polynome p, Polynome q) {
Polynome r=new Polynome(p.val+q.val);
if (p.suite == null)
r.suite= q.suite;
else if (q.suite == null)
r.suite = p.suite;
else
r.suite= plus(p.suite, q.suite);
return r;
}
La multiplication par une constante :
static Polynome mult(int x,Polynome p) {
Polynome r= new Polynome(x*p.val);
if (p.suite != null)
r.suite=mult(x,p.suite);
return r;
}
permet d'écrire le produit
static Polynome mult(Polynome p, Polynome q) {
Polynome r= mult(p.val,q);
Polynome s=new Polynome(0);
if (p.suite != null)
s.suite=mult(p.suite,q);
return plus(r,s);
}