簡體   English   中英

多項式相加

[英]Adding polynomials

我需要幫助。 我正在做測試驅動的開發。 這些是測試:

@Test public void add01() throws TError { assertEquals(new Term(10,5),  new Term(4,5).add(new Term(6,5))); }
@Test public void add02() throws TError { assertEquals(new Term(6,5),   new Term(0,5).add(new Term(6,5))); }
@Test public void add03() throws TError { assertEquals(new Term(2,5),   new Term(-4,5).add(new Term(6,5))); }
@Test public void add04() throws TError { assertEquals(new Term(10,0),  new Term(4,0).add(new Term(6,0))); }
@Test public void add05() throws TError { assertEquals(new Term(4,5),   new Term(4,5).add(new Term(0,5))); }
@Test public void add06() throws TError { assertEquals(new Term(-2,5),  new Term(4,5).add(new Term(-6,5))); }

@Test (expected = IncompatibleTerms.class)
public void add07() throws TError { t = new Term(4,5).add(new Term(6,0)); }

@Test (expected = CoefficientOverflow.class)
public void add08() throws TError { t = new Term(min,4).add(new Term(-1,4)); }

@Test public void add09() throws TError { assertEquals(new Term(min,4), new Term(min+1,4).add(new Term(-1,4))); }
@Test public void add10() throws TError { assertEquals(new Term(-11,4), new Term(-10,4).add(new Term(-1,4))); }
@Test public void add11() throws TError { assertEquals(new Term(-2,4),  new Term(-1,4).add(new Term(-1,4))); }
@Test public void add12() throws TError { assertEquals(new Term(-1,4),  new Term(0,4).add(new Term(-1,4))); }
@Test public void add13() throws TError { assertEquals(Term.Zero,       new Term(1,4).add(new Term(-1,4))); }
@Test public void add14() throws TError { assertEquals(new Term(9,4),   new Term(10,4).add(new Term(-1,4))); }
@Test public void add15() throws TError { assertEquals(new Term(max,4), new Term(max-1,4).add(new Term(1,4))); }

@Test (expected = CoefficientOverflow.class)
public void add16() throws TError { t = new Term(max,4).add(new Term(1,4)); }

//Using domain-specific knowledge:  addition should be commutative

@Test public void add17() throws TError { assertEquals(new Term(-1,2).add(new Term(1,2)),       new Term(1,2).add(new Term(-1,2))); }
@Test public void add18() throws TError { assertEquals(new Term(min+1,2).add(new Term(-1,2)),   new Term(-1,2).add(new Term(min+1,2))); }
@Test public void add19() throws TError { assertEquals(new Term(min,2).add(Term.Zero),      Term.Zero.add(new Term(min,2))); }
@Test public void add20() throws TError { assertEquals(new Term(min,0).add(Term.Unit),      Term.Unit.add(new Term(min,0))); }
@Test public void add21() throws TError { assertEquals(new Term(max,0).add(Term.Zero),      Term.Zero.add(new Term(max,0))); }
@Test public void add22() throws TError { assertEquals(new Term(max-1,2).add(new Term(1,2)),    new Term(1,2).add(new Term(max-1,2))); }
@Test public void add23() throws TError { assertEquals(new Term(max,2).add(new Term(min,2)),    new Term(min,2).add(new Term(max,2))); }

到目前為止,這是我在“Andrej Gajduk”的幫助下添加的方法。

public Term add(Term that) throws CoefficientOverflow, IncompatibleTerms {
    if (this.expo != that.expo) throw new IncompatibleTerms();
    return new Term (this.coef + that.coef,expo);
}

這些是失敗的測試 = 2, 5, 8, 12, 16, 19, 21。但我對測試 17-23 有疑問; 我知道他們中的一些人通過了,但我認為他們不應該通過,因為這就像 x + y = y + x,但我不確定如何實現它。

我需要一些關於為什么這個測試失敗的幫助/指導。

對於測試 8 和 16,我嘗試了下面的代碼。 它完成了這項工作,但隨后會在其他測試中產生更多錯誤。 那么所有的失敗都變成了 2, 5, 8, 12, 16, 19, 21 + 20, 23 (兩個額外的失敗)

if (this.coef == Integer.MIN_VALUE || that.coef == Integer.MIN_VALUE) throw new CoefficientOverflow();
if (this.coef == Integer.MAX_VALUE || that.coef == Integer.MAX_VALUE) throw new CoefficientOverflow();

此鏈接顯示了檢測溢出的不同方法: http : //mindprod.com/jgloss/gotchas.html#OVERFLOW

我認為這是因為當 coef 在 this 或 that 中為 0 時,整個語句被假定為零,因此您需要檢查這一點並使用零的替代方案

if(this.coef ==0)
{//return Term t = new Term(that.coef, that.expo);}

else
{//return Term t = new Term(this.coef, this.expo);}
}

我希望這有幫助

您的equals方法不正確。 請更新如下:

@Override
public boolean equals(Object that) {
    if (that == null) {
        return false;
    }else if (this.getClass() != that.getClass()) {
        return false;
    }else if (this == that) {
        return true;
    }else{
        Term term = (Term) that;
        if ((term.coef) == (this.coef) && (term.expo) == (this.expo)) {
            return true;
        }
    }
    return false;
}

為了讓大部分測試用例通過,更新add方法如下:

public Term add(Term that) throws IncompatibleTerms, CoefficientOverflow{
    //handle addition with term zero
        if(this.equals(Term.Zero)){
        return that;
    }else if(Term.Zero.equals(that)){
        return this;
    }

    if (this.expo != that.expo) throw new IncompatibleTerms();
    if((this.coef>= 0 && that.coef >= 0 && this.coef + that.coef <0) ||
            (this.coef<= 0 && that.coef <= 0 && this.coef + that.coef >0)){
                         throw new CoefficientOverflow();
        }    
    return new Term (this.coef + that.coef,expo);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM