1
0
Fork 0

Grundrechenarten

Fehler behoben mit der 10. Reihe(charAt(1) bei D10 ist 1 also wird in
Reihe 1 eingetragen)
main
Dr.Janson 2024-01-06 20:40:58 +01:00
parent dcceb207a1
commit 203a833bc6
2 changed files with 79 additions and 21 deletions

View File

@ -16,27 +16,31 @@ public class Axel {
Scanner input = new Scanner(System.in); Scanner input = new Scanner(System.in);
spr.put("A3", "123"); spr.put("A3", "123");
spr.put("A2", "1");
spr.put("B9", "=41+A2"); spr.put("B9", "=41+A2+10-2/3");
spr.put("J5", "=7*6");
spr.put("J6", "=3/2");
System.out.println("Schreibe 'stop' zum anhalten."); System.out.println("Schreibe 'stop' zum anhalten.");
do{ do{
String command = ""; String command = "";
String form = ""; String form = "";
String cell= ""; String cell= "";
boolean cellTRUE = true;
System.out.println(spr); System.out.println(spr);
System.out.println("Befehl (z.B. D4_=7*6): "); System.out.println("Befehl (z.B. D4_=7*6): ");
command = input.nextLine(); command = input.nextLine();
if(command.contentEquals("stop")) if(command.contentEquals("stop"))
break; break;
for(int i = 3; i<command.length();i++) { for(int i = 0; i<command.length();i++) {
form+=command.charAt(i); if(command.charAt(i)=='_') {
cellTRUE = false;
continue;
}
if(cellTRUE) {
cell+=command.charAt(i);
}else {
form+=command.charAt(i);
}
} }
cell+=command.charAt(0);
cell+=command.charAt(1);
spr.put(cell, form); spr.put(cell, form);

View File

@ -5,6 +5,9 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.List;
/** /**
* A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim. * A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim.
@ -32,8 +35,18 @@ public class Spreadsheet {
} }
// ----- // -----
/*
* @param str A String with multiple numbers.
* @return true if there are ONLY numbers in this sting.
*/
private boolean isNumber(String str) {
return str.matches("[0-9]+");
}
private boolean isCar(String str) {//Brum
return str.matches("[-\\+\\*/]+");
}
// retrieve or change values of cells // retrieve or change values of cells
private String get(int row, int col) { private String get(int row, int col) {
return cells[row][col].getValue(); return cells[row][col].getValue();
} }
@ -57,7 +70,15 @@ public class Spreadsheet {
return cellName.charAt(0) - 'A'; return cellName.charAt(0) - 'A';
} }
private int getRow(String cellName) { private int getRow(String cellName) {
return cellName.charAt(1) - '1'; if (cellName.length()==2) {
return cellName.charAt(1) - '1';
}else {
int Row = 0;
Row +=((cellName.charAt(1)-48)*10);
Row +=(cellName.charAt(2)-49);
System.out.println(Row);
return Row;
}
} }
// ----- // -----
// business logic // business logic
@ -160,19 +181,52 @@ public class Spreadsheet {
private long calculate(String formula) throws ArithmeticException { private long calculate(String formula) throws ArithmeticException {
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
long res = 0; List<String> args = new ArrayList<>();
// TODO implement while (m.find()) {
if(!m.group().isEmpty()) {
// uncomment the following to see an example how the elements of a formula can be accessed if(isNumber(m.group())||isCar(m.group())) {
while (m.find()) { // m.find() must always be used before m.group() args.add(m.group());
String s = m.group(); }else {
if (!s.isEmpty()) { if(cells[getRow(m.group())][getCol(m.group())].getValue().isEmpty())
System.out.println(s); args.add("0");
else
args.add(m.group());
}
} }
} }
return res; for(int i = 2; i<args.size();) {
System.out.println(args.get(i));
String val = "";
switch(args.get(i-1)) {
case "+":
val =""+ (Integer.parseInt(args.get(i-2))+Integer.parseInt(args.get(i)));
args.set(i,val);
args.remove(i-1);
args.remove(i-2);
break;
case "-":
val =""+ (Integer.parseInt(args.get(i-2))-Integer.parseInt(args.get(i)));
args.set(i,val);
args.remove(i-1);
args.remove(i-2);
break;
case "*" :
val =""+ (Integer.parseInt(args.get(i-2))*Integer.parseInt(args.get(i)));
args.set(i,val);
args.remove(i-1);
args.remove(i-2);
break;
case "/":
val =""+ (Integer.parseInt(args.get(i-2))/Integer.parseInt(args.get(i)));
args.set(i,val);
args.remove(i-1);
args.remove(i-2);
break;
}
}
return Long.parseLong(args.get(0));
} }
// ----- // -----