有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多项式项的Java toString方法

我创建了一个没有使用多项式的多项式类,我使用自己的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"

但是实现所有这些功能并使其相互作用会让我感到非常头痛,例如,如果我有{},我希望{}出现,而{}表示{}。有人能帮我想出一些逻辑,把所有这些“规则”组合在一起,形成一个toString方法吗


共 (5) 个答案

  1. # 1 楼答案

    这里的秩序很重要。如果你想清楚了,你会发现coefficient = 0应该是第一位的,因为当它为零时,其他什么都不重要。接下来是当exponent等于10时的特殊情况。当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; 
        }
    }
    
  2. # 2 楼答案

    您可以将第一个特殊情况与最后一个特殊情况合并。您还可以通过查看系数的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();
    
  3. # 3 楼答案

    我想,你不知道世博会不会是负面的。尝试以下方法:

    @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;
        }
    }
    

    EDIT:要使用StringBuilder,请按如下所示更改最后一条语句(不过我看不到有什么好处)

       return new StringBuilder(pref).append("x").append(suff).toString();
    
  4. # 4 楼答案

    嗯,你真的没有什么办法让它变得更容易。您基本上有以下情况:

    系数=0-->;显示0(指数无关)
    系数=+/-1-->;显示-if-1(特例(-1,0)),否则不显示任何内容
    其他系数-->;显示为存储状态

    指数=0-->;不显示任何内容
    否则,请显示x^指数

    所以

    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;
    }
    

    我想这就够了吧?我并没有通过单元测试来真正确定

  5. # 5 楼答案

    这是我想说的最接近的了

    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));
        }
    }
    

    还有一个fiddle