Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack;
- import java.util.Scanner;
- /* * * * * * * * * * * * * * * * * * * * * * * * * *
- *
- * Description: Evaluation Expression (with parens)
- * Author : Marco 'RootkitNeo' C.
- * License : GNU/GPL V.3
- * * * * * * * * * * * * * * * * * * * * * * * * * *
- */
- class EvaluateExpression {
- private EvaluateExpression() {}
- // Verifica se il token passato e' un numero
- // @return true if it's a number; false otherwise
- // (work with negative number)
- // --------------------------------------------------------------------------------
- private static boolean isNumeric(String str)
- {
- return str.matches("-?\\d+(\\.\\d+)?");
- }
- // --------------------------------------------------------------------------------
- // Verifica la priorita' degli operatori
- // @return true if token1 has a higher priority than token2
- // --------------------------------------------------------------------------------
- private static boolean isHightPriority(String token1, String token2)
- {
- switch(token2)
- {
- case "^":
- if(!token1.equals("^")) return true;
- else return false;
- case "*":
- case "/":
- if(token1.equals("+") || token1.equals("-")) return true;
- else return false;
- case "+":
- case "-":
- return false;
- }
- return false;
- }
- // ---------------------------------------------------------------------------------
- // Parsing Infix Expression
- // @return Postfix Expression
- // ---------------------------------------------------------------------------------
- private static String evaluate(String exp)
- {
- Stack<String> stack = new Stack<String>();
- StringBuilder sb = new StringBuilder();
- Scanner scanner = new Scanner(exp);
- scanner.useDelimiter(" ");
- while(scanner.hasNext())
- {
- String token = scanner.next();
- if(isNumeric(token))
- {
- sb.append(token);
- sb.append(" ");
- }
- else if(!token.equals("(") && !token.equals(")"))
- {
- if(!stack.isEmpty() && isHightPriority(token,stack.peek()))
- {
- sb.append(stack.pop());
- sb.append(" ");
- }
- stack.push(token);
- }
- else if(token.equals("("))
- {
- stack.push(token);
- }
- else if(token.equals(")"))
- {
- String e;
- while(!(e = stack.pop()).equals("("))
- {
- sb.append(e);
- sb.append(" ");
- }
- }
- }
- while(!stack.isEmpty()) sb.append(stack.pop());
- return sb.toString().trim();
- }
- // -----------------------------------------------------------------------------------
- // Parsing Postfix expression
- // @return number (result)
- // -----------------------------------------------------------------------------------
- public static double calculate(String exp)
- {
- Stack<Double> stack = new Stack<Double>();
- Scanner scanner = new Scanner(evaluate(exp));
- scanner.useDelimiter(" ");
- while(scanner.hasNext())
- {
- String item = scanner.next();
- switch(item)
- {
- case "*":
- stack.push(stack.pop()*stack.pop());
- break;
- case "/":
- stack.push(stack.pop()/stack.pop());
- break;
- case "+":
- stack.push(stack.pop()+stack.pop());
- break;
- case "-":
- stack.push(stack.pop()-stack.pop());
- break;
- case "^": // Non funziona proprio correttamente in alcuni casi, ma l'ho aggiunta comunque
- double f = stack.pop();
- double s = stack.pop();
- stack.push(Math.pow(s,f));
- break;
- default:
- stack.push(Double.parseDouble(item));
- }
- }
- return stack.pop();
- }
- // -------------------------------------------------------------------------------------
- }
- class TestExp {
- public static void main(String[] args) {
- // This is the correct format of the input string
- System.out.println(EvaluateExpression.calculate("( 1 + 3 * ( 4 ^ 2 ) + ( ( 2 + 3 + 5 ) * 2 + 2 * 4 ) * 2 ) ^ 2"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement