1

所以我有一个双精度数组,它们代表多项式的系数。我想要一个返回多项式字符串的函数。

所以系数 = [1.3, 4.5, 6.0]

该函数将产生一个字符串

“1.3x^2 + 4.5x + 6.0”

我一直在考虑迭代来解决这个问题,但我不断收到错误。假设数组 coeffs 已经被构建。

public String toString()
{
    int len = coeffs.length;
    return 
    for(int i = 0; i < len ; ++i)
    {
        for(int j = len; len > 0; len--)
        {
            return this.coeffs[i] + "x^(" + len + ")" + ;
        }
    }
}
4

10 回答 10

2
public String toString(){
    int len = coeffs.length;
    StringBuilder return_value =new StringBuilder();

    // Add all the x to the power of something
    for(int i = 0; i < len-1 ; i++)
        return_value += this.coeffs[i]+"x^"+len-i-1+ " + ";

     // last one has no x.
     return_value += this.coeffs[len-1];
}

如果你不想有 2x^2 + 3x^1 + 5,你也可以换一种情况;(所以, x 也将是一个特例)

编辑:
就像 Kewin 建议的那样,如果长度为 0,这将引发异常。
请注意,您必须处理极端情况,并考虑如果其中一个系数为 0 会发生什么(您应该跳过该迭代):)

于 2013-11-12T06:33:05.623 回答
0

这个怎么样?

public String toString(){
  int len = coeffs.length;
  String equation = '';
  String power = '';
  for(int i = len - 1; i > 0; i--) {
    power = (i > 1 ? 'x^' + i : 'x');
    equation += (coeffs[len - 1 - i] + variable + " ");
  }
  equation += coeffs[0];
}
于 2013-11-12T06:36:09.323 回答
0
double [] coeff = {1.34,2.4,5.6};
String finalStr = "";

if(coeff.length>0){
    for(int i=coeff.length-1,j=0;i>=0 && j<coeff.length;i--,j++){
        finalStr = finalStr+coeff[j]+" "+"x^"+i+" + ";
        finalStr = (i==0)?finalStr.replace("x^0 + ", ""):finalStr;
    }
}

finalStr成为1.34 x^2 + 2.4 x^1 + 5.6

于 2013-11-12T06:43:41.333 回答
0

你也可以试试这个。

float[] coeffs = new float[]{1.4f, 1.3f, 4.5f, 6.0f};
if (coeffs == null || coeffs.length == 0) {
    return;
}
int power = coeffs.length - 1;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < coeffs.length; i++) {
    if(power == 0) {
        stringBuilder.append(" + " + coeffs[i]);
    } else if(power == 1) {
        stringBuilder.append(" + " + coeffs[i] + "x");
    } else {
        stringBuilder.append(" + " + coeffs[i] + "x^" + power);
    }
    power--;
}
System.out.println(stringBuilder.substring(3));
于 2013-11-12T06:42:18.647 回答
0

那这个呢

 public static String polyToString() {
  double[] coeffs = new double[] { 0.1, 0.2, 0.3, 1.1, 2.2, 1.3, 4.5, 6.0 };
  int len = coeffs.length;
  StringBuilder sb = new StringBuilder();

  if(len==0)
    return "";
  if(len==1)
    return ""+coeffs[0];

  for (int i = 3; i <= len; i++) {
    sb.append(coeffs[len - i] + "x^" + (len - i + 2) + " + ");
  }

  if (len >= 2) {
    sb.append(coeffs[len - 2] + "x + " + coeffs[len - 1]);
  }
  return sb.toString();

}

出去

1.3x^7 + 2.2x^6 + 1.1x^5 + 0.3x^4 + 0.2x^3 + 0.1x^2 + 4.5x + 6.0
于 2013-11-12T06:48:54.473 回答
0

你可以试试这个:

  double[] coeffs = { 1.3, 4.5, 6.0 };

    StringBuilder sb = new StringBuilder();

    int len = coeffs.length;

    for (int i = 0; i < len - 1; i++) {
        sb.append(coeffs[i]).append('x');
        if (len - 1 - i == 1) {
            sb.append('+').append(' ');
        } else {
            sb.append('^').append(len - i - 1).append('+').append(' ');
        }
    }
    sb.append(coeffs[len - 1]);// Append last element

    System.out.println(sb.toString());// Print "1.3x^2 + 4.5x + 6.0"

在控制台中输出结果:

1.3x^2+ 4.5x+ 6.0
于 2013-11-12T06:37:41.550 回答
0

如前所述,您不能返回循环。你必须做这样的事情

public String toString()
{
    int len = coeffs.length;
    String result = "";
    int j = coeffs.length-1;

    for(int i = 0; i < len ; ++i)
    {
        result += coeffs[i]+"x^("+j+")+" ; 
        j--;
    }

    return result.substring(0,result.length-1);
}
于 2013-11-12T06:33:05.643 回答
0

这个怎么样?

public String toString(){
    int len = coeffs.length;
    StringBuilder result = new StringBuilder();
    for(int i = 0; i < len; ++i){
            int power = ((i-len+1)*-1)
            result.append("" + coeffs[i] + "x^(" + power + ")");
            if (i < (len - 1)){ result.append(" + "); }
    }
    return result.toString();
}

1.3x^(2) + 4.5x^(1) + 6.0x^(0)

我不确定这是否是您想要的格式(?)

于 2013-11-12T06:33:22.513 回答
0

试试下面的代码:

public String toString(){
int len = coeffs.length;
StringBuilder sb=new StringBuilder();
return 
    for(int i = 0; i < len ; ++i){
            for(int j = len; len > 0; len--){
                return sb.append(this.coeffs[i] + "x^(" + len + ")");

                    }
        }
    }
于 2013-11-12T06:31:54.903 回答
0

类似于以下内容的内容可能是您正在寻找的内容。严格来说,下面的代码不使用迭代。可以遍历 values 数组。但是,为当前索引值设置一个整数可以更容易地确定是否需要“x”以及指数的正确格式(如果有)。

public final class Test1 {
  /*
   * The main routine is where execution actually starts
   */
  public static void main(String[] args) {
    System.out.println((new Coeffs()).toString());
  }
}  

class Coeffs {
  double[] values = new double[]  {1.3, 4.5, 6.0};
  public String toString() {
    int                 valuesLen = values.length;
    int                 exponent;
    StringBuffer        result = new StringBuffer();
    for (int i = 0; i < valuesLen; i++) {
      if (result.length() > 0) 
        result.append(" + ");
      result.append(((Double) values[i]).toString());
      exponent = valuesLen - i - 1;
      if (exponent > 0)
        result.append("x");
      if (exponent > 1) {
        result.append("^");
        result.append(((Integer) exponent).toString());
      }        
    }
    return new String(result);
  }
}
于 2013-11-12T06:50:42.063 回答