Anda di halaman 1dari 8

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package projetoandreia;
import EstrurasDeDados.*;
/**
*
* @author u14418
*/
public class ConvertePosFixa {
//converte para a forma pos fixada
public static Fila convertePosFixa(String e) throws Exception{
String s="";
Fila f = new Fila(e.length()*2);
Pilha p = new Pilha(e.length()*2);
char c;
// controla o que empilha e o que desempilha de acordo com a ordem
matematica
for (int i=0; i<e.length(); i++){
c=e.charAt(i);
if (c == '('){
p.empilhar(c);
}else
if (c == ')'){
while (p.consultaTopo() != '(')
f.enfileirar(p.desempilhar());
p.desempilhar();
}else
if (c == '*'){
p.empilhar(c);
}else
if (c == '/'){
p.empilhar(c);
}else
if (c == '+'){
if(p.pilhaVazia() || p.consultaTopo()=='+' || p.consulta
Topo()=='-'){
p.empilhar(c);
}else{
while(p.consultaTopo()=='*' || p.consultaTopo()=='/'
){
f.enfileirar(p.desempilhar());
}
p.empilhar(c);
}
}else
if (c == '-'){
if(p.pilhaVazia() || p.consultaTopo()=='+' || p.consulta
Topo()=='-'){
p.empilhar(c);
}else{
while(p.consultaTopo()=='*' || p.consultaTopo()=='/'
){
f.enfileirar(p.desempilhar());
}
p.empilhar(c);
}

}else f.enfileirar(c);
}
//coloca todos os simbolos na fila pos fixada
while(!p.pilhaVazia()) {
f.enfileirar(p.desempilhar());
}
return f;
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package projetoandreia;
import EstrurasDeDados.Pilha;
/**
*
* @author u14418
*/
public class EstaBalanceada {
public static boolean estaBalanceada(String E) throws Exception{
Pilha p = new Pilha(E.length());
for(int i=0; i<E.length(); i++){
if( E.charAt(i) == '('){
p.empilhar(i);
}
if( E.charAt(i) == ')'){
p.desempilhar();
}

}
return p.pilhaVazia();
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package projetoandreia;
import EstrurasDeDados.Fila;
/**
*
* @author u14418
*/
public class ImprimePosFixa {
public static String imprimePosFixa (Fila f) throws Exception{
String expressao = "";
Fila aux = f.clonar();
char c;
while (!aux.filaVazia()){
c = (char)aux.consultaPrimeiro();
aux.desenfileirar();
expressao += (char)c;
}
return expressao;
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package projetoandreia;
/*import EstrurasDeDados.*;
import static projetoandreia.ConvertePosFixa.convertePosFixa;
import static projetoandreia.EstaBalanceada.estaBalanceada;
import static projetoandreia.ImprimePosFixa.imprimePosFixa;
import static projetoandreia.ResultadoPilha.resultadoPilha;
import static projetoandreia.SalvaFilaNumerica.salvaFilaNumerica;
import static projetoandreia.TransformaEmLetra.transformaEmLetra;
*/
/**
*
* @author u14418
*/
public class ProjetoAndreia {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {

/*

Calculadora calc = new Calculadora();


calc.setVisible(true);
// TODO code application logic here
String expressao = "10+((5+(9-8))*2)/4";
String aux="";
char transformaChar;
int j=0;
Fila f = new Fila(100);
Fila filaNumerica = new Fila(100);
Pilha resultado = new Pilha(100);
/* String[] parteNum = expressao.split("[\\D]");//todos nao digitos
String[] parteDigito = expressao.split("[\\d]");//todos digitos
String devolvida = expressao.replaceAll("[^\\d]", " ");
System.out.println(devolvida);
*/ // 47 = / | 40 = ( | 41 = ) | 42 = * | 43 = + | 45 = - |
/* f.enfileirar(i);
System.out.println(f);*/
//verifica se os parenteses estao balanceados
/* if(estaBalanceada(expressao)){
System.out.println("Est balanceada");
//chama a funo que transforma a expressao em infixa

filaNumerica = salvaFilaNumerica(expressao);
System.out.println("TESTE " + filaNumerica);

aux =transformaEmLetra(expressao);
System.out.println("aux "+aux);

//converce char to int pra enfileirar


for (int i=0; i<aux.length();i++){
transformaChar = aux.charAt(i);
f.enfileirar((int)transformaChar);
}
System.out.println(f);
// fila posfixa
f = (convertePosFixa(aux));
System.out.print(f);
// string pos fixa
String pos = imprimePosFixa(f);
System.out.println(pos);
resultado = resultadoPilha(pos, f,filaNumerica);
System.out.println(resultado);
}
else System.out.println("No est balanceada");*/
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package projetoandreia;
import EstrurasDeDados.*;
/**
*
* @author u14418
*/
public class ResultadoPilha {
public static String resultadoPilha (String e, Fila f, Fila num) throws Exc
eption {
Pilha p = new Pilha(e.length()*2);
int num1,num2;
int i=0;
char caracter;
String aux = "";
while(i < e.length()){
//verifica se simbolo
if(!f.filaVazia()){
caracter = (char)f.desenfileirar();
if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(caracter)> -1){
p.empilhar(num.desenfileirar());
}else if (caracter=='+') {
num1 = p.desempilhar();
num2 = p.desempilhar();
p.empilhar(num1+num2);
}else if (caracter=='-'){
num1 = p.desempilhar();
num2 = p.desempilhar();
p.empilhar(num2-num1);
}else if (caracter=='*') {
num1 = p.desempilhar();
num2 = p.desempilhar();
p.empilhar(num1*num2);
}else if (caracter=='/'){
num1 = p.desempilhar();
num2 = p.desempilhar();
p.empilhar(num2/num1);
}
}
i++;
}
aux = p.toString();
return aux;
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package projetoandreia;
import EstrurasDeDados.Fila;
/**
*
* @author u14418
*/
public class SalvaFilaNumerica {
public static Fila salvaFilaNumerica (String e) throws Exception{
Fila f_numerica = new Fila(e.length()*2);
String aux="";
int i=0;
String expressao = e.replaceAlla("[^\\d]", "+");
for (char c : expressao.toCharArray()){
if (c!='+'){
aux+=c;
}else
if (!aux.isEmpty()){
f_numerica.enfileirar(Integer.parseInt(aux));
aux="";
}
}
if (!aux.isEmpty())
f_numerica.enfileirar(Integer.parseInt(aux));
return f_numerica;
}
}

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package projetoandreia;
/**
*
* @author u14418
*/
public class TransformaEmLetra {
public static String transformaEmLetra (String E) throws Exception{
char letra='A';
String expressao= "";
int i=0;
while(i < E.length()){
//verifica se simbolo
if ("0123456789".indexOf(E.charAt(i))==-1){
expressao += (E.charAt(i));
}// verifica se nmero
else if ((i==0) || ("0123456789".indexOf(E.charAt(i-1))==-1) ){
expressao += letra;
letra++;
}
i++;
}
return expressao;
}
}

Anda mungkin juga menyukai