我用Java做了一个多项式加法的程序,可是有一个地方不太明白,小弟是个菜鸟不要见笑

来源:百度知道 编辑:UC知道 时间:2024/07/06 15:03:50
public class Polynomial
{
private Monomial first ;//这是链表的首项
//一下是创建一个多项式的过程
public void append(Monomial monomial)
{
if(monomial == null)
{
//结点为空不做操作
}
else if(first == null)
{
first = monomial ;
}
else
{
Monomial current = first ;
while (current != null)
{
//下面是输入时如果输入的指数相同则相加
if (current.index == monomial.index)
{
current.coefficent += monomial.coefficent ;
break ;
}
else if (current.next == null)
{//next域为空意味着后面无数据了就放到最后一个
current.next = monomial ;
break ;
}
//next域不为空就把next指向的内容给current
current = current.next ;
}
}
}
public void append(double c, int i)//输入要做运算的多项式并将它的值返回给sb这个类
{

append (new Monomial( c, i)) ;
}
public String toString()
{
StringBuffer sb = new StringBu

类Monomial构造函数时有问题,把里边的void 去掉.把类Monomial改下.就OK了

class Monomial {
double coefficent;// 系数
int index; // 指数
Monomial next; // 后继结点

public Monomial(double c, int i) {
this.coefficent = c;
this.index = i;
// TODO Auto-generated constructor stub
}

public void Monomial() {
// 如果不输入数据就什么都不做
}

}

public class Polynomial
{
private Monomial first ;//这是链表的首项
//一下是创建一个多项式的过程
public void append(Monomial monomial)
{
if(monomial == null)
{
//结点为空不做操作
}
else if(first == null)
{
first = monomial ;
}
else
{
Monomial current = first ;
while (current != null)
{
//下面是输入时如果输入的指数相同则相加
if (current.index == monomial.index)
{
current.coefficent += monomial.coefficent ;
break ;
}
else if (current.next == null)