-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpemdascalc
More file actions
111 lines (96 loc) · 3.61 KB
/
pemdascalc
File metadata and controls
111 lines (96 loc) · 3.61 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
import java.util.Scanner;
import java.util.Stack;
public class PEMDASCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an equation: ");
String expression = scanner.nextLine();
try {
double result = calculate(expression);
System.out.println("Result: " + result);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
scanner.close();
}
public static double calculate(String input) throws Exception {
input = input.replaceAll(" ", "").toLowerCase();
if (!input.matches("^[0-9+\\-*/().^]*$")) {
throw new Exception("Invalid characters in the input.");
}
return evaluate(input);
}
private static double evaluate(String expression) throws Exception {
Stack<Double> operands = new Stack<>();
Stack<Character> operators = new Stack<>();
for (int i = 0; i < expression.length(); i++) {
char currentChar = expression.charAt(i);
if (currentChar >= '0' && currentChar <= '9') {
StringBuilder numBuilder = new StringBuilder();
while (i < expression.length() &&
(Character.isDigit(expression.charAt(i)) || expression.charAt(i) == '.')) {
numBuilder.append(expression.charAt(i));
i++;
}
i--;
operands.push(Double.parseDouble(numBuilder.toString()));
} else if (currentChar == '(') {
operators.push(currentChar);
} else if (currentChar == ')') {
while (!operators.isEmpty() && operators.peek() != '(') {
operands.push(applyOperator(operators.pop(), operands.pop(), operands.pop()));
}
operators.pop();
} else if (isOperator(currentChar)) {
while (!operators.isEmpty() && precedence(currentChar) <= precedence(operators.peek())) {
operands.push(applyOperator(operators.pop(), operands.pop(), operands.pop()));
}
operators.push(currentChar);
}
}
while (!operators.isEmpty()) {
operands.push(applyOperator(operators.pop(), operands.pop(), operands.pop()));
}
if (operands.size() == 1) {
return operands.pop();
} else {
throw new Exception("Invalid expression");
}
}
private static boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';
}
private static int precedence(char operator) {
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}
private static double applyOperator(char operator, double operand2, double operand1) {
switch (operator) {
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
case '*':
return operand1 * operand2;
case '/':
if (operand2 == 0) {
throw new ArithmeticException("Division by zero");
}
return operand1 / operand2;
case '^':
return Math.pow(operand1, operand2);
default:
return 0;
}
}
}