forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicCalculatorUsingStack.java
More file actions
90 lines (78 loc) · 2.87 KB
/
BasicCalculatorUsingStack.java
File metadata and controls
90 lines (78 loc) · 2.87 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
package com.thealgorithms.stacks;
import java.util.Stack;
/**
* Utility class to evaluate a basic arithmetic expression containing
* non-negative integers, '+', '-', parentheses '(', ')', and spaces.
*
* <p>The evaluation is done using two stacks:
* one for operands and one for operators.
*
* <p>This class cannot be instantiated.</p>
*/
public final class BasicCalculatorUsingStack {
private BasicCalculatorUsingStack() {
}
/**
* Evaluates a mathematical expression.
*
* @param expression the input expression
* @return the evaluated result
* @throws IllegalArgumentException if the expression is null or empty
*/
public static int evaluate(String expression) {
if (expression == null || expression.isEmpty()) {
throw new IllegalArgumentException("Expression must not be null or empty.");
}
Stack<Integer> operands = new Stack<>();
Stack<Character> operators = new Stack<>();
for (int i = 0; i < expression.length(); i++) {
char current = expression.charAt(i);
if (current == ' ') {
continue;
}
if (Character.isDigit(current)) {
int number = 0;
while (i < expression.length() && Character.isDigit(expression.charAt(i))) {
number = number * 10 + (expression.charAt(i) - '0');
i++;
}
i--;
operands.push(number);
} else if (current == '(') {
operators.push(current);
} else if (current == ')') {
while (!operators.isEmpty() && operators.peek() != '(') {
applyOperation(operands, operators);
}
operators.pop(); // remove '('
} else if (current == '+' || current == '-') {
if (isUnaryMinus(expression, i)) {
operands.push(0);
}
while (!operators.isEmpty() && operators.peek() != '(') {
applyOperation(operands, operators);
}
operators.push(current);
}
}
while (!operators.isEmpty()) {
applyOperation(operands, operators);
}
return operands.pop();
}
private static void applyOperation(Stack<Integer> operands, Stack<Character> operators) {
int b = operands.pop();
int a = operands.pop();
char operator = operators.pop();
operands.push(operator == '+' ? a + b : a - b);
}
private static boolean isUnaryMinus(String expression, int index) {
int j = index - 1;
while (j >= 0 && expression.charAt(j) == ' ') {
j--;
}
return j < 0 || expression.charAt(j) == '('
|| expression.charAt(j) == '+'
|| expression.charAt(j) == '-';
}
}