Recent Posts
None | 20 sec ago
C++ | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
Lua | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 9th of Feb 2010 09:33:01 PM Download | Raw | Embed | Report
  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 x = 2;
  9.                
  10.                 if(b.equals("+") || b.equals("-"))y = 1;
  11.                 else y = 2;
  12.                
  13.                 if(x<y)return true;
  14.                 else return false;
  15.         }
  16.        
  17.         public static String infixToPostfix(String ex){
  18.                 Scanner sc = new Scanner(ex);
  19.                 StringBuffer sb = new StringBuffer();
  20.                 Stack<String> operators = new Stack<String>();
  21.                
  22.                 String token;
  23.                 while(sc.hasNext()){
  24.                         token = sc.next();
  25.                         try{
  26.                                 Integer.parseInt(token);
  27.                                 sb.append(" "+token+" ");
  28.                                
  29.                         } catch (NumberFormatException e){                                     
  30.                                 if(operators.isEmpty()) operators.push(token);
  31.                                 else{
  32.                                         if(priority(operators.peek(), token)){
  33.                                                 operators.push(token);
  34.                                         }
  35.                                         else{
  36.                                                 sb.append(" "+operators.pop()+" ");
  37.                                                 operators.push(token);
  38.                                         }
  39.                                        
  40.                                 }
  41.                         }
  42.                 }
  43.                 while(!operators.isEmpty()){
  44.                         sb.append(" "+operators.pop()+" ");
  45.                 }
  46.                
  47.                 return sb.toString();
  48.         }
  49.        
  50.         public static float calculate(float a, float b, String o){
  51.                 if(o.equals("+"))return a+b;
  52.                 else if(o.equals("-"))return a-b;
  53.                 else if(o.equals("/"))return b/a; //nevem tocno zakaj mora bit tukaj obrnjeno :P
  54.                 else return a*b;
  55.         }
  56.        
  57.         public static float evaluate(String ex){
  58.                 float result=0;
  59.                 Stack<Float> calc = new Stack<Float>();
  60.                
  61.                 ex = infixToPostfix(ex);
  62.                 Scanner sc = new Scanner(ex);
  63.                 String token;
  64.                
  65.                 while(sc.hasNext()){
  66.                         token = sc.next();
  67.                         try{
  68.                                 calc.push(Float.parseFloat(token));
  69.                         } catch (NumberFormatException e){                                     
  70.                                 calc.push(calculate(calc.pop(), calc.pop(), token));
  71.                         }
  72.                 }
  73.                 result = calc.pop();
  74.                
  75.                 return result;
  76.         }
  77.        
  78.         public static void main(String[] args){
  79.                 Scanner sc = new Scanner(System.in);
  80.                 String input = "";
  81.                
  82.                 System.out.println("Navodila: Dovoljeni operatorji so +,-,*,/.\nOperande in operatorje locite s presledki.\nZa izhod vpisite \"x\".");
  83.                 do{
  84.                         System.out.print("Izraz: ");
  85.                         System.out.println("Rezultat: "+evaluate(sc.nextLine()));
  86.                 }while(!input.equals("x"));
  87.         }
  88. }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: