Sie sind auf Seite 1von 12

EXPRESIN POSTFIJA.

package evaluarExpresion;
import TipoPila.*;
public class ExprePostfija
{
public static String postFija(String exprg! t"ro#s Exception
{
Pila$ista pila;
String e;
%"aracter at;
c"ar&'expsion;
c"ar c";
boolean (esapila;
int n ) *; ++conta(or (e expresi,n en postfija

if (- vali(o(exprg!!
t"ro# ne# Exception(.%ar/cter no v/li(o en una expresi,n.!;
e ) exprg.to0pper%ase(!; ++ operan(os en ma12sculas
expsion ) ne# c"ar&e.lengt"(!';
pila ) ne# Pila$ista(!;
for (int i ) *; i 3 e.lengt"(!; i44!
{
c" ) e.c"ar5t(i!;
++ an/lisis (el elemento
if (operan(o(c"!!
{
expsion&n44' ) c";
6
else if (c" -) 7!7! ++ es un opera(or
{
(esapila ) true;
#"ile ((esapila!
{
c"ar ope%ima ) 7 7;
if (-pila.pila8acia(!!
{
at ) (%"aracter! pila.cimaPila(!;
ope%ima ) at.c"ar8alue(!;
6
if (pila.pila8acia(! 99
(pr(a(Fuera(c"! : pr(a(;entro(ope%ima!!!
{
pila.insertar(ne# %"aracter(c"!!;
(esapila ) false;
6
else if (pr(a(Fuera(c"! 3) pr(a(;entro(ope%ima!!
{
at ) (%"aracter! pila.<uitar(!;
expsion&n44' ) at.c"ar8alue(!;
6
6
6
else ++ es un 7!7
{
at ) (%"aracter!pila.<uitar(!;
(o{
expsion&n44' ) at.c"ar8alue(!;
at ) (%"aracter!pila.<uitar(!;
6#"ile (at.c"ar8alue(! -) 7(7!;
6
6
+*
se vuelca los opera(ores <ue <ue(an en la pila 1 se pasan a
la expresi,n.
*+
#"ile (- pila.pila8acia(!!
{
at ) (%"aracter!pila.<uitar(!;
expsion&n44' ) at.c"ar8alue(!;
6
return ne# String(expsion= *= n!; ++ expresi,n en postfija
6
private static int pr(a(;entro(c"ar op!
{
int p(p ) >?;
s#itc" (op!
{
case 7@7A
p(p ) B;
break;
case 7*7 A case 7+7A
p(p ) C;
break;
case 747 A case 7>7A
p(p ) ?;
break;
case 7(7 A
p(p ) *;
6
return p(p;
6
++ priori(a( (el opera(or en la expresi,n infija
private static int pr(a(Fuera(c"ar op!
{
int pfp ) >?;
s#itc" (op!
{
case 7@7 A
pfp ) D;
break;
case 7*7 A case 7+7A
pfp ) C;
break;
case 747 A case 7>7A
pfp ) ?;
break;
case 7(7 A
pfp ) E;
6
return pfp;
6
private static boolean vali(o(String expresion!
{
boolean s# ) true;
for (int i ) *; (i 3 expresion.lengt"(!! FF s#; i44!
{
c"ar c;
c ) expresion.c"ar5t(i!;
s# ) s# FF (
(c :) 757 FF c 3) 7G7! 99
(c :) 7a7 FF c 3) 7H7! 99
(c))7@7 99 c))7+7 99 c))7*7 99
c))747 99 c))7>7 99
c))7(7 99 c))7!7!
!;
6
return s#;
6
public static boolean operan(o(c"ar c!
{
return (c :) 757 FF c 3) 7G7!;
6
6
EVALUAR UNA EXPRESIN ALGEBRAICA.
package evaluarExpresion;
import TipoPila.*;
import java.util.*;
import java.io.*;
public class EvalPostfija
{
public static (ouble
evalua(String postFija= (ouble v&'!t"ro#s Exception
{
Pila$ista pila;
(ouble valor ) >?.*;
(ouble a= b;
int i;
pila ) ne# Pila$ista(!;
for (i ) *; i 3 postFija.lengt"(!; i44!
{
c"ar op;
op ) postFija.c"ar5t(i!;
if (ExprePostfija.operan(o(op!! ++ es un operan(o
{
int in(ice;
in(ice ) op > 757;
pila.insertar(ne# ;ouble(v&in(ice'!!;
6
else ++ es un opera(or
{
;ouble op%ima;
op%ima ) (;ouble! pila.<uitar(!;
b ) op%ima.(ouble8alue(!;
op%ima ) (;ouble! pila.<uitar(!;
a ) op%ima.(ouble8alue(!;
s#itc" (op!
{
case 7@7A valor ) Iat".po#(a=b!;
break;
case 7*7A valor ) a * b;
break;
case 7+7A if (b -) *.*!
valor ) a + b;
else
t"ro# ne# Exception(.;ivisi,n por cero..!;
break;
case 747A valor ) a 4 b;
break;
case 7>7A valor ) a J b;
6
pila.insertar(ne# ;ouble(valor!!;
6
6
return ((;ouble!pila.<uitar(!!.(ouble8alue(!;
6
public static voi(
valorpr(os(String postFija= (ouble v&'! t"ro#s Exception
{
++ asignan valores numKricos a los operan(os
int i;
c"ar c";
Luffere(Mea(er entra(a ) ne# Luffere(Mea(er(
ne# NnputStreamMea(er(S1stem.in!!;
for (i ) *; i 3 postFija.lengt"(!; i44!
{
c"ar op;
op ) postFija.c"ar5t(i!;

if (ExprePostfija.operan(o(op!! ++ es un operan(o
{
int in(ice;
;ouble (;
in(ice ) op > 757;
S1stem.out.print(op 4 . ) .!;
( ) ;ouble.valuef(entra(a.rea($ine(!!;
v&in(ice' ) (.(ouble8alue(!;
6
6
6
6
PROGRAMA PARA EVALUAR UNA EXPRESIN ALGEBRAICA.
import java.io.*;
import evaluarExpresion.*;
class EjemploEvaluaExpresion
{
public static voi( main(String &' a!
{
(ouble &'v ) ne# (ouble&CO';
(ouble resulta(o;
Luffere(Mea(er entra(a ) ne# Luffere(Mea(er(
ne# NnputStreamMea(er(S1stem.in!!;
String expresion;
tr1 {
S1stem.out.print(.PnExpresi,n aritmKticaA .!;
expresion ) entra(a.rea($ine(!;
++ %onversi,n (e infija a postfija
expresion ) ExprePostfija.postFija(expresion!;
S1stem.out.println(.PnExpresi,n en postfijaA . 4 expresion!;
++ Evaluaci,n (e la expresi,n
EvalPostfija.valorpr(os(expresion= v!; ++ valor (e operan(os
resulta(o ) EvalPostfija.evalua(expresion= v!;
S1stem.out.println(.Mesulta(o ) . 4 resulta(o!;
6
catc" (Exception e!
{
S1stem.out.println(.PnError en el proceso ... . 4 e!;
e.getStackTrace(!;
6
6
6
EJERCICIO 9.1
// clase PilaLieal
import java.io.*;
class Pila$ineal
{
private static final int T5IPN$5 ) QR;
private int cima;
private bject &'listaPila;
public Pila$ineal(!
{
cima ) >?;
listaPila ) ne# bject&T5IPN$5';
6
public voi( insertar(bject elemento!t"ro#s Exception
{
if (pila$lena(!!
{
t"ro# ne# Exception(.;esbor(amiento pila.!;
6
cima44;
listaPila&cima' ) elemento;
6

public bject <uitar(! t"ro#s Exception
{
bject aux;
if (pila8acia(!!
{
t"ro# ne# Exception (.Pila vacSa= no se pue(e extraer..!;
6
aux ) listaPila&cima';
cima>>;
return aux;
6
public bject cimaPila(! t"ro#s Exception
{
if (pila8acia(!!
{
t"ro# ne# Exception (.Pila vacSa= no se pue(e extraer..!;
6
return listaPila&cima';
6
public boolean pila8acia(!
{
return cima )) >?;
6
public boolean pila$lena(!
{
return cima )) T5IPN$5>?;
6
public voi( limpiarPila(!
{
cima ) >?;
6
6
public class Palin(romo
{
public static voi( main(String &' a!
{
Pila$ineal pila%"ar;
c"ar c";
boolean esPal;
String pal;
Luffere(Mea(er entra(a ) ne# Luffere(Mea(er(
ne# NnputStreamMea(er(S1stem.in!!;
tr1 {
pila%"ar ) ne# Pila$ineal(!; ++ crea pila vacSa
S1stem.out.print(.Teclea la palabra. 4
. a verificar si es palSn(romoA .!;
pal ) entra(a.rea($ine(!;
++ se crea la pila con los caracteres (e la palabra
for (int i ) *; i 3 pal.lengt"(!; !
{
%"aracter c;
c ) ne# %"aracter(pal.c"ar5t(i44!!;
pila%"ar.insertar(c!;
6
++ se comprueba si es palSn(romo
esPal ) true;
for (int j ) *; esPal FF -pila%"ar.pila8acia(!; !
{
%"aracter c;
c ) (%"aracter! pila%"ar.<uitar(!;
esPal ) pal.c"ar5t(j44! )) c.c"ar8alue(!;
6
pila%"ar.limpiarPila(!;
if (esPal!
S1stem.out.println(.$a palabra . 4 pal 4
. es un palSn(romo Pn.!;
else
S1stem.out.println(.$a palabra . 4 pal 4
. no es un palSn(romo Pn.!;
6
catc" (Exception er!
{
S1stem.err.println(.ExcepcionA . 4 er!;
6
6
6
A!a"#a$% 9.&' Clase PilaVec#%"
package TipoPila;
import java.util.8ector;
public class Pila8ector
{
private static final int NTN%N5$ ) ?R;
private int cima;
private 8ector listaPila;
public Pila8ector(!
{
cima ) >?;
listaPila ) ne# 8ector(NTN%N5$!;
6
public voi( insertar(bject elemento!t"ro#s Exception
{
cima44;
listaPila.a((Element(elemento!;
6

public bject <uitar(! t"ro#s Exception
{
bject aux;
if (pila8acia(!!
{
t"ro# ne# Exception (.Pila vacSa= no se pue(e extraer..!;
6
aux ) listaPila.element5t(cima!;
listaPila.removeElement5t(cima!;
cima>>;
return aux;
6
public bject cimaPila(! t"ro#s Exception
{
if (pila8acia(!!
{
t"ro# ne# Exception (.Pila vacSa= no se pue(e extraer..!;
6
return listaPila.element5t(cima!;
6
public boolean pila8acia(!
{
return cima )) >?;
6
public voi( limpiarPila(!t"ro#s Exception
{
#"ile (- pila8acia(!!
<uitar(!;
6
6
EJEMPLO 9.(
import java.io.*;
import TipoPila.*;
class EjemploPila;inamica
{
public static voi( main(String &' a!
{
Pila8ector pila ) ne# Pila8ector(!;
int x;
Luffere(Mea(er entra(a ) ne# Luffere(Mea(er(
ne# NnputStreamMea(er(S1stem.in!!;
S1stem.out.println(.Teclea n2mero (e elementosA .!;
tr1 {
x ) Nnteger.parseNnt(entra(a.rea($ine(!!;
for (int j ) ?; j 3) x; j44!
{
;ouble (;
( ) ;ouble.valuef(entra(a.rea($ine(!!;
pila.insertar((!;
6
++ vacia(o (e la pila
S1stem.out.println(.Elementos (e la PilaA .!;
#"ile (-pila.pila8acia(!!
{
;ouble (;
( ) (;ouble!pila.<uitar(!;
if ((.(ouble8alue(! : *.*!
S1stem.out.print(( 4 . .!;
6
6
catc" (Exception er!
{
S1stem.err.println(.ExcepcionA . 4 er!;
6
6
6
A!a"#a$% 9.(' Clase PilaLis#a
PILA LISTA
package TipoPila;
public class To(oPila
{
bject elemento;
To(oPila siguiente;
To(oPila(bject x!
{
elemento ) x;
siguiente ) null;
6
6
package TipoPila;
public class Pila$ista
{
private To(oPila cima;

public Pila$ista(!
{
cima ) null;
6
public boolean pila8acia(!
{
return cima )) null;
6
public voi( insertar(bject elemento!
{
To(oPila nuevo;
nuevo ) ne# To(oPila(elemento!;
nuevo.siguiente ) cima;
cima ) nuevo;
6
public bject <uitar(! t"ro#s Exception
{
if (pila8acia(!!
t"ro# ne# Exception (.Pila vacSa= no se pue(e extraer..!;
bject aux ) cima.elemento;
cima ) cima.siguiente;
return aux;
6
public bject cimaPila(! t"ro#s Exception
{
if (pila8acia(!!
t"ro# ne# Exception (.Pila vacSa= no se pue(e leer cima..!;
return cima.elemento;
6
public voi( limpiarPila(!
{
To(oPila t;
#"ile(-pila8acia(!!
{
t ) cima;
cima ) cima.siguiente;
t.siguiente ) null;
6
6
6