Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: Java | Size: 2.78 KB | Hits: 36 | Expires: Never
Copy text to clipboard
  1. import java.util.Scanner;
  2. import java.util.Stack;
  3.  
  4. public class Calculator {      
  5.         public static boolean priority(String a, String b){
  6.                 int x,y;
  7.                 if(a.equals("+") || a.equals("-")) x = 1;
  8.                 else if(a.equals("*") || a.equals("/")) x = 2;
  9.                 else x = 0;
  10.                
  11.                 if(b.equals("+") || b.equals("-")) y = 1;
  12.                 else if(b.equals("*") || b.equals("/")) y = 2;
  13.                 else y = 0;
  14.                
  15.                 if(x<y)return true;
  16.                 else return false;
  17.         }
  18.        
  19.         public static String infixToPostfix(String ex){
  20.                 Scanner sc = new Scanner(ex);
  21.                 StringBuffer sb = new StringBuffer();
  22.                 Stack<String> operators = new Stack<String>();
  23.                
  24.                 String token;
  25.                 while(sc.hasNext()){
  26.                         token = sc.next();
  27.                        
  28.                         /*if(operators.isEmpty())System.out.println("empty");
  29.                         else{
  30.                                 for(String str : operators){
  31.                                         System.out.print(" "+str);
  32.                                 }
  33.                                 System.out.println();
  34.                         }*/
  35.                        
  36.                         try{
  37.                                 Integer.parseInt(token);
  38.                                 sb.append(" "+token+" ");
  39.                                
  40.                         } catch (NumberFormatException e){                                     
  41.                                 if(operators.isEmpty()) operators.push(token);
  42.                                 else{
  43.                                         if(token.equals(")")){
  44.                                                 while(true){
  45.                                                         if(operators.peek().equals("(")){
  46.                                                                 operators.pop();
  47.                                                                 break;
  48.                                                         }
  49.                                                         else sb.append(operators.pop());
  50.                                                 }
  51.                                         }
  52.                                         else if(token.equals("(")) operators.push(token);
  53.                                         else if(priority(operators.peek(), token)){
  54.                                                 operators.push(token);
  55.                                         }
  56.                                         else{
  57.                                                 sb.append(" "+operators.pop()+" ");
  58.                                                 operators.push(token);
  59.                                         }
  60.                                        
  61.                                 }
  62.                         }
  63.                 }
  64.                 while(!operators.isEmpty()){
  65.                         sb.append(" "+operators.pop()+" ");
  66.                 }
  67.                
  68.                 return sb.toString();
  69.         }
  70.        
  71.         public static float calculate(float a, float b, String o){
  72.                 if(o.equals("+"))return a+b;
  73.                 else if(o.equals("-"))return b-a;
  74.                 else if(o.equals("/"))return b/a; //nevem tocno zakaj mora bit tukaj obrnjeno :P
  75.                 else return a*b;
  76.         }
  77.        
  78.         public static float evaluate(String ex){
  79.                 float result=0;
  80.                 Stack<Float> calc = new Stack<Float>();
  81.                
  82.                 ex = infixToPostfix(ex);
  83.                 Scanner sc = new Scanner(ex);
  84.                 String token;
  85.                
  86.                 while(sc.hasNext()){
  87.                         token = sc.next();
  88.                         try{
  89.                                 calc.push(Float.parseFloat(token));
  90.                         } catch (NumberFormatException e){                                     
  91.                                 calc.push(calculate(calc.pop(), calc.pop(), token));
  92.                         }
  93.                 }
  94.                 result = calc.pop();
  95.                
  96.                 return result;
  97.         }
  98.        
  99.         public static void main(String[] args){
  100.                
  101.                 Scanner sc = new Scanner(System.in);
  102.                 String input = "";
  103.                
  104.                 System.out.println("Navodila: Dovoljeni operatorji so +,-,*,/.\nOperande in operatorje locite s presledki.\nZa izhod vpisite \"x\".");
  105.                 while(true){
  106.                         System.out.print("Izraz: ");
  107.                         input = sc.nextLine();
  108.                         if(input.equals("x"))break;
  109.                         try{
  110.                                
  111.                         System.out.println("Rezultat: "+evaluate(input));
  112.                         } catch(Exception e){
  113.                                 System.out.println("Napaka v sintaksi, poskusite ponovno.");
  114.                         }
  115.                 }
  116.         }
  117. }