代码之家  ›  专栏  ›  技术社区  ›  George Irimiciuc

打印多项式

  •  -2
  • George Irimiciuc  · 技术社区  · 12 年前

    我试图从给定的数字中打印出一个多项式。 我做了下面的例子,但是 100 它将打印 1x^2+ ,当我想 x^2 。我要找的是如何使其不打印 + 同时去除 1 .

    编辑:我做到了,它打印得很完美。请随意使用。

    private static String S_frumos(int poli) {
        String s = "";
        for (int i = 0; i < String.valueOf(poli).length(); i++) {
            int nr = Character.getNumericValue(S_GetCoefs(poli, i));
            if (nr != 0) {
                if (i == String.valueOf(poli).length() - 1) {
                    s = s + nr;
                } else if (i == String.valueOf(poli).length() - 2) {
                    if ((S_zero(poli, i + 1) == 1)) {
                        if (nr != 1) {
                            s = s + nr + "x";
                        } else {
                            s = s + "x";
                        }
                    } else {
                        if (nr != 1) {
                            s = s + nr + "x" + "+";
                        } else {
                            s = s + "x" + "+";
                        }
                    }
                } else if ((S_zero(poli, i + 1) == 1)) {
                    if (nr != 1) { s = s + nr + "x^" + (String.valueOf(poli).length() - i - 1);}
                    else  { s = s + "x^" + (String.valueOf(poli).length() - i - 1);}
                } else {
                    if (nr != 1){ s = s + nr + "x^" + (String.valueOf(poli).length() - i - 1) + "+";}
                    else { s = s + "x^" + (String.valueOf(poli).length() - i - 1) + "+";}
                }
            }
        }
    
        return s;
    }
    private static int S_GetCoefs(int poli, int x) {
        return String.valueOf(java.lang.Math.abs(poli)).charAt(x);
    }
    
    2 回复  |  直到 12 年前
        1
  •  1
  •   Will_61    12 年前

    存储未知长度的东西。。。那么您仍然可以使用int/double数组,只是稍微复杂一些。

    public static void main(String[] args)
    {
      // Say the size is given in a command line argument.
      int coefficientNumber = Integer.parseInt(args[0]);
    
      int[] poly = new int[coefficientNumber];
      for (int i = 0; i < poly.length; i++)
      {
        poly[i] = 0;
      }
      // Set the highest coeffient to 1 (if there is 3 coefficients, this is coefficient
      // of x^2, if 4 coefficients, this is coefficient of x^3
      poly[0] = 1;
      printPoly(poly);
    }
    
    // To print a polynomial of unknown length.
    // If the coefficient is 0, don't print it.
    private static void printPoly(int[] poly)
    {
      String output = "";
      for (int index = 0; index < poly.length; index++)
      {
        if (poly[index] != 0)
        {
          // If this is the first coefficient with a value
          if (output.length() == 0)
            output = poly[index] + "x^" + (poly.length - (index + 1));
          // Else if there is already some coefficient with values printed.
          else
            output += " + " + "x^" + (poly.length - (index + 1));
        } // if
      } // for
      System.out.println(output);
    } // printPoly
    
        2
  •  1
  •   Will_61    12 年前

    首先,将多项式存储在一个变量中不是一个好主意,因为如果系数大于9,你会感到困惑。 更好的方法imo(不生成多项式类)是将多项式存储在int/double数组中。

    public static void main(String[] args)
    {
      // To store the polynomial x^2, you could do the following:
      int[] poly = new int[3];
      poly[0] = 1;
      poly[1] = 0;
      poly[2] = 0;
      printPoly(poly);
    }
    
    // To print it:
    private static void printPoly(int[] poly)
    {
      String output = "";
      if (poly[0] != 0)
        output += poly[0] + "x^2"
      if (poly[1] != 0)
      {
        if (output.size() > 0)
          output += " + " + poly[1] + "^x";
        else
          output += poly[1] + "x";
      }
      if (poly[2] != 0)
      {
        if (output.size() > 0)
          output += " + " + poly[2];
        else
          output += poly[2];
      }
    }