链表的经典应用,程序在多项式相加同类项合并还有小的瑕疵,有待改进。
代码如下:
package 一元多项式计算器; public class PolyNode { private double a; private int i; PolyNode next; public PolyNode(double a,int i){ this.a=a; this.i=i; this.next=null; } public PolyNode(){ this(0,0); } public double getA() { return a; } public int getI() { return i; } public void setA(double a) { this.a = a; } public void setI(int i) { this.i = i; } }
package 一元多项式计算器; 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; } //打印多项式 public String printS(){ StringBuilder s=new StringBuilder(""); StringBuilder a=new StringBuilder(""); StringBuilder i=new StringBuilder(""); StringBuilder theOne=new StringBuilder(""); current=head.next; int count1=0; int count2=0; while(current!=null){ if(current.getA() == 0){ count1++; } count2++; current = current.next; } if(count1 == count2){ s.append("0"); }else{ current=head.next; while(current!=null){ a.delete(0, a.length()); i.delete(0, i.length()); theOne.delete(0, theOne.length()); if((current.getA()==1 || current.getA()==-1) && current.getI()!=0) a.append(""); else if(current.getA() < 0){ a.append(String.valueOf((-current.getA()))); }else{ a.append(String.valueOf(current.getA())); } if(current.getI()==1) { i.append(""); theOne.append(a.toString()).append("x").append(i.toString()); } else if(current.getI()==0){ i.append(""); theOne.append(a.toString()); } else{ i.append(String.valueOf(current.getI())); theOne.append(a.toString()).append("x^").append(i.toString()); } if(current.getA() == 0){ s.append(""); }else{ if(current==head.next && current.getA()<0){ s.append("-").append(theOne.toString()); }else if(current==head.next && current.getA()>0){ s.append(theOne.toString()); } else if(current.getA() < 0){ s.append(" - ").append(theOne.toString()); }else s.append(" + ").append(theOne.toString()); } current = current.next; } } return s.toString(); } //加法运算 public static PolyList add(PolyList p1,PolyList p2){ PolyList result=new PolyList(); //分别指向p1 p2的第一个元素 p1.current=p1.head.next; p2.current=p2.head.next; while(p1.current!=null && p2.current!=null){ if(p1.current.getI()==p2.current.getI()){ result.insert(new PolyNode(p1.current.getA()+p2.current.getA(),p1.current.getI())); p1.current=p1.current.next; p2.current=p2.current.next; } else if(p1.current.getI()<p2.current.getI()){ result.insert(p1.current); p1.current=p1.current.next; }else{ result.insert(p2.current); p2.current=p2.current.next; } } while(p1.current!=null){ result.insert(p1.current); p1.current=p1.current.next; } while(p2.current!=null){ result.insert(p2.current); p2.current=p2.current.next; } //return result; result.current=result.head.next; PolyNode tempPrevious=result.current; PolyNode temp=result.current.next; while(result.current.next!=null){ while(temp!=null) { if(temp.getI()!=result.current.getI()) { temp=temp.next; tempPrevious=tempPrevious.next; }else{ result.current.setA(result.current.getA()+temp.getA()); tempPrevious.next=temp.next; temp=temp.next; } } result.current=result.current.next; tempPrevious=result.current; temp=result.current.next; } return result; } //减法运算 public static PolyList sub(PolyList p1,PolyList p2){ PolyList result=new PolyList(); p2.current = p2.head.next; while(p2.current!=null){ p2.current.setA(-(p2.current.getA())); p2.current=p2.current.next; } //System.out.println(p2.printS()); //分别指向p1 p2的第一个元素 p1.current=p1.head.next; p2.current=p2.head.next; while(p1.current!=null && p2.current!=null){ if(p1.current.getI()==p2.current.getI()){ result.insert(new PolyNode(p1.current.getA()+p2.current.getA(),p1.current.getI())); p1.current=p1.current.next; p2.current=p2.current.next; } else if(p1.current.getI()<p2.current.getI()){ result.insert(p1.current); p1.current=p1.current.next; }else{ result.insert(p2.current); p2.current=p2.current.next; } } while(p1.current!=null){ result.insert(p1.current); p1.current=p1.current.next; } while(p2.current!=null){ result.insert(p2.current); p2.current=p2.current.next; } return result; } //乘法运算 public static PolyList multiply(PolyList p1,PolyList p2){ PolyList result=new PolyList(); //分别指向p1 p2的第一个元素 p1.current=p1.head.next; p2.current=p2.head.next; while(p1.current!=null){ while(p2.current!=null) { double a=p1.current.getA()*p2.current.getA(); int i=p1.current.getI()+p2.current.getI(); result.insert(new PolyNode(a,i)); p2.current=p2.current.next; } p1.current=p1.current.next; p2.current=p2.head.next; } //合并同类项 result.current=result.head.next; PolyNode tempPrevious=result.current; PolyNode temp=result.current.next; PolyNode t = result.current.next; while(result.current.next!=null){ while(temp!=null) { if(temp.getI()!=result.current.getI()) { temp=temp.next; tempPrevious=tempPrevious.next; }else{ result.current.setA(result.current.getA()+temp.getA()); tempPrevious.next=temp.next; temp=temp.next; } } result.current=result.current.next; tempPrevious=result.current; temp=result.current.next; } return result; } //就地逆置单链表 public static void reverse(PolyList p_){ PolyNode p,q; p = p_.head.next; p_.head.next = null; while(p!=null){ q = p.next; p.next = p_.head.next;//让p.next变成p_.head.next; p_.head.next = p;//让p_.head.next变成p; p = q;//让p变成q; } } }
package 一元多项式计算器; import java.util.*; public class Testclass { public static void main(String[] args) { // TODO Auto-generated method stub while(true){ PolyList p1 = new PolyList(); PolyList p2 = new PolyList(); Scanner scan = new Scanner(System.in); System.out.print("请输入要计算的第一个多项式的系数和指数,以0,0结束:"); double a = scan.nextDouble(); int i = scan.nextInt(); while(a!=0 || i!=0){ p1.insert(new PolyNode(a,i)); a = scan.nextDouble(); i = scan.nextInt(); } System.out.println("您输入的第一个多项式为:"); System.out.println("p1 = " + p1.printS()); System.out.print("请输入要计算的第二个多项式的系数和指数,以0,0结束:"); a = scan.nextDouble(); i = scan.nextInt(); while(a!=0 || i!=0){ p2.insert(new PolyNode(a,i)); a = scan.nextDouble(); i = scan.nextInt(); } System.out.println("您输入的第二个多项式为:"); System.out.println("p2 = " + p2.printS()); System.out.println("请按提示选择您想要进行的运算:"); System.out.println("0 -----> +"); System.out.println("1 -----> -"); System.out.println("2 -----> x"); int n; n = scan.nextInt(); if(n == 0){ PolyList resultList= PolyList.add(p1, p2); PolyList.reverse(resultList); System.out.println("p1 + p2 = "+resultList.printS()); }else if(n == 1){ PolyList resultList= PolyList.sub(p1, p2); PolyList.reverse(resultList); System.out.println("p1 - p2 = "+resultList.printS()); }else{ PolyList resultList= PolyList.multiply(p1, p2); //PolyList.reverse(resultList); System.out.println("p1 x p2 = "+resultList.printS()); } } } } //还存在的bug:1-当系数为0的时候应该删去一整项; 已解决! // 2-当一个多项式只有0时,应该输出一个0;已解决! // 3-计算器的仿真界面。已解决!使用不方便! // 4-合并同类项有空指针错误,待解决,错误数据如1 1 1 1 0 0+1 1 1 1 0 0;