您的位置:首页 > 理论基础 > 数据结构算法

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

2016-06-15 19:07 459 查看

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 head;
PolyNode current;

public PolyList(){
head = new PolyNode();
current = head;
head.next = null;
}

public boolean isEmpty() {
return head.next == null;
}

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

/**
* 粗略打印,不考虑正负一输出的我情况
* @return
*/
public String toString(){
StringBuilder sb = new StringBuilder();
PolyNode node = head.next;
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) {
PolyNode pnext = p.head.next;
PolyNode qnext = q.head.next;
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) {
PolyNode pnext = p.head.next;
PolyNode qnext = q.head.next;
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;
pnext = p.head.next;//复位
}
//合并同类项
PolyNode current = result.head.next;
PolyNode preCurrent = result.head;
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());

//相加
PolyList resultList1= PolyList.addPoly(p1, p2);
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: