Advertisement
didiabd

Exercice Polynome

May 23rd, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. public class Polynome{
  2.    
  3.      private double coef[];
  4.  
  5.      private int degre ;
  6.      // degre = (coef.length -1)
  7.      public  Polynome(int d , double [] tab){
  8.                      
  9.               degre=d;
  10.               coef=tab;
  11.           }
  12.          
  13.      public Polynome Ajouter(Polynome P){
  14.          
  15.         if((P.coef).length==(this.coef).length){
  16.              for(int i=0;i<(P.coef).length;i++){
  17.                  P.coef[i] = P.coef[i]+this.coef[i];
  18.              }
  19.          }
  20.          else {
  21.              if((P.coef).length<(this.coef).length){
  22.                  for(int i=0;i<(P.coef).length;i++){
  23.                     this.coef[i]=P.coef[i]+this.coef[i];
  24.                  }
  25.              }
  26.              else{
  27.                  for(int i=0;i<(this.coef).length;i++){
  28.                     P.coef[i]=P.coef[i]+this.coef[i];
  29.                  }
  30.                  
  31.              }
  32.          }
  33.         int d;
  34.         if((P.degre)>this.degre){
  35.             d=P.degre;
  36.         }
  37.         else{
  38.             d=this.degre;
  39.         }
  40.         double tab[]=new double[(P.coef).length+(this.coef).length];
  41.         if((P.coef).length>=(this.coef).length){
  42.             tab=P.coef;
  43.         }
  44.         else{
  45.             tab=this.coef;
  46.         }
  47.         Polynome A= new Polynome(d,tab);
  48.         System.out.println(A);
  49.         return A;
  50.  
  51.      }
  52.  
  53.      public double Evaluer(double x){
  54.          double a=coef[0]+x*coef[1];
  55.          double sum=0 ;
  56.          for(int i=2;i<coef.length;i++){
  57.              
  58.              sum=sum+((Math.pow(x,i))*coef[i]);
  59.              
  60.          }
  61.          
  62.          sum=a+sum;
  63.          System.out.println(sum);
  64.          return sum;
  65.          
  66.          
  67.           }
  68.    
  69.     public String toString(){
  70.         String s="";
  71.         for (int i=1;i<coef.length;i++){
  72.             s+=" + "+coef[i]+"X"+"^"+i;
  73.         }
  74.         return coef[0]+" "+s;
  75.  
  76.     }
  77.  
  78. }
  79. public class Test{
  80.  
  81.  
  82.     public static void main(String [] args){
  83.  
  84.      double x[]={5,2,2,7,7,1};
  85.      double x1[] ={1,3,6,5};
  86.         Polynome P1 = new Polynome(6,x);
  87.  
  88.        Polynome P2 = new Polynome( 5,x1);
  89.  
  90.        System.out.println(P1);
  91.        System.out.println(P2);
  92.        System.out.print("Evaluer x dans P1 :");
  93.        P1.Evaluer(5);
  94.        System.out.print("Evaluer x dans P2 :");
  95.        P2.Evaluer(6);    
  96.        P2.Ajouter(P1);
  97.        
  98.        
  99.        
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement