Skip to content

Commit a59f446

Browse files
committed
added infix evalutaion in java
1 parent 9327403 commit a59f446

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import java.util.*;
2+
abstract class InfixEvaluation
3+
{
4+
/**
5+
* INFIX EVALUTION
6+
* it will only work when expression has spaces in it
7+
* it will throw exception when no bracket is balanced
8+
*
9+
* test cases
10+
* 2 + 3 => 5
11+
* ( 3 + 3 ) * 2 => 12
12+
**/
13+
public static void main(String[] args)
14+
{
15+
Scanner in = new Scanner(System.in);
16+
System.out.println("Enter the expression with spaces");
17+
String expr = in.nextLine(); //input string expression
18+
in.close();
19+
String[] arr = expr.split(" "); //parsing the string expression
20+
21+
Stack<Integer> valstack = new Stack<Integer>(); //create the value/operand stack
22+
Stack<Character> opstack = new Stack<Character>(); //create the operator stack
23+
24+
for(int i=0;i<arr.length;i++) //traversing the exp
25+
{
26+
if(isopr(arr[i])) //if the arr[i] string is operator
27+
{
28+
if(arr[i].equals("(")) //if left paranthesis found
29+
{
30+
char ch = arr[i].charAt(0);
31+
opstack.push(ch); //push left paranthesis to operator stack
32+
}
33+
else if(arr[i].equals(")")) //if right paranthesis found
34+
{
35+
while(opstack.peek()!='(')
36+
{
37+
char ch = opstack.pop(); //pop one operator
38+
int a = valstack.pop(); //pop first operand
39+
int b = valstack.pop(); //pop second operand
40+
int result=solve(a,b,ch); //solve the small expression
41+
valstack.push(result); //push result to value stack
42+
}
43+
opstack.pop();
44+
}
45+
else //other operator * - + / ^
46+
{
47+
while(!opstack.empty() && precedence(opstack.peek().toString())>=precedence(arr[i]))
48+
{
49+
char ch = opstack.pop(); //pop one operator
50+
int a = valstack.pop(); //pop first operand
51+
int b = valstack.pop(); //pop second operand
52+
int result=solve(a,b,ch); //solve the small expression
53+
54+
valstack.push(result); //push result to value stack
55+
}
56+
opstack.push(arr[i].charAt(0)); //push this operator to operator stack
57+
}
58+
}
59+
else //if number found
60+
{
61+
valstack.push(Integer.parseInt(arr[i])); //push to value stack
62+
}
63+
}
64+
65+
while(!opstack.empty())
66+
{
67+
char ch = opstack.pop(); //pop one operator
68+
int a = valstack.pop(); //pop first operand
69+
int b = valstack.pop(); //pop second operand
70+
int result=solve(a,b,ch); //solve the small expression
71+
valstack.push(result); //push result to value stack
72+
}
73+
System.out.println(valstack.peek()); //final value present in the value stack
74+
}
75+
76+
/**
77+
* it will solve the expression
78+
*
79+
* @param a for first operand
80+
* @param b for second operand
81+
* @param ch for operator
82+
*
83+
* @return int the result of the epxression
84+
**/
85+
public static int solve(int a,int b,char ch)
86+
{
87+
switch(ch)
88+
{
89+
case '+':
90+
return a+b;
91+
case '-':
92+
return a-b;
93+
case '*':
94+
return a*b;
95+
case '/':
96+
return a/b;
97+
case '^':
98+
return (int)Math.pow(a, b);
99+
default:
100+
return -1;
101+
}
102+
}
103+
104+
/**
105+
* it will tell the precedence of operators
106+
*
107+
* @param str for operator in string format
108+
*
109+
* @return the precedence value
110+
**/
111+
public static int precedence(String str)
112+
{
113+
switch(str)
114+
{
115+
case "+":
116+
case "-":
117+
return 1;
118+
case "*":
119+
case "/":
120+
return 2;
121+
case "^":
122+
return 3;
123+
default:
124+
return -1;
125+
}
126+
}
127+
128+
/**
129+
* it will tell wheter the string is operator or not
130+
*
131+
* @param str for operator in string format
132+
*
133+
* @return boolean
134+
**/
135+
public static boolean isopr(String str)
136+
{
137+
switch(str)
138+
{
139+
case "+":
140+
case "-":
141+
case "*":
142+
case "/":
143+
case "(":
144+
case ")":
145+
case "^":
146+
return true;
147+
default:
148+
return false;
149+
}
150+
}
151+
}

Java/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* [Stack class](Data-Structures/STACKS/INBUILT-STACK/Stacks.java)
6161
* MISC STACKS
6262
* [Given expression have balanced brackets or not](Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java)
63+
* [Infix Evaluation of mathematical expression](Data-Structures/STACKS/MISC-STACKS/InfixEvaluation.java)
6364

6465
#### QUEUES
6566

datastructures.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ Indexer for Data Structures Lover
237237
* [JAVA](Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java)
238238
* complexity
239239

240+
#### Infix Evaluation
241+
242+
* blog
243+
* docs
244+
* implementation
245+
* [JAVA](Java/Data-Structures/STACKS/MISC-STACKS/InfixEvaluation.java)
246+
* complexity
247+
240248
#### Postfix Evaluation
241249

242250
* blog

0 commit comments

Comments
 (0)