2013-11-14 3 views
0

이것이 제가 말한 것입니다. 선언 세 가지 클래스 : 홈페이지 기간 Polynom 그래서 Polynom에서 나는 "플러스"메서드를 호출하려고ArrayList를 사용하여 두 개의 Polynoms (plus) 추가하기 <Term>

:

난이 시도 :

public Polynom add(Polynom pol){ 

    Polynom res = new Polynom("",0); 
    Term tox = new Term(0,0); 

    for(Term p : Polynom){ 


      for(Term other : pol.Polynom){ 
       if(p.getDeg()==other.getDeg()){ 
        tox.setCoef(p.getCoef()+ other.getCoef()); 
        tox.setDeg(p.getDeg()); 
     } 
       res.addTerm(tox); 
      } 


    } 
    return res; 
} 

그리고 나는이 문제에 직면 오전 : I 이 객체와이 객체가 다른 객체에 들어가는 것을 알 수 있습니다. 그래서 두 개의 폴리곤을 추가하려고하면 잘못된 결과가 나옵니다.

두 개의 Polynoms를 추가하는 좋은 방법이 있습니까?

private ArrayList<Term> Polynom = new ArrayList<Term>(); 
private String name; 
private double number; 

용어는 다음과 같습니다 그들의 지수가 같은 경우

public class Term { 
private int deg; 
private double coef; 
} 

답변

0

당신은 두 Term 인스턴스를 추가 할 수 있습니다.

MonomialPolynomial의 공통 인터페이스 Term의 두 클래스가 있습니다. 인터페이스에 add 메서드가 있습니다.

복합 패턴이라고합니다. 이 같은

뭔가 :

IMonomial :

package poly; 

import java.util.Iterator; 

/** 
* IMonomial 
* @author Michael 
* @since 5/27/11 
*/ 
public interface IMonomial { 
    double TOLERANCE = 1.0E-4; 

    boolean hasChildren(); 

    int getNumTerms(); 

    double getCoeff(); 

    int getExpon(); 

    Iterator<IMonomial> iterator(); 

    IMonomial add(IMonomial other); 

    IMonomial sub(IMonomial other); 

    IMonomial mul(IMonomial other); 

    IMonomial div(IMonomial other); 

    IMonomial integrate(); 

    IMonomial differentiate(); 

    double evaluate(double value); 
} 

단항식 :

package poly; 

import java.util.Iterator; 

public class Monomial implements IMonomial { 
    private final double coeff; 
    private final int expon; 

    public Monomial() { 
     this(0.0, 0); 
    } 

    public Monomial(double coeff) { 
     this(coeff, 0); 
    } 

    public Monomial(double coeff, int expon) { 
     this.coeff = coeff; 
     this.expon = expon; 
    } 

    public boolean hasChildren() { 
     return false; 
    } 

    public int getNumTerms() { 
     return 1; 
    } 

    public double getCoeff() { 
     return this.coeff; 
    } 

    public int getExpon() { 
     return expon; 
    } 

    public Iterator<IMonomial> iterator() { 
     return new Iterator<IMonomial>() { 

      private boolean hasNext = true; 

      public boolean hasNext() { 
       return hasNext; 
      } 

      public IMonomial next() { 
       hasNext = false; 
       return new Monomial(getCoeff(), getExpon()); 
      } 

      public void remove() { 
       throw new UnsupportedOperationException("remove() is not implemented"); 
      } 
     }; 
    } 

    public IMonomial add(IMonomial other) { 
     if (this.expon != other.getExpon()) 
      throw new IllegalArgumentException("Exponents must match in order to add"); 

     return new Monomial((this.getCoeff() + other.getCoeff()), this.expon); 
    } 

    public IMonomial sub(IMonomial other) { 
     if (this.expon != other.getExpon()) 
      throw new IllegalArgumentException("Exponents must match in order to subtract"); 

     return new Monomial((this.getCoeff() - other.getCoeff()), this.expon); 
    } 

    public IMonomial mul(IMonomial other) { 
     return new Monomial((this.getCoeff() * other.getCoeff()), (this.expon + other.getExpon())); 
    } 

    public IMonomial div(IMonomial other) { 
     return new Monomial((this.getCoeff()/other.getCoeff()), (this.expon - other.getExpon())); 
    } 

    public IMonomial integrate() { 
     if (Math.abs(this.getCoeff()) < IMonomial.TOLERANCE) return new Monomial(1.0); 
     else return new Monomial(this.getCoeff()/(this.getExpon() + 1.0), (this.getExpon() + 1)); 
    } 

    public IMonomial differentiate() { 
     return ((this.getExpon() == 0) ? new Monomial() : new Monomial(this.getCoeff() * this.getExpon(), (this.getExpon() - 1))); 
    } 

    public double evaluate(double value) { 
     return this.coeff * Math.pow(value, this.expon); 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) { 
      return true; 
     } 
     if (o == null || getClass() != o.getClass()) { 
      return false; 
     } 

     Monomial monomial = (Monomial) o; 

     if (Double.compare(monomial.getCoeff(), getCoeff()) != 0) { 
      return false; 
     } 
     if (expon != monomial.expon) { 
      return false; 
     } 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     int result; 
     long temp; 
     temp = getCoeff() != +0.0d ? Double.doubleToLongBits(getCoeff()) : 0L; 
     result = (int) (temp^(temp >>> 32)); 
     result = 31 * result + expon; 
     return result; 
    } 

    @Override 
    public String toString() { 
     final StringBuilder sb = new StringBuilder(); 
     if (getCoeff() > 0.0) sb.append('+'); 
     sb.append(getCoeff()); 
     if (expon != 0) { 
      if (Math.abs(getCoeff()) > IMonomial.TOLERANCE) sb.append('x'); 
      if (expon != 1) sb.append('^').append(expon); 
     } 
     return sb.toString(); 
    } 
} 

다항식 :

package poly; 

import java.util.Collections; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Map; 

/** 
* Polynomial 
* @author Michael 
* @since 5/27/11 
*/ 
public class Polynomial implements IMonomial { 
    private Map<Integer, IMonomial> terms; 

    public Polynomial() { 
     init(); 
    } 

    private void init() { 
     this.terms = new HashMap<Integer, IMonomial>(); 
    } 

    public Polynomial(IMonomial other) { 
     init(); 

     if (other.hasChildren()) { 
      Polynomial p = (Polynomial) other; 
      for (IMonomial term : p.terms.values()) { 
       this.terms.put(term.getExpon(), term); 
      } 
     } else { 
      this.terms.put(other.getExpon(), other); 
     } 
    } 

    public boolean hasChildren() { 
     return (this.terms.size() > 0); 
    } 

    public int getNumTerms() { 
     return this.terms.size(); 
    } 

    public double getCoeff() { 
     return ((this.terms.size() == 0) ? 0.0 : this.terms.get(Collections.max(this.terms.keySet())).getCoeff()); 
    } 

    public int getExpon() { 
     return ((this.terms.size() == 0) ? 0 : this.terms.get(Collections.max(this.terms.keySet())).getExpon()); 
    } 

    public Iterator<IMonomial> iterator() { 
     return this.terms.values().iterator(); 
    } 

    public IMonomial add(IMonomial other) { 
     if (other.hasChildren()) { 
      Iterator<IMonomial> iterator = this.terms.values().iterator(); 
      while (iterator.hasNext()) { 
       IMonomial next = iterator.next(); 
       this.add(next); 
      } 
     } else { 
      IMonomial term = this.terms.get(other.getExpon()); 

      if (term != null) { 

       this.terms.put(other.getExpon(), term.add(other)); 
      } else { 
       this.terms.put(other.getExpon(), other); 
      } 
     } 

     return new Polynomial(this); 
    } 

    public IMonomial sub(IMonomial other) { 
     if (other.hasChildren()) { 
      Iterator<IMonomial> iterator = this.terms.values().iterator(); 
      while (iterator.hasNext()) { 
       IMonomial next = iterator.next(); 
       this.sub(next); 
      } 
     } else { 
      IMonomial term = this.terms.get(other.getExpon()); 

      if (term != null) { 

       this.terms.put(other.getExpon(), term.sub(other)); 
      } else { 
       this.terms.put(other.getExpon(), new Monomial(-other.getCoeff(), other.getExpon())); 
      } 
     } 

     return new Polynomial(this); 
    } 

    public IMonomial mul(IMonomial other) { 
     Polynomial value = new Polynomial(); 

     for (IMonomial term : terms.values()) { 
      value.add(term.mul(other)); 
     } 

     return value; 
    } 

    public IMonomial div(IMonomial other) { 
     Polynomial value = new Polynomial(); 

     for (IMonomial term : terms.values()) { 
      value.add(term.div(other)); 
     } 

     return value; 
    } 

    public IMonomial integrate() { 
     Polynomial value = new Polynomial(); 

     for (IMonomial term : terms.values()) { 
      value.add(term.integrate()); 
     } 

     return value; 
    } 

    public IMonomial differentiate() { 
     Polynomial value = new Polynomial(); 

     for (IMonomial term : terms.values()) { 
      value.add(term.differentiate()); 
     } 

     return value; 
    } 

    public double evaluate(double value) { 
     double sum = 0.0; 

     for (IMonomial term : this.terms.values()) { 
      sum += term.evaluate(value); 
     } 

     return sum; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) { 
      return true; 
     } 
     if (o == null || getClass() != o.getClass()) { 
      return false; 
     } 

     Polynomial that = (Polynomial) o; 

     if (terms != null ? !terms.equals(that.terms) : that.terms != null) { 
      return false; 
     } 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     return terms != null ? terms.hashCode() : 0; 
    } 

    @Override 
    public String toString() { 
     final StringBuilder sb = new StringBuilder(); 
     for (IMonomial term : this.terms.values()) { 
      sb.append(term); 
     } 
     return sb.toString(); 
    } 
} 
0

에만 대신, 한 번 새로운 용어를 만드는 하나는 전자 결과 다항식에서 매우 정도. 당신이 것을 해결하면 상황은 불행하게도,이는 원래 다항식의 용어와 동일한 학위를 가지고 다른 다항식에서 조건을 추가 할 것입니다,

Polynom res = new Polynom("",0); 

for(Term p : Polynom){ 
    Term tox = new Term(0,0); // Here's the change 

    for(Term other : pol.Polynom){ 
    if(p.getDeg()==other.getDeg()){ 
     tox.setCoef(p.getCoef()+ other.getCoef()); 
     tox.setDeg(p.getDeg()); 
    } 
    res.addTerm(tox); 
    } 
} 

뭔가를 더 얻을 것이다. 결과는 아마도 누락 된 용어 일 것입니다. 당신은 아래의 골격의 라인을 따라 뭔가를 수행하여 그들 모두를 얻을 수 있습니다 : 당신이에서 시작하는 곳을 제공

List<Term> sorted1; 
List<Term> sorted2; // Sort both lists of Terms in ascending order of degree 

while(sorted1.size() > 0 || sorted2.size() > 0) { 
    if(sorted1.size() == 0) { 
    // Make a new term from the last element of sorted2 
    sorted2.remove(sorted2.size() - 1); 
    } 
    else if(sorted2.size() == 0) { 
    // Make a new term from the last element of sorted1 
    sorted1.remove(sorted1.size() - 1); 
    else { 
    // Figure out whether the terms have the same degree or not - if they do, 
    // add them together into one term. Otherwise, choose the one with the larger 
    // degree. 
    // ... 
    } 
} 

희망을.

관련 문제