Java - Necesito ayuda con este programa Pila de Infija a Posfija(no me muestra los operadores ni evalua)

 
Vista:

Necesito ayuda con este programa Pila de Infija a Posfija(no me muestra los operadores ni evalua)

Publicado por Riddle (2 intervenciones) el 09/09/2018 01:23:21
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
package TPilas;
 
public class TPilas {
 
	private Object pila[];
	private int max;
	private int tope;
 
 
	public TPilas(int i){
		this.max = max;
		tope = 0;
		pila = new Object [max];
	}
 
 
	public Object[] getPila() {
		return pila;
	}
 
 
	public void setPila(Object[] pila) {
		this.pila = pila;
	}
 
 
	public int getMax() {
		return max;
	}
 
 
	public void setMax(int max) {
		this.max = max;
	}
 
 
	public int getTope() {
		return tope;
	}
 
 
	public void setTope(int tope) {
		this.tope = tope;
	}
 
 
	public boolean pilaVacia() {
		return (tope==0);
	}
 
 
	public boolean pilaLlena() {
        return (tope==max);
	}
 
 
	public boolean meter(Object dato){
		if (pilaLlena()){
			return false;
		}
		pila[tope]=dato;
		tope++;
		return true;
	}
 
 
	public Object sacar(){
	    if(pilaVacia()){
	    	return null;
	    }
    	tope--;
    	return pila[tope];
	}
 
 
	protected Object mostrar(){
		Object Dato1 = null;
		if(!pilaVacia()){
            for(int Dato=(tope);Dato>=0;Dato--) {
                System.out.println(pila[Dato]);
            }
        }
		return Dato1;
	}
 
	public void limpiarPila(){
		System.out.println("\t=Pila Limpia=");
		tope = -1;
	}
 
 
	public Object UltimoElem(){
		return pila[tope-1];
	}
}
 
package TPilas;
 
public class EvaluarExpresion {
 
	public static double evaluarInfija(String infija){
		String posfija = convertir(infija);
		System.out.println("=La expresión posfija es: "+posfija);
		return evaluarPosfija(posfija);
	}
 
 
	public static String convertir(String infija){
		String posfija = "";
		TPilas pila = new TPilas(79);
		for (int i = 0; i < infija.length(); i++) {
			char letra = infija.charAt(i);
			if(esOperador(letra)) {
				if(pila.pilaVacia()) {
					pila.meter(letra);
				} else {
                    if(prioridadExpresion(letra) > prioridadPila((char)pila.UltimoElem())){
                            pila.meter(letra);
                    }else {
                        posfija = posfija + pila.sacar();
                        pila.meter(letra);
                    }
                }
            }else {
                posfija = posfija + letra;
            }
		}
		while(!pila.pilaVacia()) {
			posfija = posfija + pila.sacar();
		}
		return posfija;
	}
 
 
	public static int prioridadExpresion(char operador) {
		switch(operador) {
            case '^':
                return 4;
            case '*':
                return 2;
            case '/':
                return 2;
            case '+':
                return 1;
            case '-':
                return 1;
            case '(':
                return 5;
        }
		return 0;
	}
 
	public static int prioridadPila(char operador) {
		switch(operador) {
			case '^':
				return 4;
			case '*':
				return 2;
			case '/':
				return 2;
			case '+':
				return 1;
			case '-':
				return 1;
			case '(':
				return 5;
		}
		return 0;
	}
 
	public static double evaluarPosfija(String posfija) {
		return 0;
	}
 
 
	public static boolean esOperador(char letra) {
		if(letra == '*'|| letra == '/'|| letra == '+'
				|| letra == '-'|| letra == '('|| letra == ')' || letra == '^') {
			return true;
		}
		return false;
	}
 
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package TPilas;
 
import java.util.Scanner;
 
public class ConversionPosfija {
 
	public static void main (String [] args) {
 
		Scanner Lectura = new Scanner (System.in);
		System.out.print("=Digite la expresión que desea convertir a posfija: ");
		String infija = Lectura.next();
		System.out.print("=El resultado de evaluar la expresión infija es: "+EvaluarExpresion.evaluarInfija(infija));
	}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder