Delete TEST2024/Polynome.java
This commit is contained in:
parent
8eb702b6e0
commit
e91b74e144
@ -1,68 +0,0 @@
|
|||||||
/**
|
|
||||||
* Classe modelisant un polynome à coefficients entiers
|
|
||||||
* Un polynome est caracterise par la suite de ses coefficients
|
|
||||||
* Ils sont stockes dans un tableau d'entiers, coefficient de poids faible en premier
|
|
||||||
* ex: 3x^2+5x+1 est represente par le tableau [1,5,3]
|
|
||||||
* Le premier coefficient ne peut pas etre nul
|
|
||||||
*/
|
|
||||||
public class Polynome{
|
|
||||||
//la liste des coefficients de notre polynome
|
|
||||||
int[] coefficients;
|
|
||||||
//le degre du polynome;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*le constructeur
|
|
||||||
*@param t un tableau d'entiers
|
|
||||||
*@throws IllegalArgumentException si le premier coefficient de t est nul
|
|
||||||
*/
|
|
||||||
public Polynome(int[] t){
|
|
||||||
coefficients=new int[t.length];
|
|
||||||
for(int i=0;i<t.length;i++){
|
|
||||||
coefficients[i]=t[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//renvoie le degre du polynome
|
|
||||||
//le degré du polynome nul est -1
|
|
||||||
public int degre(){
|
|
||||||
return coefficients.length-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*evalue le polynome sur un entier donne
|
|
||||||
*@param x l'entier
|
|
||||||
*@return int p(x)
|
|
||||||
*/
|
|
||||||
public int evaluer(int x){
|
|
||||||
int p=x;
|
|
||||||
int r=0;
|
|
||||||
for(int i=this.degre();i>=0;i--){
|
|
||||||
r+=coefficients[i]*p;
|
|
||||||
p*=x;
|
|
||||||
}
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*additionne deux polynome, renvoie le resultat dans un nouveau polynome
|
|
||||||
*@param p un polynome
|
|
||||||
*@return un polynome
|
|
||||||
*/
|
|
||||||
public Polynome add(Polynome p){
|
|
||||||
int d=this.degre()+1;
|
|
||||||
int[] c=new int[d];
|
|
||||||
for(int i=0;i<d;i++){
|
|
||||||
c[i]+=this.coefficients[i]+p.coefficients[i];
|
|
||||||
}
|
|
||||||
return new Polynome(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString(){
|
|
||||||
if(this.degre()==-1) return 0+"";
|
|
||||||
String s=""+coefficients[0];
|
|
||||||
for(int i=1;i<=this.degre();i++){
|
|
||||||
s+=coefficients[i]+"x^"+i+"+";
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user