main
Obai Albek 2025-09-09 09:42:46 -04:00
parent 9b80c5bdc6
commit 481ff340cf
1 changed files with 17 additions and 21 deletions

View File

@ -16,8 +16,6 @@ public class Spreadsheet {
); );
public Spreadsheet(int rows, int cols) { public Spreadsheet(int rows, int cols) {
if (rows < 1 || rows > 99) throw new IllegalArgumentException("rows must be 1..99");
if (cols < 1 || cols > 26) throw new IllegalArgumentException("cols must be 1..26 (A..Z)");
this.rows = rows; this.rows = rows;
this.cols = cols; this.cols = cols;
@ -141,9 +139,9 @@ public class Spreadsheet {
} }
private void evaluateCell(int row, int col) throws NumberFormatException { private void evaluateCell(int row, int col) {
String f = cells[row][col].getFormula().trim(); String f = cells[row][col].getFormula().trim();
String result = ""; String result;
try { try {
if (f.toUpperCase().startsWith("SUMME(")) { if (f.toUpperCase().startsWith("SUMME(")) {
@ -272,14 +270,14 @@ public class Spreadsheet {
if (st.size() < 2) throw new IllegalArgumentException("Malformed expression"); if (st.size() < 2) throw new IllegalArgumentException("Malformed expression");
long b = st.pop(), a = st.pop(); long b = st.pop(), a = st.pop();
switch (t) { switch (t) {
case "+": st.push(a + b); break; case "+" -> st.push(a + b);
case "-": st.push(a - b); break; case "-" -> st.push(a - b);
case "*": st.push(a * b); break; case "*" -> st.push(a * b);
case "/": case "/" -> {
if (b == 0) throw new ArithmeticException("/0"); if (b == 0) throw new ArithmeticException("/0");
st.push(a / b); break; st.push(a / b);
case "^": }
st.push((long)Math.pow(a, b)); break; case "^" -> st.push((long)Math.pow(a, b));
} }
} else { } else {
st.push(parseLongStrict(t)); st.push(parseLongStrict(t));
@ -293,15 +291,15 @@ public class Spreadsheet {
return "+-*/^".contains(s); return "+-*/^".contains(s);
} }
private int precedence(String op) { private int precedence(String op) {
switch (op) { return switch (op) {
case "^": return 3; case "^" -> 3;
case "*": case "/": return 2; case "*", "/" -> 2;
case "+": case "-": return 1; case "+", "-" -> 1;
default: return 0; default -> 0;
} };
} }
private boolean isLeftAssoc(String op) { private boolean isLeftAssoc(String op) {
return !op.equals("^"); // Potenz ist rechtsassoziativ return !op.equals("^");
} }
private String resolveRef(String ref) { private String resolveRef(String ref) {
@ -318,8 +316,6 @@ public class Spreadsheet {
return Long.parseLong(s); return Long.parseLong(s);
} }
// ---------------------------
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();