Java - Listas Enlazadas

 
Vista:

Listas Enlazadas

Publicado por Ayudaaa (2 intervenciones) el 15/04/2020 22:53:09
Escribir un programa que permita dar entrada a polinomios en x, representándolos
con una lista enlazada simple.

Escribir métodos para:

Evaluar el polinomio en un valor de x dado
Obtener la suma de 2 polinomios
Obtener la derivada de un polinomio
Obtener el producto de 2 poliniomios
Alguien Ayudeme
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
-1
Responder

Listas Enlazadas

Publicado por carlos andres hinestroza (1 intervención) el 24/04/2021 07:59:38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*Cabe recalcar que falta la opción de evaluar polinomio en x cuando tenga tiempo lo subire. espero les sirva */
/*este es el ejecutable:)*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Ejecutable {
 
    public static void main(String[] args) throws NumberFormatException, IOException {
 
        BufferedReader dato = new BufferedReader(new InputStreamReader(System.in));
 
        int salir = 1;
        do {
            System.out.println("\n1. Obtener la suma de 2 polinomios");
            System.out.println("2. Obtener la derivada de un polinomio");
            System.out.println("3. Obtener el producto de 2 poliniomios.");
            System.out.println("4. Salir");
            System.out.print("Ingrese opcion: ");
            int opcion = Integer.parseInt(dato.readLine());
            switch (opcion) {
                case 1:
                    ListaSE poli1 = new ListaSE();
                    ListaSE poli2 = new ListaSE();
                    System.out.print("Incialmente ingrese cuantos terminos posee el 1 primer Polinomio: ");
                    int cantidad = Integer.parseInt(dato.readLine());
                    for (int i = 1; i <= cantidad; i++) {
                        System.out.print("Termino: " + i + "\n");
                        System.out.print("Ingrese coeficiente: ");
                        int coeficiente = Integer.parseInt(dato.readLine());
                        System.out.print("Ingrese exponente: ");
                        int exponente = Integer.parseInt(dato.readLine());
                        if (exponente == 0)
                            exponente = -1;
                        Polinomio poli = new Polinomio(coeficiente, exponente);
                        poli1.insertarAlFrente(poli);
                    }
                    System.out.print("Incialmente ingrese cuantos terminos posee el 2 segundo Polinomio: ");
                    int cantidad2 = Integer.parseInt(dato.readLine());
                    for (int i = 1; i <= cantidad2; i++) {
                        System.out.print("Termino: " + i + "\n");
                        System.out.print("Ingrese coeficiente: ");
                        int coeficiente = Integer.parseInt(dato.readLine());
                        System.out.print("Ingrese exponente: ");
                        int exponente = Integer.parseInt(dato.readLine());
                        if (exponente == 0)
                            exponente = -1;
                        Polinomio poli = new Polinomio(coeficiente, exponente);
                        poli2.insertarAlFrente(poli);
                    }
                    System.out.println();
                    System.out.print("RESPUESTA SUMA:  ");
                    sumaPolinomios(poli1.primerNodo, poli2.primerNodo, cantidad, cantidad2);
                    System.out.println();
                    break;
                case 2:
                    ListaSE derivada = new ListaSE();
                    System.out.print("Incialmente ingrese cuantos terminos posee el 1 primer Polinomio: ");
                    cantidad = Integer.parseInt(dato.readLine());
                    for (int i = 1; i <= cantidad; i++) {
                        System.out.print("Termino: " + i + "\n");
                        System.out.print("Ingrese coeficiente: ");
                        int coeficiente = Integer.parseInt(dato.readLine());
                        System.out.print("Ingrese exponente: ");
                        int exponente = Integer.parseInt(dato.readLine());
                        Polinomio poli = new Polinomio(coeficiente, exponente);
                        derivada.insertarAlFrente(poli);
                    }
                    System.out.println();
                    System.out.print("RESPUESTA DERIVACION:  ");
                    derivadaPolinomios(derivada.primerNodo);
                    System.out.println();
                    break;
                case 3:
                    poli1 = new ListaSE();
                    poli2 = new ListaSE();
                    System.out.print("Incialmente ingrese cuantos terminos posee el 1 primer Polinomio: ");
                    cantidad = Integer.parseInt(dato.readLine());
                    for (int i = 1; i <= cantidad; i++) {
                        System.out.print("Termino: " + i + "\n");
                        System.out.print("Ingrese coeficiente: ");
                        int coeficiente = Integer.parseInt(dato.readLine());
                        System.out.print("Ingrese exponente: ");
                        int exponente = Integer.parseInt(dato.readLine());
                        Polinomio poli = new Polinomio(coeficiente, exponente);
                        poli1.insertarAlFrente(poli);
                    }
                    System.out.print("Incialmente ingrese cuantos terminos posee el 2 segundo Polinomio: ");
                    cantidad2 = Integer.parseInt(dato.readLine());
                    for (int i = 1; i <= cantidad2; i++) {
                        System.out.print("Termino: " + i + "\n");
                        System.out.print("Ingrese coeficiente: ");
                        int coeficiente = Integer.parseInt(dato.readLine());
                        System.out.print("Ingrese exponente: ");
                        int exponente = Integer.parseInt(dato.readLine());
                        Polinomio poli = new Polinomio(coeficiente, exponente);
                        poli2.insertarAlFrente(poli);
                    }
                    System.out.println();
                    System.out.print("RESPUESTA PRODUCTO:  ");
                    producto(poli1.primerNodo, poli2.primerNodo, cantidad, cantidad2);
                    System.out.println();
                    break;
                case 4:
                    salir = 0;
                    break;
                default:
            }
        } while (salir != 0);
    }
 
 
    public static void producto(Nodo poli1, Nodo poli2, int cantidad, int cantidad2) {
 
        ListaSE guardap1 = new ListaSE();//GUARDA MI LISTA CON LOS ELEMNTOS MULTIPLICADOS
        //PARA DESPUES SUMAR
        Nodo a;
        Nodo b;
 
        if (cantidad > cantidad2) {
 
            a = poli1;
            b = poli2;
        } else {
 
            a = poli2;
            b = poli1;
        }
 
        int cont = 0;
 
        while (a != null) {
            while (b != null) {
                int coeficiente = a.poli.getCoeficiente() * b.poli.getCoeficiente();
                int exponente = a.poli.getExponente() + b.poli.getExponente();
                Polinomio poli = new Polinomio(coeficiente, exponente);
                guardap1.insertarAlFrente(poli);
                b = b.siguiente;
                cont++;
            }
            if (cantidad > cantidad2) {
                b = poli2;
            } else {
                b = poli1;
            }
            a = a.siguiente;
        }
        ListaSE vacio = new ListaSE();
 
        Nodo ver = guardap1.primerNodo;
 
        while (ver != null) {
            if (ver.poli.getExponente() == 0) {
                ver.poli.setExponente(-1);
            }
            ver = ver.siguiente;
        }
        sumaPolinomios(guardap1.primerNodo, vacio.primerNodo, cont, 0);
    }
 
 
    public static void derivadaPolinomios(Nodo derivada) {
        Nodo p = derivada;
        int acum = 0;
        int r = 0;
        int cont = 0;
        while (p != null) {
            acum = p.poli.getCoeficiente() * p.poli.getExponente();
            r = p.poli.getExponente();
            r--;
            if (cont == 0) {
                if (r > 0 && acum > 0)
                    System.out.print(acum + " X ^  " + r + "   ");
 
                if (r > 0 && acum < 0)
                    System.out.print(acum + " X ^  " + r + "   ");
 
                if (r == 0)
                    System.out.print(acum + "  ");
 
                if (r < 0)
                    System.out.print(" 0 ");
            }
 
 
            if (cont > 0) {
                if (r > 0 && acum > 0)
                    System.out.print(" + " + acum + " X ^  " + r + "   ");
 
                if (r > 0 && acum < 0)
                    System.out.print(acum + " X ^  " + r + "   ");
 
                if (r == 0 && acum > 0)
                    System.out.print(" + " + acum + "  ");
                if (r == 0 && acum < 0)
                    System.out.print(acum + "  ");
 
                if (r < 0)
                    System.out.print(" +0 ");
 
            }
            cont++;
            acum = 0;
            r = 0;
            p = p.siguiente;
        }
    }
 
 
    public static void sumaPolinomios(Nodo poli1, Nodo poli2, int cantidad, int cantidad2) {
        int[] numerosl1 = new int[cantidad];
        int[] numerosl2 = new int[cantidad2];
 
        //PRIMER POLINOMIO
        int acomp = 0;
        Nodo p = poli1;
        while (p != null) {
            numerosl1[acomp] = p.poli.getExponente();
            acomp++;
            p = p.siguiente;
        }
 
        //SEGUNDO POLINOMIO
        int acomp2 = 0;
        Nodo q = poli2;
        while (q != null) {
            numerosl2[acomp2] = q.poli.getExponente();
            acomp2++;
 
            q = q.siguiente;
        }
 
        //DESDE AQUI EMPIEZA LA PROGRAMACION EN SUMA
        int[] mipoli = new int[100];// AQUI VA A GUARDARCE MI POLINOMIO
        int aux = 0;//AQUI SE VA ALMACENAR MI MAYOR NUMERO
        int vueltas = cantidad + cantidad2;
        int cambios1 = 0, cambios2 = 0;
 
        for (int j = 0; j < cantidad; j++) {
            mipoli[j] = numerosl1[j];
 
        }
        for (int j = 0; j < cantidad2; j++) {
            mipoli[cantidad++] = numerosl2[j];
        }
 
        //HEMOS ELIMINADO LOS REPETIDOS
        int ver2 = 0;
        for (int i = 0; i < vueltas; i++) {
            for (int j = 0; j < vueltas; j++) {
                if (mipoli[i] == mipoli[j])
                    ver2++;
 
                if (ver2 == 2) {
                    mipoli[j] = 0;
                    ver2--;
                }
 
            }
            ver2 = 0;
 
        }
 
        //VAMOS HA DEJAR SOLO CON LOS EXPONENTES CONTAR LOS CEROS
        int ccero = 0;
        for (int i = 0; i < vueltas; i++) {
            if (mipoli[i] == 0) {
                ccero++;
            }
        }
        int actual = vueltas - ccero;
        int[] vpolinomio = new int[actual];
        int cvpoli = 0;
        for (int i = 0; i < vueltas; i++) {
            if (mipoli[i] != 0) {
                vpolinomio[cvpoli] = mipoli[i];
                cvpoli++;
            }
        }
 
        //ORDENAMOS
        ordBurbuja(vpolinomio, cvpoli);
 
        int d = 0;
        acomp = vpolinomio.length;
        while (acomp != 0) {
            int acum = 0;
            Nodo a = poli1;
            Nodo b = poli2;
 
            int ver = 0;
            boolean pass = false;
            boolean pass2 = false;
 
            while (ver < 2) {
                if (!pass) {
                    if (a != null) {
                        if (vpolinomio[d] == a.poli.getExponente()) {
                            acum += a.poli.getCoeficiente();
                        }
                        a = a.siguiente;
                    } else {
                        ver++;
                        pass = true;
                    }
                }
 
                if (!pass2) {
                    if (b != null) {
                        if (vpolinomio[d] == b.poli.getExponente()) {
                            acum += b.poli.getCoeficiente();
                        }
                        b = b.siguiente;
                    } else {
                        ver++;
                        pass2 = true;
                    }
                }
            }
            if (acomp != 0) {
                if (d == 0) {
                    if (vpolinomio[d] != -1 && acum > 0) {
                        System.out.print(acum + " X ^ " + vpolinomio[d] + "    ");
                    } else if (acum < 0 && vpolinomio[d] != -1) {
                        System.out.print(acum + " X ^ " + vpolinomio[d] + "    ");
                    }
                    if (vpolinomio[d] == -1)
                        System.out.print(acum + "    ");
                } else if (d > 0) {
                    if (vpolinomio[d] != -1 && acum > 0)
                        System.out.print(" + " + acum + " X ^ " + vpolinomio[d] + "    ");
                    else if (acum < 0 && vpolinomio[d] != -1)
                        System.out.print(acum + " X ^ " + vpolinomio[d] + "    ");
                    if (vpolinomio[d] == -1 && acum > 0)
                        System.out.print(" + " + acum + "    ");
                    else if (vpolinomio[d] == -1 && acum < 0)
                        System.out.print(acum + "    ");
                }
            }
 
            d++;
            acomp--;
        }
 
    }
 
    static void ordBurbuja(int a[], int n) {
        boolean interruptor = true;
        for (int pasada = 0; pasada < n - 1 && interruptor; pasada++) {
            interruptor = false;
            for (int j = 0; j < n - pasada - 1; j++)
                if (a[j] < a[j + 1]) {
                    int aux;
                    interruptor = true;
                    aux = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = aux;
                }
 
        }
    }
}
------------------------------------------------------------------------------------------------------------------------>
/*Esta es la lista*/
public class ListaSE {
	public Nodo primerNodo;
	public Nodo ultimoNodo;
 
	public ListaSE() {
		primerNodo = ultimoNodo = null;
	}
 
	public void insertarAlFrente(Polinomio est) {
		if (estaVacia())
			primerNodo = ultimoNodo = new Nodo(est);
		else
			primerNodo = new Nodo(est, primerNodo);
	}
 
 
	public boolean estaVacia() {
		return primerNodo == null;
	}
 
	public void insertarAlFinal(Polinomio est) {
		if (estaVacia())
			primerNodo = ultimoNodo = new Nodo(est);
		else
			ultimoNodo = ultimoNodo.siguiente = new Nodo(est);
 
	}
 
------------------------------------------------------------------------------------------------------------------------------------>
/*Este es el Nodo*/
public class Nodo {
    Polinomio poli;
    Nodo siguiente;
 
    public Nodo(Polinomio x) {
        this.poli = x;
        siguiente = null;
    }
    public Nodo(Polinomio x, Nodo nuevoelemento) {
        this.poli = x;
        siguiente = nuevoelemento;
    }
    Polinomio getPolinomio() {
        return this.poli;
    }
 
    Nodo getsiguientet() {
        return siguiente;
    }
}
--------------------------------------------------------------------------------------------------------------------------->
/*Este es el polinomio*/
public class Polinomio {
	private int coeficiente;
	private int exponente;
 
 
	public Polinomio(){
		this.coeficiente=0;
		this.exponente=0;
 
 
	}
 
 
	public Polinomio(int co, int ex){
		setPolinomio(co, ex);
 
	}
 
	public void setPolinomio(int coeficiente,int exponente){
		this.coeficiente=coeficiente;
		this.exponente=exponente;
	}
 
	public int getExponente(){
		return this.exponente;
	}
 
	public void setExponente(int ex){
		this.exponente=ex;
	}
	public int getCoeficiente(){
		return this.coeficiente;
	}
 
}
//Fin
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
-1
Comentar