diff --git a/Java/Data-Structures/STACKS/MISC-STACKS/InfixEvaluation.java b/Java/Data-Structures/STACKS/MISC-STACKS/InfixEvaluation.java new file mode 100644 index 0000000..1a33102 --- /dev/null +++ b/Java/Data-Structures/STACKS/MISC-STACKS/InfixEvaluation.java @@ -0,0 +1,151 @@ +import java.util.*; +abstract class InfixEvaluation +{ + /** + * INFIX EVALUTION + * it will only work when expression has spaces in it + * it will throw exception when no bracket is balanced + * + * test cases + * 2 + 3 => 5 + * ( 3 + 3 ) * 2 => 12 + **/ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + System.out.println("Enter the expression with spaces"); + String expr = in.nextLine(); //input string expression + in.close(); + String[] arr = expr.split(" "); //parsing the string expression + + Stack valstack = new Stack(); //create the value/operand stack + Stack opstack = new Stack(); //create the operator stack + + for(int i=0;i=precedence(arr[i])) + { + char ch = opstack.pop(); //pop one operator + int a = valstack.pop(); //pop first operand + int b = valstack.pop(); //pop second operand + int result=solve(a,b,ch); //solve the small expression + + valstack.push(result); //push result to value stack + } + opstack.push(arr[i].charAt(0)); //push this operator to operator stack + } + } + else //if number found + { + valstack.push(Integer.parseInt(arr[i])); //push to value stack + } + } + + while(!opstack.empty()) + { + char ch = opstack.pop(); //pop one operator + int a = valstack.pop(); //pop first operand + int b = valstack.pop(); //pop second operand + int result=solve(a,b,ch); //solve the small expression + valstack.push(result); //push result to value stack + } + System.out.println(valstack.peek()); //final value present in the value stack + } + + /** + * it will solve the expression + * + * @param a for first operand + * @param b for second operand + * @param ch for operator + * + * @return int the result of the epxression + **/ + public static int solve(int a,int b,char ch) + { + switch(ch) + { + case '+': + return a+b; + case '-': + return a-b; + case '*': + return a*b; + case '/': + return a/b; + case '^': + return (int)Math.pow(a, b); + default: + return -1; + } + } + + /** + * it will tell the precedence of operators + * + * @param str for operator in string format + * + * @return the precedence value + **/ + public static int precedence(String str) + { + switch(str) + { + case "+": + case "-": + return 1; + case "*": + case "/": + return 2; + case "^": + return 3; + default: + return -1; + } + } + + /** + * it will tell wheter the string is operator or not + * + * @param str for operator in string format + * + * @return boolean + **/ + public static boolean isopr(String str) + { + switch(str) + { + case "+": + case "-": + case "*": + case "/": + case "(": + case ")": + case "^": + return true; + default: + return false; + } + } +} \ No newline at end of file diff --git a/Java/README.md b/Java/README.md index 7d7e7d8..97b3656 100644 --- a/Java/README.md +++ b/Java/README.md @@ -60,6 +60,7 @@ * [Stack class](Data-Structures/STACKS/INBUILT-STACK/Stacks.java) * MISC STACKS * [Given expression have balanced brackets or not](Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java) + * [Infix Evaluation of mathematical expression](Data-Structures/STACKS/MISC-STACKS/InfixEvaluation.java) #### QUEUES diff --git a/datastructures.md b/datastructures.md index 29b8835..82da2e0 100644 --- a/datastructures.md +++ b/datastructures.md @@ -237,6 +237,14 @@ Indexer for Data Structures Lover * [JAVA](Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java) * complexity +#### Infix Evaluation + +* blog +* docs +* implementation + * [JAVA](Java/Data-Structures/STACKS/MISC-STACKS/InfixEvaluation.java) +* complexity + #### Postfix Evaluation * blog