Advertisement
Guest User

Java code

a guest
Dec 13th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.util.*;
  2. public class Calculator {
  3.     int rc;
  4.     static int src;
  5.     double result;
  6.     String s;
  7.     String token;
  8.     String operator;
  9.     String expr;
  10.     String peektoken;
  11.     int cursor = 0;
  12.  
  13.  
  14.     public static void main(String[] args)
  15.     {
  16.      Calculator x = new Calculator();
  17.      src = x.process();
  18.  
  19.      return;
  20.     }
  21.  
  22.  
  23.  
  24.  
  25.     int process()
  26.     {
  27.         Scanner sc = new Scanner(System.in);
  28.         boolean keepgoing = true;
  29.         while(keepgoing == true)
  30.            {
  31.             String s = sc.nextLine();
  32.             s = s.trim();
  33.                 if (s== null)
  34.                {
  35.                 keepgoing = false;
  36.                 continue;
  37.                 System.out.println("The input is" + s);
  38.                 result = evalexpr(s);
  39.                }
  40.            }
  41.  
  42.       return 0;
  43.     }
  44.  
  45.  
  46.  
  47.     double evalterm()
  48.     {
  49.        double a = evalfactor();
  50.        token = peektoken();
  51.            
  52.         if((token.equals("*")) || (token.equals("/")))
  53.         {
  54.              operator = token;
  55.              cursor++;
  56.              double b = evalfactor();
  57.                 if (operator == "*")
  58.                 {  
  59.                  a=a*b;
  60.            
  61.                 }else{
  62.            
  63.                  a=a/b;
  64.            }
  65.         }
  66.        return a;
  67.     }
  68.    
  69.     double evalexpr()
  70.     {
  71.         double x = evalterm();
  72.         token = peektoken();
  73.        
  74.         if((token.equals("+")) || (token.equals("-")))
  75.         {
  76.             operator = token;
  77.             cursor++;
  78.             double y = evalterm();
  79.                 if (operator == "+")
  80.                 z = x + y;
  81.                 else
  82.                 z = x - y;
  83.         }
  84.         return x;
  85.     }
  86.    
  87.    
  88.     double evalfactor()
  89.     {
  90.         token = peektoken();
  91.         if(token.equals("("));
  92.         {
  93.             cursor++;
  94.             double K = evalexpr();
  95.            
  96.         return K;
  97.        
  98.         }
  99.         else
  100.         {
  101.         cursor++;
  102.         double m = Double.parseDouble(token);
  103.     }
  104.         return m;
  105.     }
  106.  
  107.    
  108.  
  109.     String peektoken()
  110.     {
  111.         String s = "";
  112.         while(cursor<expr.length())
  113.         {
  114.             s = expr.substring(cursor,cursor+1);
  115.            
  116.             if(s.matches("[0-9]"))
  117.         {
  118.             return s;
  119.         }
  120.             if(s.equals(" "))
  121.         {
  122.             cursor++;
  123.             continue;
  124.         }
  125.         if(s.matches("[+-*/]"))
  126.         {
  127.         return s;
  128.         }
  129.         System.out.println("Error");
  130.         return s;
  131.         }
  132.     }
  133. }
  134.  
  135.  
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement