## Java数据结构之多项式加法和乘法实现

• 存储结构
• 数组
• 链表

### 数组实现

package com.genge.jichu.polyn;

/**
* 用数组表示的多项式，以及相关实现
* Created by Genge on 2016-06-15.
*/
public class Polynomial {
/**
* 多项式的加法
*
* @param a 多项式系数数组
* @param b 多项式系数数组
* @return 多项式系数数组
*/
public static double[] addPolyn(double[] a, double[] b) {
double[] heigher = a.length > b.length ? a : b;
double[] lower = a.length > b.length ? b : a;
double[] result = new double[heigher.length];
for (int i = 0; i < heigher.length; i++) {
if (i < lower.length) {
result[i] = heigher[i] + lower[i];
} else {
result[i] = heigher[i];
}
}
return trim(result);
}

/**
* 多项式乘法（拆分法）
*
* @param a
* @param b
* @return
*/
public static double[] mulPolyn(double[] a, double[] b) {
double[] result = new double[a.length + b.length - 1];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {
result[i + j] += (a[i] * b[j]);
}
}
return trim(result);
}

/**
* 将高位系数为0的项去掉
*
* @param a 源多项式
* @return 处理后的多项式
*/
public static double[] trim(double[] a) {
int k = a.length - 1;
for (; k > 0 && a[k] == 0.0; k--) ;
double[] newarr = new double[k++];
System.arraycopy(a, 0, newarr, 0, k + 1);
return newarr;
}

}

### 链表实现

package com.genge.jichu.polyn;

/**
* 用单链表实现多项式
* Created by Genge on 2016-06-15.
*/
public class PolyNode {

private double coef;//系数
private int expn;//指数
public PolyNode next;

public PolyNode() {
this(0, 0);
}

public PolyNode(double coef, int expn) {
this.coef = coef;
this.expn = expn;
}

public double getCoef() {
return coef;
}

public void setCoef(double coef) {
this.coef = coef;
}

public int getExpn() {
return expn;
}

public void setExpn(int expn) {
this.expn = expn;
}
}

package com.genge.jichu.polyn;

/**
* Created by Genge on 2016-06-15.
*/
public class PolyList {
PolyNode current;

public PolyList(){
}

public boolean isEmpty() {
}

public void insert(PolyNode node) {
current.next = node;
current = node;
}

/**
* 粗略打印，不考虑正负一输出的我情况
* @return
*/
public String toString(){
StringBuilder sb = new StringBuilder();
while (node != null) {
sb.append(node.getCoef() + "x^"+node.getExpn());
sb.append(" + ");
node = node.next;
}
return sb.substring(0, sb.length() - 2);
}

/**
* 多项式相加
* @param p
* @param q
* @return
*/
public static PolyList addPoly(PolyList p, PolyList q) {
PolyList result = new PolyList();

while (pnext != null && qnext != null) {
int pexpn = pnext.getExpn();
int qexpn = qnext.getExpn();
double pcoef = pnext.getCoef();
double qcoef = qnext.getCoef();
if (pexpn == qexpn) {
if (pcoef+qcoef != 0) {
PolyNode node = new PolyNode(pcoef + qcoef, pexpn);
result.insert(node);
}
pnext = pnext.next;
qnext = qnext.next;
}else if(pexpn < qexpn){
PolyNode node = new PolyNode(pnext.getCoef(), pnext.getExpn());
result.insert(node);
pnext = pnext.next;
}else{
PolyNode node = new PolyNode(qnext.getCoef(), qnext.getExpn());
result.insert(node);
qnext = qnext.next;
}
}
//q多项式已经完成
while (pnext != null) {
PolyNode node = new PolyNode(pnext.getCoef(), pnext.getExpn());
result.insert(node);
pnext = pnext.next;
}
//p多项式已经完成
while (qnext != null) {
PolyNode node = new PolyNode(qnext.getCoef(), qnext.getExpn());
result.insert(node);
qnext = qnext.next;
}
return result;
}

/**
* 多项式相乘
* @param p  多项式
* @param q  多项式
* @return   乘积
*/
public static PolyList mulPoly(PolyList p, PolyList q) {
PolyList result = new PolyList();
while (qnext != null) {
while (pnext != null) {
double coef = pnext.getCoef() * qnext.getCoef();
int expn = pnext.getExpn() + qnext.getExpn();
result.insert(new PolyNode(coef, expn));
pnext = pnext.next;
}
qnext = qnext.next;
}
//合并同类项
while (current != null) {
PolyNode nextNode = current.next;
PolyNode preNode = current;
while (nextNode != null) {
if (nextNode.getExpn() == current.getExpn()) {
current.setCoef(current.getCoef() + nextNode.getCoef());
nextNode = nextNode.next;
preNode.next = nextNode;
}else{
preNode = preNode.next;
nextNode = nextNode.next;
}
}
//删除系数为0的项
if (current.getCoef() == 0) {
preCurrent.next = current.next;
}
current = current.next;
}
return result;
}
}


package com.genge.jichu.polyn;

/**
* Created by Genge on 2016-06-15.
*/
public class PolyTest {
public static void main(String[] args) {
//多项式p1
PolyList p1=new PolyList();
p1.insert(new PolyNode(2,2));
p1.insert(new PolyNode(100,3));
p1.insert(new PolyNode(45,5));
p1.insert(new PolyNode(3,20));
System.out.println("p1="+p1.toString());

//多项式p2
PolyList p2=new PolyList();
p2.insert(new PolyNode(8,2));
p2.insert(new PolyNode(7,3));
p2.insert(new PolyNode(4,4));
p2.insert(new PolyNode(6,18));
p2.insert(new PolyNode(7,20));
System.out.println("p2="+p2.toString());

//相加
System.out.println("p1+p2="+resultList1.toString());

System.out.println();

//多项式p3
PolyList p3=new PolyList();
p3.insert(new PolyNode(2,1));
p3.insert(new PolyNode(3,2));
p3.insert(new PolyNode(4,3));
System.out.println("p3="+p3.toString());

//多项式p4
PolyList p4=new PolyList();
p4.insert(new PolyNode(5,1));
p4.insert(new PolyNode(1,2));
System.out.println("p4="+p4.toString());

//相乘
PolyList resuList2=PolyList.mulPoly(p3, p4);
System.out.println("p3*p4="+resuList2.toString());
}
}


p1=2.0x^2 + 100.0x^3 + 45.0x^5 + 3.0x^20
p2=8.0x^2 + 7.0x^3 + 4.0x^4 + 6.0x^18 + 7.0x^20
p1+p2=10.0x^2 + 107.0x^3 + 4.0x^4 + 45.0x^5 + 6.0x^18 + 10.0x^20

p3=2.0x^1 + 3.0x^2 + 4.0x^3
p4=5.0x^1 + 1.0x^2
p3*p4=10.0x^2 + 17.0x^3 + 23.0x^4 + 4.0x^5 

#### 多项式加法的Java实现

2015-06-16 19:03:45

#### 用Java实现多项式相加

2013-06-14 20:00:54

#### java多项式相加

2014年03月23日 3KB 下载

#### 巩固一下基础，java多项式相加

2014-06-13 21:20:49

#### Java实现多项式

2015-04-01 12:30:20

#### 多项式乘积（Java）

2016-05-19 13:57:30

#### java 实现多项式的运算

2014年11月25日 7KB 下载

#### PAT 02-线性结构2 一元多项式的乘法与加法运算 (java)

2016-09-25 22:35:34

#### java数据结构之单链表应用之一元多项式求和

2016-06-01 22:36:28

#### Java链表形式实现多项式相加

2015-07-30 17:45:24

## 不良信息举报

Java数据结构之多项式加法和乘法实现