Using the Java List to implement polynomial addition, multiply

Source: Internet
Author: User

Package Com.learn.algorithm.ploy;import Java.util.linkedlist;import Java.util.list;import java.util.Scanner;/** * Polynomial-related Operations */public class Ploy {public static void main (string[] args) {list<node> La = init (); List<node> Lb = init (); System.out.println (Polymulti (LA,LB));} private static list<node> init () {list<node> poly = new linkedlist<node> (); Scanner sc = new Scanner (system.in); System.out.println ("Enter the coefficients and parameters (for example, a-B indicates ax^b, the input 0,0 ends.) ): "); (true) {String line = Sc.nextline (); if (vaildate line) {string[] split = Line.split (", "); int coefficient = in Teger.parseint (split[0]); int exponential = Integer.parseint (split[1]); if (coefficient = = 0 && exponential = = 0) { break;} Poly.add (new Node (coefficient, exponential));} else {System.out.println ("[" + Line + "] entered incorrectly");}} SYSTEM.OUT.PRINTLN (poly); return poly;} /** * Polynomial addition * @param La * @param Lb * @return */public static list<node> polyPlus (list<node> LA,LIST&LT;NODE&G T LB) {list<node> Lc =New linkedlist<> (); int Sa = La.size (), Sb=lb.size (), Ia=0,ib=0;while (IA < Sa && IB < Sb) {if (La.get (i a). Getexponential () < Lb.get (IB). Getexponential ()) {Lc.add (La.get (IA)); ia + +;} else if (La.get (IA). Getexponential ( ) = = Lb.get (ib). Getexponential ()) {int Coe = La.get (ia). Getcoefficient () + Lb.get (IB). Getcoefficient (); if (Coe! = 0) {Lc. Add (New Node (Coe,la.get (IA). Getexponential ()));} IA + +, IB + +;} else {Lc.add (Lb.get (IB)); IB + +;}} while (IA < Sa) {Lc.add (La.get (ia++));} while (IB < Sb) {Lc.add (Lb.get (ib++));} return Lc;} /** * Polynomial addition (unordered) * @param La * @param Lb * @return */public static list<node> polyplus_update (list<node> la,lis T<node> Lb) {int Sa = La.size (), Sb=lb.size (), Ia=0,ib=0;while (IA < Sa) {node node = La.get (IA); Node nodebyexp = Getnodebyexp (Lb,node.getexponential ()), if (nodebyexp! = null) {if (node.getcoefficient () + nodebyexp.ge Tcoefficient () = = 0) {lb.remove (nodebyexp);} else{nodebyexp.setcoefficient (Node.getcoefficient () + nodebyexp.getcoefficient ());}} else {lb.add (node);} ia + +;} return Lb;} /** * Polynomial multiplication * @param La * @param Lb * @return */public static list<node> polymulti (list<node> la,list<node& Gt LB) {list<node> Lc = new linkedlist<> (), int Sa = La.size (), Sb=lb.size (), Ia=0,ib=0;while (IA < Sa) {IB = 0; Node Na = La.get (IA), while (IB < Sb) {Node Nb = Lb.get (IB); int exp = nb.getexponential () + na.getexponential ();//exponential addition in  T Coe = nb.getcoefficient () * na.getcoefficient ();//coefficients multiplied by node Nodebyexp = Getnodebyexp (LC, exp); if (nodebyexp! = null) {if (Coe + nodebyexp.getcoefficient () = = 0) {Lc.remove (nodebyexp);} Else{nodebyexp.setcoefficient (coe+ nodebyexp.getcoefficient ());}} else {lc.add (new Node (COE,EXP));} IB + +;} ia + +;} return Lc;}  /** * search for corresponding items based on coefficients, no return null * @param p * @param exp * @return */public static Node getnodebyexp (list<node> P,integer EXP) {if (exp = = NULL | | p = = NULL) {return null;} for (Node node:p) {if (node.exponential = = exp) {return Node;}} Return null;} /** * Verify the legitimacy of the input string * @param s * @return */public static Boolean vaildate (string s) {return s.matches ("[-]{0,1}[0-9]+[,]{1}[0 -9]+ ");}} /** entity class * */class Node {integer coefficient = 0;//coefficient Integer exponential = 0;//Index public node (integer coefficient,integer E xponential) {this.coefficient = Coefficient;this.exponential = exponential;} Public Integer getcoefficient () {return coefficient;} public void setcoefficient (Integer coefficient) {this.coefficient = coefficient;} Public Integer getexponential () {return exponential;} public void setexponential (Integer exponential) {this.exponential = exponential;}  @Overridepublic String toString () {return this.coefficient.toString () + "x^" + this.exponential.toString ();}}

It can also be implemented in maps, and it's easier

Using the Java List to implement polynomial addition, multiply

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.