// evaluateCell() implemented + some error corrections
parent
5a69f8b0f5
commit
23d6c57683
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
|
<classpathentry kind="src" path="Axel/src"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>PR1-Spreadsheet</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
|
@ -2,6 +2,8 @@ package de.hs_mannheim.informatik.spreadsheet;
|
||||||
//m
|
//m
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
|
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.
|
||||||
*
|
*
|
||||||
|
@ -11,6 +13,13 @@ public class Axel {
|
||||||
|
|
||||||
public static void main(String[] args) throws FileNotFoundException {
|
public static void main(String[] args) throws FileNotFoundException {
|
||||||
Spreadsheet spr = new Spreadsheet(10,10);
|
Spreadsheet spr = new Spreadsheet(10,10);
|
||||||
|
Scanner sc= new Scanner(System.in);
|
||||||
|
|
||||||
|
|
||||||
|
boolean exit=false;
|
||||||
|
String readInput;
|
||||||
|
String currentCell;
|
||||||
|
String formula;
|
||||||
|
|
||||||
spr.put("A3", "123");
|
spr.put("A3", "123");
|
||||||
spr.put("A2", "1");
|
spr.put("A2", "1");
|
||||||
|
@ -19,11 +28,54 @@ public class Axel {
|
||||||
spr.put("J5", "=7*6");
|
spr.put("J5", "=7*6");
|
||||||
spr.put("J6", "=3/2");
|
spr.put("J6", "=3/2");
|
||||||
|
|
||||||
|
//add some values for testing
|
||||||
|
spr.put("B1", "10");
|
||||||
|
spr.put("B2", "5");
|
||||||
|
spr.put("B3", "8");
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
System.out.println(spr);
|
System.out.println(spr);
|
||||||
|
|
||||||
spr.saveCsv("/tmp/test.csv");
|
spr.saveCsv("/tmp/test.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.
|
||||||
}
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
//set values in cells
|
||||||
|
|
||||||
|
System.out.println("cell:");
|
||||||
|
readInput= sc.nextLine();
|
||||||
|
|
||||||
|
currentCell=readInput;
|
||||||
|
|
||||||
|
System.out.println("Set:");
|
||||||
|
|
||||||
|
readInput= sc.nextLine();
|
||||||
|
|
||||||
|
formula=readInput;
|
||||||
|
|
||||||
|
spr.put(currentCell, formula);
|
||||||
|
|
||||||
|
System.out.println(spr);
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
// System.out.println("Exit? j/n");
|
||||||
|
// readInput= sc.nextLine();
|
||||||
|
|
||||||
|
if(readInput.equals("j"))
|
||||||
|
exit=true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
while(!exit);
|
||||||
|
|
||||||
|
System.out.println("Programmende.");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
|
//
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
* A cell needs to be able to hold a formula and a value
|
* A cell needs to be able to hold a formula and a value
|
||||||
|
|
|
@ -6,6 +6,8 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim.
|
* A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim.
|
||||||
* One aspect worth mentioning is that it only supports long numbers, not doubles.
|
* One aspect worth mentioning is that it only supports long numbers, not doubles.
|
||||||
|
@ -127,32 +129,19 @@ public class Spreadsheet {
|
||||||
String sum1;
|
String sum1;
|
||||||
String sum2;
|
String sum2;
|
||||||
|
|
||||||
if (formula.startsWith("SUMME(")) { // e.g. SUMME(A3:A8) //SUMME(A13:B22)
|
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) //SUMME(A13:B22)
|
||||||
/* if(Character.isDigit(formula.charAt(8))) {
|
|
||||||
sum1= formula.substring(6, 9);
|
|
||||||
|
|
||||||
if(Character.isDigit(formula.charAt(12))) {
|
|
||||||
sum2=formula.substring(10,13);
|
|
||||||
}else sum2=formula.substring(10,12);
|
|
||||||
}else {
|
|
||||||
sum1= formula.substring(6, 8);
|
|
||||||
if(Character.isDigit(formula.charAt(11))) {
|
|
||||||
sum2=formula.substring(9,12);
|
|
||||||
}else sum2=formula.substring(9,11);
|
|
||||||
|
|
||||||
} */
|
|
||||||
result = "" + sum(getStartCellName(formula), getEndCellName(formula)); // TODO adapt to cells with two digits
|
result = "" + sum(getStartCellName(formula), getEndCellName(formula)); // TODO adapt to cells with two digits
|
||||||
}
|
|
||||||
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
|
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
|
||||||
result = "TODO"; // TODO
|
result = "" + prod(getStartCellName(formula),getEndCellName(formula)); // TODO
|
||||||
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
||||||
result = "TODO"; // TODO
|
result = ""+ average(getStartCellName(formula),getEndCellName(formula)); // TODO
|
||||||
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
|
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
|
||||||
result = "TODO"; // TODO
|
result = "" + stDev(getStartCellName(formula),getEndCellName(formula)); // TODO
|
||||||
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert
|
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert
|
||||||
result = "TODO"; // TODO
|
result = ""+ min(getStartCellName(formula), getEndCellName(formula));
|
||||||
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung
|
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung
|
||||||
result = "TODO"; // TODO
|
result = ""+ max(getStartCellName(formula), getEndCellName(formula)); // TODO
|
||||||
else if (!formula.isEmpty()) {
|
else if (!formula.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
result = "" + calculate(formula);
|
result = "" + calculate(formula);
|
||||||
|
@ -167,7 +156,7 @@ public class Spreadsheet {
|
||||||
/**
|
/**
|
||||||
* Method for calculating the sum of a rectangular block of cells, such as from A1 to B3.
|
* Method for calculating the sum of a rectangular block of cells, such as from A1 to B3.
|
||||||
* @param startCellName The name of the cell in the upper left corner of the rectangle.
|
* @param startCellName The name of the cell in the upper left corner of the rectangle.
|
||||||
* @param endCellName The name of the cell in the lower right corner of the rectangle.
|
* @param endCellName The name of the cell in the lower right corner ofODO the rectangle.
|
||||||
* @return The sum calculated.
|
* @return The sum calculated.
|
||||||
*/
|
*/
|
||||||
private long sum(String startCellName, String endCellName) {
|
private long sum(String startCellName, String endCellName) {
|
||||||
|
@ -178,7 +167,7 @@ public class Spreadsheet {
|
||||||
|
|
||||||
int endRow=0;
|
int endRow=0;
|
||||||
int endCol;
|
int endCol;
|
||||||
int sum=0;
|
long sum=0;
|
||||||
int temp;
|
int temp;
|
||||||
|
|
||||||
startRow= getRow(startCellName);
|
startRow= getRow(startCellName);
|
||||||
|
@ -198,6 +187,178 @@ public class Spreadsheet {
|
||||||
|
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private long prod(String startCellName, String endCellName) {
|
||||||
|
|
||||||
|
int startRow=0;
|
||||||
|
int startCol=0;
|
||||||
|
|
||||||
|
int endRow=0;
|
||||||
|
int endCol;
|
||||||
|
long prod=1;
|
||||||
|
long temp;
|
||||||
|
|
||||||
|
startRow= getRow(startCellName);
|
||||||
|
startCol= getCol(startCellName);
|
||||||
|
|
||||||
|
endRow= getRow(endCellName);
|
||||||
|
endCol= getCol(endCellName);
|
||||||
|
|
||||||
|
for(int i= startRow; i<=endRow; i++) {
|
||||||
|
for(int j=startCol; j<=endCol; j++) {
|
||||||
|
|
||||||
|
temp= Integer.parseInt(cells[i][j].getValue());
|
||||||
|
prod=prod*temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return prod;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int average(String startCellName, String endCellName) {
|
||||||
|
|
||||||
|
int startRow=0;
|
||||||
|
int startCol=0;
|
||||||
|
|
||||||
|
int endRow=0;
|
||||||
|
int endCol;
|
||||||
|
int sum=0;
|
||||||
|
int temp;
|
||||||
|
int part=0;
|
||||||
|
int avg=0;;
|
||||||
|
|
||||||
|
startRow= getRow(startCellName);
|
||||||
|
startCol= getCol(startCellName);
|
||||||
|
|
||||||
|
endRow= getRow(endCellName);
|
||||||
|
endCol= getCol(endCellName);
|
||||||
|
|
||||||
|
for(int i= startRow; i<=endRow; i++) {
|
||||||
|
for(int j=startCol; j<=endCol; j++) {
|
||||||
|
|
||||||
|
temp= Integer.parseInt(cells[i][j].getValue());
|
||||||
|
sum=sum+temp;
|
||||||
|
part++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
avg=sum/part;
|
||||||
|
|
||||||
|
return avg;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Method to calculate the minimum
|
||||||
|
|
||||||
|
private int min(String startCellName, String endCellName) {
|
||||||
|
int startRow=0;
|
||||||
|
int startCol=0;
|
||||||
|
|
||||||
|
int endRow=0;
|
||||||
|
int endCol;
|
||||||
|
int min;
|
||||||
|
int temp;
|
||||||
|
|
||||||
|
startRow= getRow(startCellName);
|
||||||
|
startCol= getCol(startCellName);
|
||||||
|
|
||||||
|
endRow= getRow(endCellName);
|
||||||
|
endCol= getCol(endCellName);
|
||||||
|
|
||||||
|
min=Integer.parseInt(cells[startRow][startCol].getValue());
|
||||||
|
|
||||||
|
for(int i= startRow; i<=endRow; i++) {
|
||||||
|
for(int j=startCol; j<=endCol; j++) {
|
||||||
|
|
||||||
|
temp= Integer.parseInt(cells[i][j].getValue());
|
||||||
|
if(min>temp) {
|
||||||
|
min=temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//calculate maximum
|
||||||
|
|
||||||
|
private int max(String startCellName, String endCellName) {
|
||||||
|
int startRow=0;
|
||||||
|
int startCol=0;
|
||||||
|
|
||||||
|
int endRow=0;
|
||||||
|
int endCol;
|
||||||
|
int max;
|
||||||
|
int temp;
|
||||||
|
|
||||||
|
startRow= getRow(startCellName);
|
||||||
|
startCol= getCol(startCellName);
|
||||||
|
|
||||||
|
endRow= getRow(endCellName);
|
||||||
|
endCol= getCol(endCellName);
|
||||||
|
|
||||||
|
max=Integer.parseInt(cells[startRow][startCol].getValue());
|
||||||
|
|
||||||
|
for(int i= startRow; i<=endRow; i++) {
|
||||||
|
for(int j=startCol; j<=endCol; j++) {
|
||||||
|
|
||||||
|
temp= Integer.parseInt(cells[i][j].getValue());
|
||||||
|
if(max<temp) {
|
||||||
|
max=temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private int stDev(String startCellName, String endCellName) {
|
||||||
|
|
||||||
|
int startRow=0;
|
||||||
|
int startCol=0;
|
||||||
|
|
||||||
|
int endRow=0;
|
||||||
|
int endCol;
|
||||||
|
int temp;
|
||||||
|
int part=0;
|
||||||
|
int avg=0;;
|
||||||
|
int dev=0;
|
||||||
|
|
||||||
|
|
||||||
|
startRow= getRow(startCellName);
|
||||||
|
startCol= getCol(startCellName);
|
||||||
|
|
||||||
|
endRow= getRow(endCellName);
|
||||||
|
endCol= getCol(endCellName);
|
||||||
|
ArrayList<Integer> elements = new ArrayList<>();
|
||||||
|
|
||||||
|
avg= average(startCellName, endCellName);
|
||||||
|
|
||||||
|
|
||||||
|
for(int i= startRow; i<=endRow; i++) {
|
||||||
|
for(int j=startCol; j<=endCol; j++) {
|
||||||
|
|
||||||
|
temp= Integer.parseInt(cells[i][j].getValue());
|
||||||
|
temp=temp-avg;
|
||||||
|
temp= temp*temp;
|
||||||
|
elements.add(temp);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
avg=0;
|
||||||
|
|
||||||
|
for(int i=0; i<elements.size(); i++) {
|
||||||
|
avg=avg+elements.get(i);
|
||||||
|
part++;
|
||||||
|
}
|
||||||
|
avg=avg/part;
|
||||||
|
dev= (int) Math.sqrt(avg);
|
||||||
|
|
||||||
|
return dev;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
|
@ -256,18 +417,60 @@ public class Spreadsheet {
|
||||||
public String getStartCellName(String formula) {
|
public String getStartCellName(String formula) {
|
||||||
|
|
||||||
String startCell="";
|
String startCell="";
|
||||||
if(Character.isDigit(formula.charAt(8))) {
|
int a=0;
|
||||||
|
|
||||||
|
for(int i=0; i<formula.length(); i++) {
|
||||||
|
if(formula.charAt(i)=='(') {
|
||||||
|
a=i;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Character.isDigit(formula.charAt(a+3))) {
|
||||||
|
startCell=formula.substring(a+1, a+4);
|
||||||
|
}else startCell=formula.substring(a+1, a+3);
|
||||||
|
|
||||||
|
//try to get the cell name for each formula not only SUMS
|
||||||
|
|
||||||
|
/* if(Character.isDigit(formula.charAt(8))) {
|
||||||
startCell= formula.substring(6, 9);
|
startCell= formula.substring(6, 9);
|
||||||
}else startCell= formula.substring(6, 8);
|
}else startCell= formula.substring(6, 8);
|
||||||
|
*/
|
||||||
|
System.out.println(startCell);
|
||||||
|
|
||||||
|
return startCell;
|
||||||
|
|
||||||
return startCell;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getEndCellName(String formula) {
|
public String getEndCellName(String formula) {
|
||||||
|
|
||||||
String endCell="";
|
String endCell="";
|
||||||
|
int a=0;
|
||||||
|
|
||||||
if(Character.isDigit(formula.charAt(8))) {
|
for(int i=0; i<formula.length(); i++) {
|
||||||
|
if(formula.charAt(i)=='(') {
|
||||||
|
a=i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Character.isDigit(formula.charAt(a+3))) {
|
||||||
|
if(Character.isDigit(formula.charAt(a+7))) {
|
||||||
|
endCell=formula.substring(a+5, a+8);
|
||||||
|
}else endCell=formula.substring(a+5, a+7);
|
||||||
|
}else{
|
||||||
|
if(Character.isDigit(formula.charAt(a+6))) {
|
||||||
|
endCell=formula.substring(a+4, a+7);
|
||||||
|
}else endCell=formula.substring(a+4, a+6);
|
||||||
|
|
||||||
|
}
|
||||||
|
System.out.println(endCell);
|
||||||
|
|
||||||
|
|
||||||
|
/* if(Character.isDigit(formula.charAt(8))) {
|
||||||
if(Character.isDigit(formula.charAt(12))) {
|
if(Character.isDigit(formula.charAt(12))) {
|
||||||
endCell=formula.substring(10,13);
|
endCell=formula.substring(10,13);
|
||||||
}else endCell=formula.substring(10,12);
|
}else endCell=formula.substring(10,12);
|
||||||
|
@ -276,9 +479,11 @@ public class Spreadsheet {
|
||||||
endCell=formula.substring(9,12);
|
endCell=formula.substring(9,12);
|
||||||
}else endCell=formula.substring(9,11);
|
}else endCell=formula.substring(9,11);
|
||||||
|
|
||||||
}
|
} */
|
||||||
|
|
||||||
return endCell;
|
return endCell;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue