一元多项式 java_java链表实现一元多项式的合并同类项以及加法

上课的作业:利用java数据结构的知识表示一元多项式,以及实现一元多项式的加法运算以及合并同类项

链表节点类:

package PloyItem;

public class Lnode implements Comparable{

public float coef;

public int exp;

public Lnode next;

public Lnode (float coef,int exp){

this.exp=exp;

this.coef=coef;

next=null;

}

public Lnode(float coef,int exp,Lnode next){

this.exp=exp;

this.coef=coef;

this.next=next;

}

public boolean equals(Object e){

Lnode node=(Lnode)e;

return (exp==node.exp);

}

@Override

public int compareTo(Lnode o) {

// TODO Auto-generated method stub

return 0;

}

}

多项式类:

package PloyItem;

import java.util.Iterator;

import org.junit.Test;

import seqList.AbList;

public class PloyItemList {

private int length;

Lnode first;

public PloyItemList(int length)

{

first=null;

this.length=length;

}

public PloyItemList(){

first=null;

length=0;

}

public int size()//获取链表的长度

{

Lnode p=first;

int count=0;

while(p!=null){

count++;

p=p.next;

}

return count;

}

public boolean add(float coef,int exp)//向链表当中添加元素的方法

{

Lnode p=first,s;

s=new Lnode(coef,exp,null);

if(first==null){

first=s;

s.next=null;

return true;

}

else {

while(p.next!=null){

p=p.next;

}

p.next=s;

s.next=null;

return true;

}

}

public boolean add(float coef)//想链表当中添加元素的方法

{

Lnode p=first,s;

s=new Lnode(coef,0,null);

if(first==null){

first=s;

s.next=null;

return true;

}

else {

while(p.next!=null){

p=p.next;

}

p.next=s;

s.next=null;

return true;

}

}

public void sort(){//对多项式进行降序排列的方法

Lnode p,q;int i,j,m;float n;

for( i=0,p=first;i

for(j=i+1,q=p.next;j

if(p.exp

{

m=p.exp;p.exp=q.exp;q.exp=m;

n=p.coef;p.coef=q.coef;q.coef=n;

}

}

public void union()//对多项式进行合并同类项的方法

{

Lnode p,q,r;

sort();

p=first;

q=p.next;

while(p!=null&& q!=null){

if(p.exp==q.exp)

{r=q;

p.coef=p.coef+q.coef;

remove(q.coef,q.exp);

p=r;

q=r.next;

}

else {

p=q;

q=q.next;

}

}

}

public void remove (float coef,int exp)//删除链表当中的某一个节点的方法

{

Lnode p=first,q=p;

for(q=p;q!=null;q=q.next)

if(q.next.coef==coef && q.next.exp==exp)

break;

q.next=q.next.next;

}

public String toString()//将链表转化为一个字符串输出的方法

{

String s="p(x)=";

Lnode p=first;

sort();

union();

while(p!=null){

if(p.coef==0)

s=s+"+";

else if(p.exp==0)

s=s+p.coef+"+";

else if(p.exp==1)

s=s+p.coef+"x"+"+";

else

s=s+p.coef+"x^"+p.exp+"+";

p=p.next;

}

return s.substring(0, s.length()-1)+"\n";

}

public void addPloyItem(PloyItemList p2)//多项式想家的而方法

{

this.sort();p2.sort();

Lnode p=this.first,q=p2.first;

while(p!=null || q!=null)

{

if(p!=null && q!=null)

{

if(p.exp==q.exp){

p.coef+=q.coef;

p=p.next;q=q.next;

}

else if(p.exp

this.add(q.coef, q.exp);

q=q.next;

}

else {

this.add(q.coef, q.exp);

q=q.next;

}

}

else if(p==null && q!=null)

this.add(q.coef, q.exp);

else if(p!=null && q==null)

p=p.next;

}

}

}

测试类:

package PloyItem;

/**

* 用线性表实现一元多项式的加法运算以及多项式的输出

* @author asus

*

*/

pub

95b1

lic class PolyItemDemo {

public static void main(String[] args) {

PloyItemList p1=new PloyItemList(4);

p1.add(3, 3);p1.add(2, 2);p1.add(1, 1);p1.add(1);

System.out.println("第一个多项式(合并同类项):"+p1);

p1.add(4, 4);

p1.add(15);

p1.add(3, 2);

System.out.println(p1);

System.out.println("多项式的项数为(合并同类项):"+p1.size());

PloyItemList p2=new PloyItemList();

p2.add(6,4);p2.add(4,4);p2.add(3,2);p2.add(1,0);

System.out.println(p2);

p2.add(19);

System.out.println(p2);

System.out.println("多项式的项数为:"+p2.size());

System.out.println("多项式的相加:");

p1.addPloyItem(p2);

System.out.println("相加的结果为:"+p1);

}

}

测试的结果:

7e7465364cfe76bcc642d85fe0f90bf1

标签:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值