-
Notifications
You must be signed in to change notification settings - Fork 0
/
Postfijo.java
148 lines (125 loc) · 4.01 KB
/
Postfijo.java
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
package final_MC;
import javax.swing.JOptionPane;
public class Postfijo{
String aEvaluar;
private StackLE<Double> evaluando;
private QueueLE<Character> postfijo;
private StackLE<Character> pila;
private String expRegular = "";
public Postfijo(String expresion){
//imprime en pantalla solución
this.aEvaluar=expresion;
this.postfijo = new QueueLE<Character>();
this.pila = new StackLE<Character>();
this.evaluando = new StackLE<Double>();
parsing(this.aEvaluar);
}
public void parsing(String expresion){
int size= this.aEvaluar.length();
char chSiguiente;
for(int i=0; i<size;i++){
//this.postfijo.enqueue(this.aEvaluar.charAt(i));
char ch=this.aEvaluar.charAt(i);
//revisa si es \n
if(ch == '\\'){
this.postfijo.enqueue(ch);
this.postfijo.enqueue(this.aEvaluar.charAt(i+1));
i++;
}
//si es un * + o caracter de alfabeto meterlo directamente a cola
if(ch != '(' && ch != ')' && ch !=',' && ch != '\\'){
this.postfijo.enqueue(ch);
//revisa que esté dentro del rango el siguiente char y que el actual sea algo del alfabeto
if((i < size-1) && (ch != '*' || ch != '+')){
chSiguiente=this.aEvaluar.charAt(i+1);
//que el siguiente sea algo del alfabeto
if(chSiguiente != ')' && chSiguiente != '*' && chSiguiente != '+' && chSiguiente != ','){
//Checar si ya hay u & en la pila, tienen misma prioridad, sacar uno
if(!this.pila.isEmpty() && this.pila.top() == '&'){
this.postfijo.enqueue(this.pila.pop());
}
this.pila.push('&');
}
}
}
//si es un '(' a la pila
if(ch=='('){
this.pila.push(ch);
}
if(ch==','){
//Si está vacía la pila o hay un '(' poner operador
if(this.pila.isEmpty() || this.pila.top()=='('){
this.pila.push(ch);
}
else{
//si no está vacía, prioridades
if(ch==',' && this.pila.top()==','){
//System.out.println("lo que entra es de la misma prioridad");
this.postfijo.enqueue(this.pila.pop());
this.pila.push(ch);
}
else if(ch==',' && this.pila.top()=='&'){
//System.out.println("lo que está en la pila tiene mayor prioridad, sacarlos");
//se saca todo
while(!this.pila.isEmpty() && this.pila.top()!='('){
this.postfijo.enqueue(this.pila.pop());
}
this.pila.push(ch);
}
}
}
if(ch==')'){
try{
while(this.pila.top()!='('){
this.postfijo.enqueue(this.pila.pop());
}
//se deshace del '(' que queda en la pila
this.pila.pop();
//revisa que esté dentro del rango el siguiente char
if(i < size-1 ){
chSiguiente=this.aEvaluar.charAt(i+1);
if(chSiguiente != ')' && chSiguiente != '*' && chSiguiente != '+' && chSiguiente != ','){
//Checar si ya hay u & en la pila, tienen misma prioridad, sacar uno
if(!this.pila.isEmpty() && this.pila.top() == '&'){
this.postfijo.enqueue(this.pila.pop());
}
this.pila.push('&');
}
}
}
catch(StackVaciaException e){
throw new StackVaciaException("No se encontró un '(' de apertura");
}
}
}
while(!this.pila.isEmpty() && this.pila.top()!='('){
this.postfijo.enqueue(this.pila.pop());
}
if(!this.pila.isEmpty() && this.pila.top()=='('){
throw new StackVaciaException("No se encontró un ')' de cierre");
}
System.out.println("Expresión en postfijo: "+this.postfijo);
pasarPostfijo(this.postfijo);
}
public String pasarPostfijo( QueueLE<Character> cola){
for(int i=0;i<cola.size();i++){
expRegular+=cola.getDatos().elementoEn(i);
}
//System.out.println(expRegular);
return expRegular;
}
public String getER(){
return this.expRegular;
}
/*public static void main(String[] args) {
String expresion = "";
try{
expresion = JOptionPane.showInputDialog("Escriba expresión a evaluar: ");
System.out.println("Expresión a evaluar: "+expresion);
Postfijo exp = new Postfijo(expresion);
}
catch(NullPointerException e){
System.out.println("Se cerró el programa");
}
}*/
}