Advertisement
Guest User

Untitled

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