Advertisement
Guest User

Untitled

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