1
0
Fork 0

Ausrechnen von Formeln

main
ERANZER 2024-01-09 13:25:44 +01:00
parent 9096432995
commit 365b569a67
3 changed files with 57 additions and 15 deletions

View File

@ -2,5 +2,6 @@
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="Axel/src"/> <classpathentry kind="src" path="Axel/src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@ -20,11 +20,11 @@ public class Axel {
cols=Integer.parseInt(kb.nextLine()); cols=Integer.parseInt(kb.nextLine());
Spreadsheet spr=new Spreadsheet(rows,cols); Spreadsheet spr=new Spreadsheet(rows,cols);
//spr.put("A3", "3"); //spr.put("A3", "3");
//spr.put("A2", "1"); spr.put("A2", "2");
spr.put("A1","=19+4");
//spr.put("B9", "=41+A2"); spr.put("B9", "=41+A2");
//spr.put("J5", "=7*6"); spr.put("J5", "=7*6");
//spr.put("J6", "=3/2"); spr.put("J6", "=4/2");
while(true) { while(true) {
System.out.println(spr); System.out.println(spr);
@ -44,8 +44,6 @@ public class Axel {
} }
spr.saveCsv("/tmp/test.csv"); spr.saveCsv("/tmp/test.csv");
kb.close(); kb.close();
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
} }
} }

View File

@ -21,15 +21,13 @@ public class Spreadsheet {
* @param cols number of columns * @param cols number of columns
*/ */
public Spreadsheet(int rows, int cols) { public Spreadsheet(int rows, int cols) {
// TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns
if (rows>99){ if (rows>99){
rows=99; rows=99;
} }
if (cols>26) { if (cols>26) {
cols=26; cols=26;
} }
cells = new Cell[rows][cols]; cells = new Cell[rows][cols];
for (int r = 0; r < rows; r++) for (int r = 0; r < rows; r++)
@ -260,14 +258,59 @@ public class Spreadsheet {
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; long res = 0;
boolean first=true;
String operation="";
// TODO implement
// uncomment the following to see an example how the elements of a formula can be accessed
while (m.find()) { // m.find() must always be used before m.group() while (m.find()) { // m.find() must always be used before m.group()
String s = m.group(); String s = m.group();
if (!s.isEmpty()) { if(!s.isEmpty()) {
System.out.println(s); if(first && (int)s.charAt(0)>=(int)'A'&&(int)s.charAt(0)<=(int)'Z') {
res=Integer.parseInt(cells[(Integer.parseInt(s.substring(1)))-1][(int)s.charAt(0)-(int)-'A'].getValue());
first=false;
}else if(first) {
res=Integer.parseInt(s);
first=false;
}else if(s.equals("+")){
operation="+";
}else if(s.equals("-")){
operation="-";
}else if(s.equals("*")){
operation="*";
}else if(s.equals("/")){
operation="/";
}else if((int)s.charAt(0)>=(int)'A'&&(int)s.charAt(0)<=(int)'Z'){
switch(operation) {
case("+"):
res+=Integer.parseInt(cells[(Integer.parseInt(s.substring(1)))-1][(int)s.charAt(0)-(int)'A'].getValue());
break;
case("-"):
res-=Integer.parseInt(cells[(Integer.parseInt(s.substring(1)))-1][(int)s.charAt(0)-(int)'A'].getValue());
break;
case("*"):
res*=Integer.parseInt(cells[(Integer.parseInt(s.substring(1)))-1][(int)s.charAt(0)-(int)'A'].getValue());
break;
case("/"):
res/=Integer.parseInt(cells[(Integer.parseInt(s.substring(1)))-1][(int)s.charAt(0)-(int)'A'].getValue());
break;
}
}else {
switch(operation) {
case("+"):
res+=Integer.parseInt(s);
break;
case("-"):
res-=Integer.parseInt(s);
break;
case("*"):
res*=Integer.parseInt(s);
break;
case("/"):
res/=Integer.parseInt(s);
break;
}
}
} }
} }