Created
July 11, 2018 05:27
-
-
Save saytosiddiq/3574871abae20995741f33085fe8d2a4 to your computer and use it in GitHub Desktop.
This java code is an implementation of eval() method to solve complex expressions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package evaluation; | |
import java.math.BigInteger; | |
import java.util.EmptyStackException; | |
import java.util.Scanner; | |
import java.util.Stack; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptEngineManager; | |
import javax.script.ScriptException; | |
public class EvalPlus { | |
private static Scanner scanner = new Scanner(System.in); | |
public static void main(String[] args) { | |
System.out.println("This Evaluation is based on BODMAS rule\n"); | |
evaluate(); | |
} | |
private static void evaluate() { | |
StringBuilder finalStr = new StringBuilder(); | |
System.out.println("Enter an expression to evaluate:"); | |
/*Example expressions: | |
(4*5(3-1)(67+23(25!)(5*6.5%2.2))) | |
3+(4*5/3(2)(4)+45-1) | |
(89*5/3(2(4(3(2))))(4)+57-1) */ | |
String expr = scanner.nextLine(); | |
if(isProperExpression(expr)) { | |
expr = replaceBefore(expr); | |
char[] temp = expr.toCharArray(); | |
String operators = "(+-*/%)"; | |
for(int i = 0; i < temp.length; i++) { | |
if((i == 0 && temp[i] != '*') || (i == temp.length-1 && temp[i] != '*' && temp[i] != '!')) { | |
finalStr.append(temp[i]); | |
} else if((i > 0 && i < temp.length -1) || (i==temp.length-1 && temp[i] == '!')) { | |
if(temp[i] == '!') { | |
StringBuilder str = new StringBuilder(); | |
for(int k = i-1; k >= 0; k--) { | |
if(Character.isDigit(temp[k])) { | |
str.insert(0, temp[k] ); | |
} else { | |
break; | |
} | |
} | |
Long prev = Long.valueOf(str.toString()); | |
BigInteger val = new BigInteger("1"); | |
for(Long j = prev; j > 1; j--) { | |
val = val.multiply(BigInteger.valueOf(j)); | |
} | |
finalStr.setLength(finalStr.length() - str.length()); | |
finalStr.append("(" + val + ")"); | |
if(temp.length > i+1) { | |
char next = temp[i+1]; | |
if(operators.indexOf(next) == -1) { | |
finalStr.append("*"); | |
} | |
} | |
} else { | |
finalStr.append(temp[i]); | |
} | |
} | |
} | |
expr = finalStr.toString(); | |
if(expr != null && !expr.isEmpty()) { | |
ScriptEngineManager mgr = new ScriptEngineManager(); | |
ScriptEngine engine = mgr.getEngineByName("JavaScript"); | |
try { | |
System.out.println("Result: " + engine.eval(expr)); | |
evaluate(); | |
} catch (ScriptException e) { | |
System.out.println(e.getMessage()); | |
} | |
} else { | |
System.out.println("Please give an expression"); | |
evaluate(); | |
} | |
} else { | |
System.out.println("Not a valid expression"); | |
evaluate(); | |
} | |
} | |
private static String replaceBefore(String expr) { | |
expr = expr.replace("(", "*("); | |
expr = expr.replace("+*", "+").replace("-*", "-").replace("**", "*").replace("/*", "/").replace("%*", "%"); | |
return expr; | |
} | |
private static boolean isProperExpression(String expr) { | |
expr = expr.replaceAll("[^()]", ""); | |
char[] arr = expr.toCharArray(); | |
Stack<Character> stack = new Stack<Character>(); | |
int i =0; | |
while(i < arr.length) { | |
try { | |
if(arr[i] == '(') { | |
stack.push(arr[i]); | |
} else { | |
stack.pop(); | |
} | |
} catch (EmptyStackException e) { | |
stack.push(arr[i]); | |
} | |
i++; | |
} | |
return stack.isEmpty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment