2012-01-24 33 views
1

我要代表班多项式:代表多项式链表

PolyNode(下一个变量):

  1. INT _电源
  2. 双_coefficient
  3. PolyNode _next

Polynom(与下一个构造函数&法):

  1. 公共多项式() - 空构造 - 空列表
  2. 公共多项式(PolyNode P) - 从PolyNode得到PARAM并插入他作为第一个列表
  3. 公共多项式ADDNODE上(PolyNode p) - 从PolyNode获取参数并将其添加到Polynom。 并返回新的多项式

这是测试用例:

// Create two Polynoms 
    Polynom p1 = new Polynom(); 
    p1.addNode(new PolyNode(0,2)); 
    p1.addNode(new PolyNode(2,4)); 
    System.out.println("\nP1:"); 
    System.out.println(p1); 

    Polynom p2 = new Polynom(new PolyNode(0,2)); 
    p2.addNode(new PolyNode(2,-1)); 
    p2.addNode(new PolyNode(4,5)); 
    System.out.println("\nP2:"); 
    System.out.println(p2); 

这是有用输出:

P1: 4.0倍^ 2 + 2.0

P2: 5.0x^4-1.0x^2 + 2.0

这是PolyNode I类写道:

public class PolyNode 
{ 
    char _operation; 
    int _power;  
    double _coefficient;  
    PolyNode _next; 



    public PolyNode() 
{ 
    _next = null; 
    _operation = '+'; 
    _coefficient = 1; 
    _power = 1; 
} 

    public PolyNode(char oper, double coeff, int power, PolyNode next) 
{ 
    _operation = oper; 
    _coefficient = coeff; 
    _power = power; 
    _next = next; 
} 


public PolyNode(PolyNode next) 
{ 
    _next = next; 
} 

public PolyNode(int power, int coeff) 
{ 
_power = power; 
    _coefficient = coeff; 
} 

public void setSign(char oper) 
{ 
    _operation = oper; 
} 


public void setCoef(double coeff) 
{ 
    _coefficient = coeff; 
} 


public void setPower(int power) 
{ 
    _power = power; 
} 


public void setNext(PolyNode next) 
{ 
    _next = next; 
} 


public char getSign() 
{ 
    return _operation; 
} 


public double getCoeff() 
{ 
    return _coefficient; 
} 

    public int getPower() 
{ 
    return _power; 
} 

public PolyNode getNext() 
{ 
    return _next; 
} 

public boolean isEnd() 
{ 
    return (_next == null); 
} 

} 

这是我写的多项式类:

public class Polynom 
{ 
    private PolyNode _head; 


    public Polynom() 
    { 
     _head = null; 
    } 

     public Polynom (Polynom poly) 
    { 
     Polynom r = new Polynom (poly); 
    } 

    public Polynom (PolyNode p) 
    { 
     _head = p; 
    } 

    public Polynom addNode (PolyNode p) 
    { 
     Polynom r = new Polynom (p); 
     PolyNode current; 

     if (_head == null) 
       _head = p; 

     else 
     { 
      current = _head; 
      while (current._next !=null) 
       current = current._next; 
       current._next = p; 

      } 
       return r; 
     } 

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

      while (_head != null) 
      { 
      s += _head.getCoeff() + "x^" + _head.getPower(); 
      _head = _head._next; 
     } 

      return s; 

     } 
    } 

这是我的错误输出:

P1: 2.0x^04.0x^2

P2: 2.0倍^^0-1.0x^25.0x 4

我不明白链表的想法!

toString()方法需要这样的示例输出:

R = 3.8x10 - 5.9x3 + 5.5x2 - 11.0

将显示像上的toString():

3.8 x^10 - 5.9 x^3 + 5.5 x^2 - 11。0

+0

希望Java有一个[LinkedList](http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html)类,它将为您完成存储工作。 – Riduidel

+1

这可能无法解决所有问题,但在Polynom.toString方法中,您正在重新分配Polynom的_head,这可能不是您想要的。您应该声明一个局部变量来迭代所有PolyNodes。 – ARRG

回答

1

我认为如果你有一个用于PolyNode的toString()方法,Polynom只是在列表中有一个节点时调用该方法会更容易。

要处理的唯一“特殊”情况是列表的头部,如果该术语是肯定的,则不需要打印“+”号。

除此之外,也有你的toString()实现,尤其是在这样几个问题,你遍历列表:

public String toString(){ 
    StringBuffer s = new StringBuffer(); 
    // WARNING: you don't want to use the list head for iteration, 
    // otherwise you lose the reference to it, and basically to the whole list! 
    PolyNode current = _head; // so we use a cursor node reference 
    while (current.next!=null){ // while current is not the last node 
     // you want to have the sign first, for every node, 
     // except the first, if it's positive 
     if(current!=_head || current.getSign()=='-') 
      s.append(current.getSign() + " "); 
     // then you append the coefficient 
     s.append(current.getCoeff()); 
     // and then the exponent 
     s.append(" x^" + current.getPower() + " "); 
     // you keep going to the next node 
     current = current._next; 
    } 
    return s.toString(); 
} 

我建议你看一下链表的一些实施不懂,然后进行练习。

+0

谢谢...我们不允许使用StringBuffer或“append”.. = \。谢谢 – Oshrib

+0

嗯,好的:好吧,只需使用像你那样的字符串。 :) –

1

正如Riduidel所说,有一个Java LinkedList类可以处理您的存储和导航。因此,您可以使用LinkedList类来存储PolyNode对象。
要从toString()方法解决输出错误的具体问题:

  • 创建输出String时,这样你就可以打印一个“+”,或者你需要检查你_operationPolyNode属性“ - ” (有额外的检查没有在第一项的前面显示一个“+”如果是正数)

  • 你需要遍历降序排列为指数(即power)在PolynomPolyNode项目,最简单的方法要做到这一点是迭代过以相反的顺序(或一个更容易,可以使用LinkedList并使用Iterator其开始于List的末尾)

  • 此外,StringBufferStringBuilder类的使用是优选的,以只使用String,特别是在建筑物可能很长的很多步骤中的字符串。

+0

嗨,谢谢。我们非常受限于类的使用 - 我们不允许使用StringBuffer ...或者简单地说 - 我们不允许使用比我的代码中显示的问题更多的问题 – Oshrib

+1

好吧,确保您的'PolyNode'将按正确顺序打印的最简单方法是实现'public Polynom addNode(PolyNode p)'这样一种方式,使得具有最高“power”的'PolyNode'总是存储在'PolyNome'的第一个位置。这实际上相当于插入排序(按降序排列)。为了促进这一点并改进重用,你可以添加一些额外的比较逻辑(例如,使'PolyNode'实现'Comparable'接口,或者如果你不能使用它,只需在'PolyNode'中写一个'compare'方法)实现这种行为。祝你好运! – fredo

1

马曼17 ....我在同样的业务:-))

我看见你已经添加的属性 - 焦_operation; 从我在OpenU论坛看到这是不允许的...

+0

它也不需要。只要检查系数是否在正确的位置就足够了。 – fredo

1

在addNode方法我认为你犯了一个错误。 他们说你需要将新节点添加到他的正确位置(关于他的电源号码)

你需要检查新的PolyNode电源并在polynom中找到他的位置并输入它。