1
0
Fork 0

Fehlererkennung beim eingeben von Falschen Formeln implementiert

master
devra 2024-01-07 14:30:17 +01:00
parent beaf0b5b9e
commit f9983c112e
2 changed files with 107 additions and 68 deletions

View File

@ -1,6 +1,8 @@
package de.hs_mannheim.informatik.spreadsheet; package de.hs_mannheim.informatik.spreadsheet;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
/** /**
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim. * Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
@ -10,27 +12,60 @@ import java.io.FileNotFoundException;
public class Axel { public class Axel {
public static void main(String[] args) throws FileNotFoundException { public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(System.in);
Spreadsheet spr = new Spreadsheet(10,10); Spreadsheet spr = new Spreadsheet(10,10);
System.out.println(); System.out.println();
spr.put("A1", "13"); // spr.put("A1", "13");
spr.put("A2", "1"); // spr.put("A2", "1");
spr.put("A3", "123"); // spr.put("A3", "123");
//
// spr.put("B1", "=3");
// spr.put("c1", "=A2-A1+30");
// spr.put("B9", "=41+A1");
// spr.put("J5", "=7*6/5");
// spr.put("J6", "=3/2");
spr.put("B1", "=A2");
spr.put("c1", "=A2-A1+30");
spr.put("B9", "=41+A1");
spr.put("J5", "=7*6/5");
spr.put("J6", "=3/2");
System.out.println(spr);
int test=1;
System.out.println("Wilkommen zum coolsten Spreadsheet Programm der Welt!");
System.out.println("Beispiel-Formel: 'B2=15+5' ");
//spr.saveCsv("C:\\Users\\devra\\git\\Axel\\Axel\\resources\\zahlen.csv"); while(test==1) {
System.out.println("Geben sie ihre Formel ein: ");
System.out.print("-> ");
String userInput = scanner.nextLine();
if(userInput.matches("[A-Za-z][1-9][0-9]?="
+ "((((([A-Za-z][1-9][0-9]?)|([0-9]+))(\\*|\\+|\\-|\\/))*"
+ "(([A-Za-z][1-9][0-9]?)|([0-9]+)))|"
+ "((SUMME|PRODUKT|MID|STABW|MIN|MAX)\\(([A-Za-z][1-9][0-9]*\\:[A-Za-z][1-9][0-9]*)\\)))"))
{
String[] parts = userInput.split("=");
spr.put(parts[0],"="+ parts[1]);
// System.out.println(parts[0]);
System.out.println(spr);
}
else {
System.out.println("Formel ist nicht valide! ");
}
// System.out.println("Was möchten sie machen?");
//
// System.out.println("1. Dokument öffnen");
// System.out.println("2. neues Dokument erstellen");
}
spr.saveCsv("C:\\Users\\devra\\git\\Axel\\Axel\\resources\\zahlen.csv");
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods. // TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
} }
} }

View File

@ -69,7 +69,15 @@ public class Spreadsheet {
} }
private int getRow(String cellName) { private int getRow(String cellName) {
return cellName.charAt(1) - '1'; if(cellName.length()>2) {
int tenDigit=cellName.charAt(1)-'0';
int oneDigit=cellName.charAt(2)-'0';
int combined= tenDigit*10+oneDigit;
return combined -1;
}
else {
return cellName.charAt(1) - '1'; //adjust for indexing at 0
}
} }
// ----- // -----
@ -156,64 +164,60 @@ public class Spreadsheet {
return 0; return 0;
} }
/** /**
* This method calculates the result of a "normal" algebraic expression. It only needs to support * This method calculates the result of a "normal" algebraic expression. It only needs to support
* expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus, * expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus,
* minus, times, split only. An expression always starts with either a number or a cell name. If it * minus, times, split only. An expression always starts with either a number or a cell name. If it
* continues, it is guaranteed that this is followed by an operator and either a number or a * continues, it is guaranteed that this is followed by an operator and either a number or a
* cell name again. It is NOT required to implement dot before dash or parentheses in formulas. * cell name again. It is NOT required to implement dot before dash or parentheses in formulas.
* @param formula The expression to be evaluated. * @param formula The expression to be evaluated.
* @return The result calculated. * @return The result calculated.
*/ */
private long calculate(String formula) throws ArithmeticException { private long calculate(String formula) throws ArithmeticException {
System.out.println(formula); //System.out.println(formula);
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
ArrayList<String> zahlen = new ArrayList<>();
while(m.find()) {
String s = m.group();
if (!s.isEmpty()) {
zahlen.add(s);
}
}
int ersteZahl = Integer.parseInt(get(zahlen.get(0)));
ArrayList<String> zahlen = new ArrayList<>();
int zweiteZahl=0;
while(m.find()) {
for (int i = 0; i < zahlen.size(); i+=2) { String s = m.group();
if(i<zahlen.size()-2) { if (!s.isEmpty()) {
String operant=zahlen.get(i+1); zahlen.add(s);
zweiteZahl=Integer.parseInt(get(zahlen.get(i+2))); }
switch(operant) {
case "+":
ersteZahl=ersteZahl+zweiteZahl;
break;
case "-":
ersteZahl=ersteZahl-zweiteZahl;
break;
case "*":
ersteZahl=ersteZahl*zweiteZahl;
break;
case "/":
ersteZahl=ersteZahl/zweiteZahl;
break;
}
}
}
System.out.print(ersteZahl);
System.out.println(" ");
return ersteZahl;
} }
int ersteZahl = Integer.parseInt(get(zahlen.get(0)));
int zweiteZahl=0;
for (int i = 0; i < zahlen.size()-2; i+=2) {
String operant=zahlen.get(i+1);
zweiteZahl=Integer.parseInt(get(zahlen.get(i+2)));
switch(operant) {
case "+":
ersteZahl=ersteZahl+zweiteZahl;
break;
case "-":
ersteZahl=ersteZahl-zweiteZahl;
break;
case "*":
ersteZahl=ersteZahl*zweiteZahl;
break;
case "/":
ersteZahl=ersteZahl/zweiteZahl;
break;
}
}
return ersteZahl;
}
// ----- // -----