Created
March 25, 2017 18:08
-
-
Save VictorSouzas/e21aec287e4fb5ab21c6cbfeab4cb758 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
*create by victor_souzas@yahoo.com.br | |
*/ | |
public class InfixaPosfixa implements InfixaPosfixaInterface { | |
public static void main(String[] args) { | |
InfixaPosfixa inf = new InfixaPosfixa(); | |
Scanner scan = new Scanner(System.in); | |
System.out.println("Entre a expressao"); | |
String expressao = scan.nextLine(); | |
inf.converte(expressao); | |
System.out.println(inf.imprimir()); | |
} | |
private PilhaDinamica pilha; | |
private String saida; | |
public InfixaPosfixa(){ | |
pilha = new PilhaDinamica(); | |
saida = ""; | |
} | |
@Override | |
public int verificaPrecedencia(char valor) { | |
if (valor == '+' || valor == '-') | |
return 0; | |
return 1; | |
} | |
@Override | |
public boolean verificaOperadores(char valor) { | |
char[] op = {'+','-','*','/'}; | |
for (int i = 0; i < op.length; i++){ | |
if (valor == op[i]) | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public void verificaParentesis(char valor) { | |
if(valor == '(') { | |
pilha.push(valor); | |
return; | |
} | |
while(!(pilha.isEmpty())){ | |
char desempilhado = (char) pilha.pop(); | |
if (desempilhado == '(') | |
break; | |
saida += desempilhado; | |
} | |
} | |
@Override | |
public void operadores(char valor) { | |
if(pilha.isEmpty()) { | |
pilha.push(valor); | |
return; | |
} | |
while (!(pilha.isEmpty())){ | |
char desempilhado = (char) pilha.pop(); | |
if(desempilhado == '(') { | |
pilha.push(desempilhado); | |
break; | |
} | |
if (verificaPrecedencia(desempilhado) < verificaPrecedencia(valor)) | |
pilha.push(desempilhado); | |
if (verificaPrecedencia(desempilhado) >= verificaPrecedencia(valor)) | |
saida += desempilhado; | |
if (verificaPrecedencia(valor) >= verificaPrecedencia(desempilhado)) | |
break; | |
} | |
pilha.push(valor); | |
} | |
@Override | |
public void converte(String valor) { | |
for (int i = 0; i < valor.length(); i++){ | |
char vl = valor.charAt(i); | |
if(vl == ' ') | |
continue; | |
if (vl == ')' || vl == '('){ | |
verificaParentesis(vl); | |
continue; | |
} | |
if(verificaOperadores(vl)){ | |
operadores(vl); | |
continue; | |
} | |
saida += vl; | |
} | |
} | |
@Override | |
public String imprimir() { | |
while (!(pilha.isEmpty())){ | |
saida += pilha.pop(); | |
} | |
return saida; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by victor_souzas@yahoo.com.br on 14/03/17. | |
*/ | |
public class PilhaDinamica<T> { | |
private No top; | |
public PilhaDinamica(){ | |
top = null; | |
} | |
public boolean isEmpty(){ | |
return top == null; | |
} | |
public void push(T item ){ | |
No novoNo = new No(item); | |
novoNo.next = top; | |
this.top = novoNo; | |
} | |
public void display(){ | |
No next = top; | |
do { | |
System.out.print(next.value); | |
next = next.next; | |
} while (next != null); | |
} | |
public T peek(){ | |
return (T) top; | |
} | |
public T pop(){ | |
T returnTop = (T) top.value; | |
top = top.next; | |
return returnTop; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by ubuntu on 14/03/17. | |
*/ | |
public class No<T> { | |
public T value; | |
public No next; | |
public No(T value) { | |
this.value = value; | |
this.next = null; | |
} | |
public void display(){ | |
System.out.println(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment