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
+ }
0 commit comments