lllllt95 于 2014.10.31 18:01 提问

public class Literal {
private int coefficient;
private int exponent;
Literal next = null;// 默认是null

``````public Literal() {
this(0, 0);
}

public Literal(int coefficient, int exponent) {
setCoe(coefficient);
setExp(exponent);
this.next=null;
}

public int getCoe() {
return coefficient;
}

public int getExp() {
return exponent;
}

public void setCoe(int coefficient) {
this.coefficient = coefficient;
}

public void setExp(int exponent) {
this.exponent = exponent;
}
``````

}

public class Polynomial {
private Literal current;

``````public Polynomial() {
}

public void insertTerm(int coefficient, int exponent) {
Literal node = new Literal(coefficient, exponent);
current.next = node;// 在现有结点后添加新结点
current = node;
}

Polynomial result = new Polynomial();

// 只有都不为空才能比较指数大小
while (current != null && p.current != null) {
System.out.println("指数 "+current.getExp()+" "+p.current.getExp());
// 指数相等时候
if (current.getExp() == p.current.getExp()) {
int a = current.getCoe() + p.current.getCoe();
if (a != 0) {
result.insertTerm(a, current.getExp());
current = current.next;
p.current = p.current.next;
}
}

// 指数不等时候
if (current.getExp() < p.current.getExp()) {
result.insertTerm(current.getCoe(), current.getExp());
current = current.next;
}
if (current.getExp() > p.current.getExp()) {
result.insertTerm(p.current.getCoe(), p.current.getExp());
p.current = p.current.next;
}
result.print();
}

// 处理还有剩余项的情况
while (current != null) {
result.insertTerm(current.getCoe(), current.getExp());
current = current.next;
}
while (p.current != null) {
result.insertTerm(p.current.getCoe(), p.current.getExp());
p.current = p.current.next;
}

return result;
}

public void print() {
String result = "";

while (current != null) {
String coe = "";
String str = "";// 先清空

if (current.getCoe() != 1)// 系数
coe += current.getCoe();

if (current.getExp() == 1) // 指数为1
str = coe + "x";
else
str = coe + "x^" + current.getExp();

result += str;
else
result += "+" + str;

current = current.next;
}
System.out.println(result);
}

public static void main(String[] args) {
Polynomial p1 = new Polynomial();
p1.insertTerm(1, 1);
p1.insertTerm(2, 2);
p1.insertTerm(3, 3);
p1.insertTerm(4, 4);

Polynomial p2 = new Polynomial();
p2.insertTerm(2, 3);
p2.insertTerm(4, 5);

//模拟实现下面p1,p2相加的过程
Polynomial polynomial=new Polynomial();
polynomial.insertTerm(p2.current.getCoe(), p2.current.getExp());
p2.current=p2.current.next;
polynomial.insertTerm(p2.current.getCoe(), p2.current.getExp());
polynomial.print();

System.out.print("p1+p2=");
p.print();
}
``````

}

2x^3+4x^5

x

at Phlonomial.Polynomial.insertTerm(Polynomial.java:17)
at Phlonomial.Polynomial.main(Polynomial.java:108)

Polynomial result = new Polynomial();
if (current.getExp() < p.current.getExp()) {
result.insertTerm(current.getCoe(), current.getExp());
current = current.next;
}

1个回答

ChinMint   2016.10.25 11:05

//均已调试通过,编译器为DEV C++ /多项式相加程序,用链表实现 //系数 coefficient  指数exponent   多项式 multinomial  #include  typedef struct Lnode {         int coe;        int exp;        struct Lnode *next;        }Lnode,*LinkLi

#include using namespace std; /*结点的定义*/ typedef struct LNode { float coef; int exp; struct LNode *next; }LNode; typede
Java链表形式实现多项式相加

C语言：用链表实现一元多项式的加法
/* 一元多多项式的加法 1.先创建链表，存储多项式 2.输出多项式 3.两个多项式相加 4.输出多项式 */ # include # include typedef struct dxs //多项式节点 { float coe; //系数 int exp; //指数 struct dxs * pNext; //指针域 }DXS, * PDXS; PDXS c
java数据结构之单链表应用之一元多项式求和

【数据结构】用链表实现多项式运算