luishenriique

Notação Polonesa

Jun 26th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Polonesa {
  4.  
  5.     private static Scanner input;
  6.  
  7.     static String converterEq (String eqTrad){
  8.         Stack<Character> pilha = new Stack<Character>();
  9.         String eqPolski = "";
  10.         int i = 0;
  11.         while(i < eqTrad.length()){
  12.             char aux = eqTrad.charAt(i);
  13.             if((aux >= '0' && aux <= '9') || (aux >= 'a' && aux <= 'z')){
  14.                 eqPolski += aux;
  15.             }
  16.             if(aux == '+' || aux == '-' || aux == '*' || aux == '/'){
  17.                 while(!pilha.empty() && (pilha.peek() > aux)){
  18.                     eqPolski += pilha.pop();
  19.                 }
  20.                 pilha.push(aux);
  21.             }
  22.             if(aux == '('){
  23.                 pilha.push(aux);
  24.             }
  25.             if(aux == ')'){
  26.                 while(pilha.peek() != '(' ){
  27.                     eqPolski += pilha.pop();
  28.                 }
  29.                 pilha.pop();
  30.             }
  31.             i++;
  32.         }
  33.         while(!pilha.empty()){
  34.             eqPolski += pilha.pop();
  35.         }
  36.         return (eqPolski);
  37.     }
  38.    
  39.     public static void main(String[] args) {
  40.         input = new Scanner(System.in);
  41.         String eqTrad;
  42.         System.out.print("Digite a equação: ");
  43.         eqTrad = input.next();
  44.         System.out.println("Equação Polonesa: "+ converterEq (eqTrad));
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment