Beruflich Dokumente
Kultur Dokumente
ALGORITHMS
OUTPUT:
OUTPUT:
OUTPUT:
Evaluate Infix Expression using stacks
1. import java.util.Scanner;
2. import java.util.Stack;
3.
4. public class evaluateInfix {
5.
6. public static void main(String[] args) {
7.
8. Scanner scn = new Scanner(System.in);
9.
10. String input = scn.next();
11.
12. System.out.println(evaluateInfixExpr(input));
13. }
14.
15. public static String infixtopostfix(String str) {
16.
17. // Containing the expression
18. str = '(' + str + ')';
19.
20. Stack<String> st = new Stack<>();
21. StringBuilder sb = new StringBuilder("");
22.
23. for (int i = 0; i < str.length();) {
24. char c = str.charAt(i);
25.
26. if (Character.isDigit(c)) {
27. StringBuilder num = new StringBuilder("");
28. while (i < str.length() && Character.isDigit(str.charAt(i))) {
29. num.append(str.charAt(i));
30. i++;
31. }
32. sb.append(num + ",");
33. } else if (c == '(') {
34. st.push("" + c);
35. i++;
36. } else if (c == ')') {
37. while (st.size() != 0) {
38. if (st.peek().compareTo("(") == 0) {
39. st.pop();
40. break;
41. }
42. sb.append(st.pop() + ",");
43. }
44. i++;
45. } else {
46. // If an operator is the first thing in your expression, or
47. // another operator comes after it, or comes after a left
48. // parenthesis, then it's an unary operator.
49. char op;
50.
51. if (i == 0 || str.charAt(i - 1) == '(' || isOperator(str.charAt(i -
1) + "") != 0) {
52. op = '.';
53. } else {
54. op = str.charAt(i);
55. }
56.
57. if (op == '^') {
58. while (st.isEmpty() == false && getPriority(st.peek().charAt(0)
) > getPriority(op)) {
59. sb.append(st.pop() + ",");
60. }
61. st.push(op + "");
62. } else {
63. while (st.isEmpty() == false && getPriority(st.peek().charAt(0)
) >= getPriority(op)) {
64. sb.append(st.pop() + ",");
65. }
66. st.push(op + "");
67. }
68.
69. i++;
70.
71. }
72.
73. }
74.
75. return sb.toString();
76. }
77.
78. public static int evaluateInfixExpr(String str) {
79. String postfixExpression = infixtopostfix(str);
80.
81. String[] expr = postfixExpression.split(",");
82.
83. Stack<Integer> st = new Stack<>();
84.
85. for (int i = 0; i < expr.length; i++) {
86. String curr = expr[i];
87. if (isOperator(expr[i]) == 0) {
88. st.add(Integer.parseInt(expr[i]));
89. } else {
90. if (expr[i].charAt(0) == '.') {
91. st.push(-1 * st.pop());
92. } else {
93. int b = st.pop();
94. int a = st.pop();
95. st.push(operate(a, b, expr[i].charAt(0)));
96. }
97.
98. }
99. }
100. return st.peek();
101. }
102.
103. public static int operate(int a, int b, char op) {
104. switch (op) {
105. case '+': {
106. return a + b;
107. }
108. case '-': {
109. return a - b;
110. }
111. case '*': {
112. return a * b;
113. }
114. case '/': {
115. return a / b;
116. }
117. case '^': {
118. return (int) Math.pow(a, b);
119. }
120. case '%': {
121. return a % b;
122. }
123. }
124. return 0;
125. }
126.
127. public static int getPriority(char c) {
128.
129. switch (c) {
130. case '(':
131. return 0;
132. case '+':
133. case '-':
134. return 1;
135. case '*':
136. case '%':
137. case '/':
138. return 2;
139. case '^':
140. return 3;
141. case '.':
142. return 4;
143. }
144.
145. return 0;
146. }
147.
148. public static int isOperator(String s) { // return 0 if not operator and
2
149. // for operators
150.
151. if (s.compareTo("+") == 0 || s.compareTo("-
") == 0 || s.compareTo("*") == 0 || s.compareTo("/") == 0
152. || s.compareTo("%") == 0 || s.compareTo("^") == 0 || s.compa
reTo(".") == 0) {
153. return 2;
154. }
155.
156. return 0;
157. }
158.
159. }
OUTPUT: