Programa para suma de Polinomios usando Listas enlazadas (Java)
Programa para realizar suma de polinomios con listas enlazadas.
package com.ubicuos.main; import java.util.LinkedList; import java.util.ArrayList; import java.util.Arrays; class Term implements Comparable { private char symbol; private double exponent; private double coefficient; public double eval (double value) { return coefficient * Math.pow (value, exponent); } public Term (double coefficient, double exponent) { this.exponent = exponent; this.coefficient = coefficient; } public Term () { symbol = 'x'; } public String toString () { StringBuffer sb = new StringBuffer (12); sb.append (coefficient); sb.append (symbol); sb.append ('^'); sb.append (exponent); return sb.toString (); } public double getCoefficient () { return coefficient; } public double getExponent () { return exponent; } public void setExponent (double exponent) { this.exponent = exponent; } public void setCoefficient (double coefficient) { this.coefficient = coefficient; } public Term add (Term a, Term b) { Term c = new Term (); if (a.getExponent () == b.getExponent ()) { c.setCoefficient (a.getCoefficient () + b.getCoefficient ()); c.setExponent (a.getExponent ()); return c; } else return c; } public int compareTo (Object anotherTerm) { if (!(anotherTerm instanceof Term)) throw new ClassCastException ("Term Expected /Se esperaba un termino."); double expo = ((Term) anotherTerm).getExponent (); return (int) (this.exponent - expo); } public static LinkedList add (LinkedList a, LinkedList b) { LinkedList result = new LinkedList (); Term x, y; int i = 0, j = 0; int maxLen = Math.max (a.size (), b.size ()); do { x = (Term) a.get (i); y = (Term) b.get (j); if (x.compareTo (y) > 0) { result.add (a.get (i)); result.add (b.get (j)); i++; } if (x.compareTo (y) == 0) { result.add (x.add ((Term) a.get (i), (Term) b.get (j))); i++; j++; } if (x.compareTo (y) < 0) { result.add (b.get (j)); result.add (a.get (i)); } i++; j++; } while (i < maxLen || j < maxLen); return result; } } public class SumPol { public static void main (String args[]) { Term a = new Term (1, 7); Term b = new Term (2, 3); System.out.println (a.add (a, b)); LinkedList x = new LinkedList (), y = new LinkedList (), res; x.add (a); y.add (b); res = Term.add (x, y); System.out.println ("suma = " + res.toString ()); }}
Quizá te interese :
Leer datos con Java Read from Console Standard Input with Java Si alguna vez, necesitaste leer ...
#include #include double f (double x) { return (x * x) - 1; } double df (double x ...
Draw an asterisk tree with Java Uno de los ejemplos clásicos de uso de ciclos en los lenguajes ...









no tienes el metodo main???
Si está al final, se crean las linked list y se llama al método.
Si tienes otra duda, no tengas inconveniente en preguntar.
me puedes enviar el package com.ubicuos.main
hola necesito el programa completo para sumar polinomios me lo piden como trabajo final en la escuela porfa m lo puedes mandar a mi correo…t lo agradeceria mucho….
El programa, está completo lo puedes copiar y pegar en tu editor, y comprobarlo.
Si tienes dudas (Sobre el programa), mandalas, con gusto.
hola tengo una duda en ese caso tu agregas los terminos de el polinomio, pero si recibieras dos polinomios en forma de lista, como lo recorerias para ir sumandolo las listas creadas con linkedlist claro espero me puedas responder, gracias por este aporte