Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Calc {
- int rc;
- static int src;
- double result;
- double evalexpr;
- double m;
- String s;
- String token;
- String operator;
- String expr;
- String peektoken;
- int cursor = 0;
- public static void main(String[] args)
- {
- Calc x = new Calc();
- src = x.process();
- return;
- }
- private int process()
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter an expression");
- s = sc.nextLine();
- boolean keepgoing = true;
- while(keepgoing == true)
- {
- String j = sc.nextLine();
- j = j.trim();
- if (j== null)
- {
- keepgoing = false;
- System.out.println("The input is" + token);
- result = evalexpr();
- continue;
- }
- }
- return 0;
- }
- double evalterm()
- {
- double a = evalfactor();
- token = peektoken();
- System.out.println("Hello from evalterm");
- if((token.equals("*")) || (token.equals("/")))
- {
- System.out.println("Hello from evalterm if");
- operator = token;
- cursor++;
- double b = evalfactor();
- if ("*".equals(operator))
- {
- a=a*b;
- }else{
- a=a/b;
- }
- }
- return a;
- }
- double evalexpr()
- {
- double x = evalterm();
- System.out.println("Hello from evalexpr");
- token = peektoken();
- double z;
- if((token.equals("+")) || (token.equals("-")))
- {
- operator = token;
- cursor++;
- double y = evalterm();
- if (operator == "+")
- z = x + y;
- else
- z = x - y;
- }
- return x;
- }
- double evalfactor(){
- token = peektoken();
- System.out.println("Hello from evalfactor");
- if(token.equals("("))
- {
- cursor++;
- double K = evalexpr();
- System.out.println("Hello from evalfactor if");
- }
- else{
- cursor++;
- double r = Double.parseDouble(token);
- }
- return x;
- }
- private String peektoken() {
- {
- String j = "";
- System.out.println("Hello from str peektoken");
- while(cursor<expr.length())
- {
- j = expr.substring(cursor,cursor+1);
- if(j.matches("[0-9]"))
- {
- return j;
- }
- if(j.equals(" "))
- {
- cursor++;
- System.out.println("Hello from peektoken if");
- continue;
- }
- if(j.matches("[+-*/]"))
- {
- return j;
- }
- System.out.println("Error");
- return j;
- }
- return null;
- }
- }
- String token(){
- double evalterm();
- System.out.println("Hello from str token");
- return main;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement