-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathHandler.java
More file actions
180 lines (161 loc) · 5.56 KB
/
MathHandler.java
File metadata and controls
180 lines (161 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* File: MathHandler.java
* ---------------------
* This class is a helper class for a sample calculator app implementation
* Author: Cobalt mkc
* Date created: July 22, 2019
* Last modified: Aug 3, 2022
*/
package exercise2;
import java.util.*;
public class MathHandler {
public static int evaluate(int operand1, int operand2, char operator) {
// No modifications needed here!!!
int result;
switch (operator) {
case '+':
result = add(operand1, operand2);
break;
case '-':
result = subtract(operand1, operand2);
break;
case 'x':
result = multiply(operand1, operand2);
break;
case '÷':
result = divide(operand1, operand2);
break;
default:
result = 0;
}
return result;
}
public static double evaluate(double operand1, double operand2, char operator) {
// No modifications needed here!!!
double result;
switch (operator) {
case '+':
result = add(operand1, operand2);
break;
case '-':
result = subtract(operand1, operand2);
break;
case 'x':
result = multiply(operand1, operand2);
break;
case '÷':
result = divide(operand1, operand2);
break;
default:
result = 0;
}
return result;
}
public static int add(int operand1, int operand2) {
return operand1 + operand2;
}
public static double add(double operand1, double operand2) {
return operand1 + operand2;
}
public static int subtract(int operand1, int operand2) {
return operand1 - operand2;
}
public static double subtract(double operand1, double operand2) {
return operand1 - operand2;
}
public static int divide(int operand1, int operand2) {
if (operand2 == 0) {
throw new IllegalArgumentException("Cannot divide by zero");
}
return operand1 / operand2;
}
public static double divide(double operand1, double operand2) {
if (operand2 == 0.0) {
throw new IllegalArgumentException("Cannot divide by zero");
}
return operand1 / operand2;
}
public static int multiply(int operand1, int operand2) {
return operand1 * operand2;
}
public static double multiply(double operand1, double operand2) {
return operand1 * operand2;
}
public static double evaluate(String expression) {
List<String> rpn = infixToRPN(expression);
return evaluateRPN(rpn);
}
private static List<String> infixToRPN(String expression) {
List<String> output = new ArrayList<>();
Stack<Character> operators = new Stack<>();
int i = 0;
while (i < expression.length()) {
char token = expression.charAt(i);
if (Character.isDigit(token) || token == '.') {
StringBuilder number = new StringBuilder();
while (i < expression.length() && (Character.isDigit(expression.charAt(i)) || expression.charAt(i) == '.')) {
number.append(expression.charAt(i++));
}
output.add(number.toString());
} else if (token == '(') {
operators.push(token);
i++;
} else if (token == ')') {
while (!operators.isEmpty() && operators.peek() != '(') {
output.add(String.valueOf(operators.pop()));
}
operators.pop(); // pop '('
i++;
} else if (isOperator(token)) {
while (!operators.isEmpty() && precedence(operators.peek()) >= precedence(token)) {
output.add(String.valueOf(operators.pop()));
}
operators.push(token);
i++;
} else {
i++;
}
}
while (!operators.isEmpty()) {
output.add(String.valueOf(operators.pop()));
}
return output;
}
private static boolean isOperator(char token) {
return token == '+' || token == '-' || token == 'x' || token == '÷';
}
private static int precedence(char operator) {
switch (operator) {
case 'x':
case '÷':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
private static double evaluateRPN(List<String> rpn) {
Stack<Double> stack = new Stack<>();
for (String token : rpn) {
if (isNumber(token)) {
stack.push(Double.parseDouble(token));
} else if (isOperator(token.charAt(0))) {
double operand2 = stack.pop();
double operand1 = stack.pop();
double result = evaluate(operand1, operand2, token.charAt(0));
stack.push(result);
}
}
return stack.pop();
}
private static boolean isNumber(String token) {
try {
Double.parseDouble(token);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}