Advertisement
Guest User

Untitled

a guest
Dec 16th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 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.             s = sc.nextLine();
  47.             System.out.println("Hello from evalte123rm");
  48.             s = s.trim();
  49.                 if (s== null)
  50.                {
  51.                
  52.                
  53.                
  54.                 System.out.println("The input is" + token);
  55.                 result = evalexpr();
  56.                 keepgoing = false;
  57.                 continue;
  58.                }
  59.            }
  60.        
  61.       return s;
  62.     }
  63.  
  64.  
  65.  
  66.     double evalterm()
  67.     {
  68.        double a = evalfactor();
  69.        token = peektoken();
  70.            System.out.println("Hello from evalterm");
  71.         if((token.equals("*")) || (token.equals("/")))
  72.         {
  73.             System.out.println("Hello from evalterm if");
  74.              operator = token;
  75.              cursor++;
  76.              double b = evalfactor();
  77.                 if ("*".equals(operator))
  78.                 {  
  79.                  a=a*b;
  80.            
  81.                 }else{
  82.            
  83.                  a=a/b;
  84.            }
  85.         }
  86.        return a;
  87.     }
  88.    
  89.     double evalexpr()
  90.     {
  91.         double x = evalterm();
  92.         System.out.println("Hello from evalexpr");
  93.         token = peektoken();
  94.             double z;
  95.        
  96.         if((token.equals("+")) || (token.equals("-")))
  97.         {
  98.             operator = token;
  99.             cursor++;
  100.             double y = evalterm();
  101.                 if (operator == "+")
  102.                 z = x + y;
  103.                 else
  104.                 z = x - y;
  105.         }
  106.         return x;
  107.     }
  108.    
  109.    
  110.     double evalfactor(){
  111.    
  112.         token = peektoken();
  113.         System.out.println("Hello from evalfactor");
  114.         if(token.equals("("))
  115.         {
  116.             cursor++;
  117.             double K = evalexpr();
  118.            System.out.println("Hello from evalfactor if");
  119.         }
  120.        
  121.         else{
  122.        
  123.         cursor++;
  124.         double r = Double.parseDouble(token);
  125.        
  126. }
  127.         return x;
  128.     }
  129.  
  130.     private String peektoken() {
  131.         {
  132.         String j = "";
  133.         System.out.println("Hello from str peektoken");
  134.         while(cursor<expr.length())
  135.         {
  136.             j = expr.substring(cursor,cursor+1);
  137.            
  138.             if(j.matches("[0-9]"))
  139.             {
  140.             return j;
  141.             }
  142.             if(j.equals(" "))
  143.         {
  144.             cursor++;
  145.             System.out.println("Hello from peektoken if");
  146.             continue;
  147.         }
  148.         if(j.matches("[+-*/]"))
  149.         {
  150.         return j;
  151.         }
  152.         System.out.println("Error");
  153.         return j;
  154.         }
  155.     return null;
  156.     }  
  157.  
  158.     }
  159.     String token(){
  160.        
  161.         System.out.println("Hello from str token");
  162.        
  163.     }
  164.  
  165.    
  166.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement