Advertisement
jovanovski

НП ЛБ1

Oct 15th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. package com.gorjan.lab1;
  2.  
  3. public class PolynomTest {
  4.  
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.        
  10.        
  11.         Polynomal pol1 = new Polynomal(new Term [] {new Term(3,4), new Term(2,2), new Term(4,1), new Term(8,0)});
  12.         Polynomal pol2 = new Polynomal(new Term [] {new Term(3,2), new Term(8,0)});
  13.        
  14.         Polynomal rez = pol1.add(pol2);
  15.        
  16.        
  17.         System.out.println(rez);
  18.     }
  19.    
  20.  
  21. }
  22.  
  23. final class Term {
  24.     final double c;
  25.     final int e;
  26.  
  27.     public Term(double c, int e) {
  28.         this.c = c;
  29.         this.e = e;
  30.     }
  31.  
  32.     public double getCoeficient() {
  33.         return c;
  34.     }
  35.  
  36.     public int getExponent() {
  37.         return e;
  38.     }
  39.  
  40.     public Term negate(){
  41.         Term tmp = new Term(this.c*(-1), this.e);
  42.         return tmp;
  43.     }
  44.    
  45.     public Term add(Term t){
  46.         Term tmp;
  47.         double c_t;
  48.        
  49.         if(this.e == t.e){
  50.             c_t = this.c + t.c;
  51.             tmp = new Term(c_t, this.e);
  52.             return tmp;
  53.         }
  54.         else return null;
  55.        
  56.     }
  57.    
  58.     @Override
  59.     public String toString() {
  60.         return c+"x^"+e;
  61.     }
  62.  
  63.     public Term multiply(Term t){
  64.         Term tmp;
  65.         double c_t;
  66.         int e_t;
  67.            
  68.         c_t = this.c * t.c;
  69.         e_t = this.e + t.e;
  70.         tmp = new Term(c_t, e_t);
  71.         return tmp;
  72.        
  73.     }
  74.    
  75.     public double evaluate(double x){
  76.         return this.c * Math.pow(x, this.e);
  77.     }
  78.    
  79.     public boolean equals(Object obj){
  80.         Term tmp = (Term) obj;
  81.         return((this.e == tmp.e)&&(this.c == tmp.c));
  82.     }
  83.    
  84.     public int compareTo(Term t){
  85.         if(this.e<t.e) return -1;
  86.         else if(this.e==t.e) return 0;
  87.         else return 1;
  88.     }
  89.  
  90. }
  91.  
  92. final class Polynomal {
  93.     Term [] terms;
  94.    
  95.     public Polynomal(Term []terms){
  96.         this.terms = new Term[terms.length];
  97.         for(int i = 0; i < terms.length ; i++){
  98.             this.terms[i] = terms[i];          
  99.         }
  100.     }
  101.    
  102.     public Term getTerm(int exp){
  103.        
  104.         for(int i = 0; i < this.terms.length ; i++){
  105.             if(terms[i].e == exp) return terms[i];
  106.            
  107.         }
  108.        
  109.         return null;
  110.     }
  111.    
  112.     public Polynomal negate(){
  113.         Polynomal tmp = new Polynomal(this.terms);
  114.         for(int i = 0; i < this.terms.length ; i++){
  115.             tmp.terms[i] = tmp.terms[i].negate();
  116.         }
  117.         return tmp;
  118.     }
  119.  
  120.     public Polynomal add(Polynomal t){;
  121.        
  122.         int cur1=0, cur2=0, i=0;
  123.         Polynomal tmp = new Polynomal(new Term[100]);
  124.        
  125.         while((cur1<=this.terms.length- 1)||(cur2<=t.terms.length-1)){
  126.             if(this.terms[cur1].e == t.terms[cur2].e){
  127.                 tmp.terms[i] = new Term(this.terms[cur1].c + t.terms[cur2].c,this.terms[cur1].e);
  128.                 cur1++;
  129.                 cur2++;
  130.                 System.out.println("Prv");
  131.             }
  132.             else if(this.terms[cur1].e > t.terms[cur2].e){
  133.                 tmp.terms[i] = new Term(this.terms[cur1].c,this.terms[cur1].e);
  134.                 cur1++;
  135.                 System.out.println("Vtor");
  136.             }
  137.             else if(this.terms[cur1].e < t.terms[cur2].e){
  138.                 tmp.terms[i] = new Term(t.terms[cur2].c,t.terms[cur2].e);
  139.                 cur2++;
  140.                 System.out.println("Tret");
  141.             }
  142.            
  143.             i++;
  144.            
  145.             if((cur1 == this.terms.length)||(cur2 == t.terms.length)) break;
  146.            
  147.            
  148.         }
  149.             System.out.println("Krajno Cur1: "+cur1+" Cur2: "+cur2);
  150.            
  151.             while(cur1 < this.terms.length){
  152.                 tmp.terms[i] = new Term(this.terms[cur1].c, this.terms[cur1].e);
  153.                 cur1++;
  154.                 i++;
  155.                 System.out.println("Cur1: "+cur1+" Cur2: "+cur2);
  156.             }
  157.            
  158.             while(cur2 < t.terms.length){
  159.                 tmp.terms[i] = new Term(t.terms[cur2].c, t.terms[cur2].e);
  160.                 cur2++;
  161.                 i++;
  162.  
  163.             }
  164.            
  165.         Polynomal nova = new Polynomal(new Term[i]);
  166.        
  167.         for(int n = 0; n < i ; n++){
  168.                 nova.terms[n] = tmp.terms[n];
  169.             }
  170.        
  171.        
  172.         return nova;
  173.        
  174.     }
  175.  
  176.     @Override
  177.     public String toString(){
  178.         String a = "";
  179.         System.out.println("A: "+ this.terms.length);
  180.         for(int i = 0; i < this.terms.length;i++){
  181.             a += this.terms[i].toString() + " + ";
  182.         }
  183.         return a;
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement