3

我创建了一个多项式类而不使用多项式,我使用我自己的Term(coefficient, exponent)来创建多项式表达式。

我有一些条件如下:

coefficient = 0 -> Term(0,2) -> 0x^2 -> "0"
coefficient = 1 -> Term(1,2) -> 1x^2 -> "x^2"
coefficient = -1 -> Term(-1,2) -> -1x^2 -> "-x^2"
exponent = 1 = -> Term(5,1) -> 5x^1 -> "5x"
exponent = 0 = -> Term(5,0) -> 5x^0 -> "5"

但是实现所有这些以在彼此内部和周围发挥作用让我非常头疼,例如,如果我有Term(-1,1)我想"-x"出现,并且"x"Term(1,1). 任何人都可以帮助考虑某种逻辑来将所有这些“规则”组合在一起以形成 toString 方法吗?

4

5 回答 5

1

顺序在这里很重要。如果您仔细考虑,您会发现coefficient = 0应该先行,因为当它为零时,其他任何事情都不重要。接下来是当exponent等于1或时的特殊情况0。然后你有当coefficient-1。剩下的就是默认情况下的负数coefficient-1正数coefficient。所以 if 语句应该如下所示:

public String toString() {
    if(coefficient == 0){
      return "0";
    } else if ( coefficient == 1 && exponent != 0){
        return "x^"+exponent; 
    } else if ( exponent == 1){
      return coefficient+"x"; 
    } else if ( exponent == 0){
      return ""+coefficient; 
    } else if ( coefficient == -1){
      return "-x^"+exponent; 
    } else {
      return coefficient+"x^"+exponent; 
    }
}
于 2012-11-16T21:08:26.057 回答
1

您可以将第一个特殊情况与最后一个特殊情况结​​合起来。您还可以通过查看abs系数的值来组合第二种和第三种情况。

// If the coefficient is zero or the exponent is zero,
// the result is simply the coefficient:
if (c == 0 || e == 0) {
    return ""+c;
}
StringBuilder res = new StringBuilder();
// Do not print the number for the coefficient of +/- 1
if (Math.abs(c) == 1) {
    // For +1 do not print the sign either
    if (c == -1) {
        res.append("-");
    }
} else {
    res.append(c);
}
res.append("x");
// For exponent of 1, do not print ^1
if (e != 1) {
    res.append("^");
    res.append(e);
}
return res.toString();
于 2012-11-16T21:09:56.937 回答
1

这是尽可能接近的,我要说清楚

public class Term { 
    private final int coefficient;
    private final int exponent;

    public Term (final int coefficient,final int exponent) {
        this.coefficient = coefficient;
        this.exponent = exponent;           
    }

    @Override
    public String toString() {
        final String sign = getSign (coefficient);
        final String number = getNumber (coefficient);
        final String exponentStr = getExponentStr (coefficient, exponent);

        return String.format ("%s%s%s",sign, number, exponentStr);
    }

    private String getExponentStr(final int coefficient, final int exponent) {
        if (coefficient == 0 || exponent == 0) {
            return "";
        }
        if (exponent == 1) {
            return "x";
        }
        return "x^" + exponent;
    }

    private String getNumber(final int value) {
        final int absValue = Math.abs(value);

        return absValue == 1 ? "" : Integer.toString (absValue);
    }

    private String getSign(final int value) {
        return value < 0 ? "-" : "";
    }

    public static void main(String[] args) throws Exception {
        System.out.println(new Term (0, 2));
        System.out.println(new Term (1, 2));
        System.out.println(new Term (-1, 2));
        System.out.println(new Term (5, 1));
        System.out.println(new Term (5, 0));
    }
}

和一个小提琴

于 2012-11-16T21:14:37.920 回答
1

我想,你不博览会不会是负面的。尝试如下:

@Override
public String toString() {
    if(coef ==0){
        return "0";
    }else if(expo ==0){
        return ""+coef;
    }else{
        String pref = coef==1? "": coef==-1?"-":""+coef; 
        String suff = expo>1? "^"+expo:""; 
        return pref+"x"+suff;
    }
}

编辑:要使用StringBuilder,将最后一条语句更改如下(虽然我没有看到太多好处)

   return new StringBuilder(pref).append("x").append(suff).toString();
于 2012-11-16T21:16:47.620 回答
0

嗯,你真的没有什么可以做的来让它更容易。您基本上有以下情况:

系数 = 0 --> 显示 0(与指数无关)
系数 = +/- 1 --> 显示 - 如果为 -1((-1,0) 的特殊情况),否则没有
其他系数 --> 显示为存储

Exponent = 0 --> 不显示
否则,显示 x^exponent...

所以.....

public String toString() {
  String term = "";

  if (coefficient == 0) {
    return "0";
  } elseif (coefficient == -1) {
    if (exponent == 0) {
      return "-1";
    } else {
      term += "-";
    }
  } elseif (coefficient != 1) {
    term += String.valueOf(coefficient);
  } else {
    if (exponent == 0) {
      return "1";
    }
  }

  if (exponent != 0) {
    if (exponent == 1) {
      term += "x";
    } else {
      term += "x^" + String.valueOf(exponent);
    }
  }

  return term;
}

我认为这涵盖了它?我没有通过 UnitTest 来确定它。

于 2012-11-16T21:08:32.830 回答