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>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="Axel/src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -20,11 +20,11 @@ public class Axel {
cols=Integer.parseInt(kb.nextLine());
Spreadsheet spr=new Spreadsheet(rows,cols);
//spr.put("A3", "3");
//spr.put("A2", "1");
//spr.put("B9", "=41+A2");
//spr.put("J5", "=7*6");
//spr.put("J6", "=3/2");
spr.put("A2", "2");
spr.put("A1","=19+4");
spr.put("B9", "=41+A2");
spr.put("J5", "=7*6");
spr.put("J6", "=4/2");
while(true) {
System.out.println(spr);
@ -44,8 +44,6 @@ public class Axel {
}
spr.saveCsv("/tmp/test.csv");
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
*/
public Spreadsheet(int rows, int cols) {
// TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns
if (rows>99){
rows=99;
}
if (cols>26) {
cols=26;
}
cells = new Cell[rows][cols];
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);
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()
String s = m.group();
if (!s.isEmpty()) {
System.out.println(s);
if(!s.isEmpty()) {
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;
}
}
}
}