我需要在类中表示一个多项式:
PolyNode(带有下一个变量):
int _power
双系数
PolyNode _next
多项式(以及下一个构造函数和方法):
公共Polynom()-空构造函数-空列表
公共Polynom(PolyNode p)-从PolyNode获取参数并将其作为列表的第一位插入
公共Polynom 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.0x ^ 2 + 2.0
P2:
5.0x ^ 4-1.0x ^ 2 + 2.0
那就是我写的PolyNode类:
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);
}
}
那就是我写的Polynom类:
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.0x ^ 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
最佳答案
我想如果您有一个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();
}
我建议您在深入练习之前,先看一下链表的一些实现以了解链表。