我需要在类中表示一个多项式:
PolyNode(带有下一个变量):
- int _power
- double _coefficient
- PolyNode _next
醇>Polynom(使用下一个构造函数和方法):
- public Polynom() - 空构造函数 - 空列表
- public Polynom(PolyNode p) - 从PolyNode获取参数并将其作为列表中的第一个插入
- public Polynom addNode(PolyNode p) - 从PolyNode获取参数并将其添加到Polynom。 并返回新的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类:
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.0倍^ 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 :(得分: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();
}
我建议你在深入练习之前先看一下链接列表的一些实现来理解它。
答案 1 :(得分:1)
正如Riduidel所说,有一个Java LinkedList
类可以为您处理存储和导航。因此,您可以使用LinkedList
类来存储PolyNode
个对象
要解决toString()
方法输出错误的具体问题:
您需要在创建输出_operation
时检查PolyNode
String
属性,以便打印“+”或“ - ”(带额外检查)如果是积极的话,不要在第一个项目前面显示“+”
您需要按照PolyNode
的降序顺序遍历Polynom
中的power
个项目(即Polynom
),最简单的方法是迭代{{1}以相反的顺序(或者更容易,使用LinkedList
并使用从Iterator
结尾处开始的List
此外,使用StringBuffer
或StringBuilder
类比使用String
更受欢迎,尤其是在很多步骤中构建可能很长的字符串时。
答案 2 :(得分:1)
Maman 17 ....我在同一个行业: - ))
我看到你添加了一个属性 - char _operation; 从我在OpenU论坛上看到的这是不允许的......
答案 3 :(得分:1)
你需要检查新的PolyNode功率并在多项式中找到它的位置并输入它。