This commit is contained in:
2026-02-09 17:03:32 +01:00
parent af8cbf6768
commit 8e08785db0
6 changed files with 187 additions and 49 deletions

View File

@@ -0,0 +1,63 @@
package oving3;
import java.util.Stack;
public class RPNCalc {
private Stack<Double> stack = new Stack<>();
RPNCalc() {
}
void push(double n) {
stack.push(n);
}
double pop() {
if (stack.isEmpty()) {
return Double.NaN;
}
return stack.pop();
}
double peek(int i) {
int id = stack.size() - i;
if (id < 0 || id >= stack.size()) {
return Double.NaN;
}
return stack.get(id);
}
int getSize() {
return stack.size();
}
void performOperation(char c) {
if (stack.size() <= 1) {
return;
}
double a = stack.pop();
double b = stack.pop();
switch (c) {
case '+':
stack.push(a + b);
break;
case '-':
stack.push(a - b);
break;
case '*':
stack.push(a * b);
break;
case '/':
stack.push(a / b);
break;
default:
throw new IllegalArgumentException("Invalid operation");
}
}
@Override
public String toString() {
return stack.toString();
}
}