Compare commits
21 Commits
Author | SHA1 | Date |
---|---|---|
Anastasia Kisner | ff7f73a413 | |
Anastasia Kisner | 4cb618729a | |
Anastasia Kisner | d7dbffeff8 | |
Anastasia Kisner | c980006f02 | |
Anastasia Kisner | a0117ae961 | |
Anastasia Kisner | 111b4a36eb | |
Anastasia Kisner | 10f0aef19a | |
Anastasia Kisner | bbe478ff81 | |
Anastasia Kisner | bbbaf7473a | |
Anastasia Kisner | 44e9e9bc9c | |
Anastasia Kisner | d3326a5c3b | |
Anastasia Kisner | b706e537c8 | |
Anastasia Kisner | fa1dc30c27 | |
Anastasia Kisner | 0a2c6c0514 | |
Anastasia Kisner | a3da7cf436 | |
Anastasia Kisner | 7472ee85e8 | |
Anastasia Kisner | c66c40e340 | |
Anastasia Kisner | 4988308c57 | |
Anastasia Kisner | 85948394c1 | |
Anastasia Kisner | df3bb6b59b | |
Anastasia Kisner | c6ce44dac8 |
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
|
@ -0,0 +1,5 @@
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/PR1-Spreadsheet.iml" filepath="$PROJECT_DIR$/PR1-Spreadsheet.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -1,6 +1,7 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
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,19 +12,43 @@ 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);
|
||||||
|
spr.readCsv("C:/Users/kisne/IdeaProjects/PR1-Spreadsheet/tmp/test.csv", ",");
|
||||||
|
Scanner keyboard = new Scanner(System.in);
|
||||||
|
|
||||||
spr.put("A3", "123");
|
spr.put("A3", "123");
|
||||||
spr.put("A2", "1");
|
spr.put("A2", "3");
|
||||||
|
spr.put("A5", "5");
|
||||||
spr.put("B9", "=41+A2");
|
spr.put("B8", "6");
|
||||||
spr.put("J5", "=7*6");
|
|
||||||
spr.put("J6", "=3/2");
|
|
||||||
|
|
||||||
|
|
||||||
|
spr.put("A8", "=Summe(A5:D8)");
|
||||||
|
spr.put("J7", "=a2+2");
|
||||||
|
spr.put("J8", "=a2+J7*4");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
System.out.println(spr);
|
System.out.println(spr);
|
||||||
|
|
||||||
|
while(true){
|
||||||
|
System.out.print("Please enter the cell name (e.g. D8, G5): ");
|
||||||
|
String cellName = keyboard.nextLine();
|
||||||
|
|
||||||
|
System.out.print("Please enter your value or your formula (starting with '='): ");
|
||||||
|
String cellValue = keyboard.nextLine();
|
||||||
|
|
||||||
|
spr.put(cellName, cellValue);
|
||||||
|
|
||||||
|
System.out.println(spr);
|
||||||
|
|
||||||
|
System.out.print("Do you want to end the program? Y/N: ");
|
||||||
|
if(keyboard.nextLine().equalsIgnoreCase("Y")){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
spr.saveCsv("C:/Users/kisne/IdeaProjects/PR1-Spreadsheet/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.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,11 +1,17 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Scanner;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
@ -20,15 +26,17 @@ public class Spreadsheet {
|
||||||
* @param rows number of rows
|
* @param rows number of rows
|
||||||
* @param cols number of columns
|
* @param cols number of columns
|
||||||
*/
|
*/
|
||||||
public Spreadsheet(int rows, int cols) {
|
public Spreadsheet(int rows, int cols) throws IllegalArgumentException {
|
||||||
|
|
||||||
// TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns
|
if(rows < 1 || rows > 99 || cols < 1 || cols > 26){
|
||||||
|
throw new IllegalArgumentException("Range in rows 1-99; Range in cols 1-26");
|
||||||
|
}else {
|
||||||
|
cells = new Cell[rows][cols];
|
||||||
|
|
||||||
cells = new Cell[rows][cols];
|
for (int r = 0; r < rows; r++)
|
||||||
|
for (int c = 0; c < cols; c++)
|
||||||
for (int r = 0; r < rows; r++)
|
cells[r][c] = new Cell();
|
||||||
for (int c = 0; c < cols; c++)
|
}
|
||||||
cells[r][c] = new Cell();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----
|
// -----
|
||||||
|
@ -62,6 +70,8 @@ public class Spreadsheet {
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getRow(String cellName) {
|
private int getRow(String cellName) {
|
||||||
|
if(cellName.length()==3){
|
||||||
|
return Integer.parseInt(cellName.substring(1))-1;}
|
||||||
return cellName.charAt(1) - '1';
|
return cellName.charAt(1) - '1';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,18 +82,44 @@ public class Spreadsheet {
|
||||||
* A method for reading in data from a CSV file.
|
* A method for reading in data from a CSV file.
|
||||||
* @param path The file to read.
|
* @param path The file to read.
|
||||||
* @param separator The char used to split up the input, e.g. a comma or a semicolon.
|
* @param separator The char used to split up the input, e.g. a comma or a semicolon.
|
||||||
* @param starCellName The upper left cell where data from the CSV file should be inserted.
|
|
||||||
* @return Nothing.
|
|
||||||
* @exception IOException If path does not exist.
|
* @exception IOException If path does not exist.
|
||||||
*/
|
*/
|
||||||
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
public void readCsv(String path, String separator) throws FileNotFoundException {
|
||||||
// TODO: implement this
|
|
||||||
}
|
|
||||||
|
|
||||||
|
ArrayList<String> lines = new ArrayList<>();
|
||||||
|
Scanner scan = new Scanner(new File(path));
|
||||||
|
|
||||||
|
while (scan.hasNextLine()) {
|
||||||
|
lines.add(scan.nextLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] linesArray = lines.toArray(new String[lines.size()]);
|
||||||
|
ArrayList<int[]> values = new ArrayList<>();
|
||||||
|
for(int i = 0; i < linesArray.length; i++) {
|
||||||
|
//System.out.println(linesArray[i]);
|
||||||
|
String[] zwischenSchritt = linesArray[i].split(separator);
|
||||||
|
for (int j = 0; j < zwischenSchritt.length; j++) {
|
||||||
|
System.out.println(zwischenSchritt[j]);
|
||||||
|
if(zwischenSchritt[j].startsWith("=")){
|
||||||
|
values.add(new int[]{i, j});
|
||||||
|
}else{
|
||||||
|
this.put(i, j, zwischenSchritt[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
for(int[] cell : values){
|
||||||
|
String[] value = linesArray[cell[0]].split(",");
|
||||||
|
String formula = value[cell[1]];
|
||||||
|
this.put(cell[0], cell[1], formula);
|
||||||
|
}
|
||||||
|
scan.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* A method for saving data to a CSV file.
|
* A method for saving data to a CSV file.
|
||||||
* @param path The file to write.
|
* @param path The file to write.
|
||||||
* @return Nothing.
|
|
||||||
* @exception IOException If path does not exist.
|
* @exception IOException If path does not exist.
|
||||||
*/
|
*/
|
||||||
public void saveCsv(String path) throws FileNotFoundException {
|
public void saveCsv(String path) throws FileNotFoundException {
|
||||||
|
@ -107,25 +143,27 @@ public class Spreadsheet {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method does the actual evaluation/calcluation of a specific cell
|
* This method does the actual evaluation/calcluation of a specific cell
|
||||||
* @param cellName the name of the cell to be evaluated
|
* @param row the row of the cell to be evaluated
|
||||||
* @return Nothing.
|
* @param col the col of the cell to be evaluated
|
||||||
*/
|
*/
|
||||||
private void evaluateCell(int row, int col) {
|
private void evaluateCell(int row, int col) {
|
||||||
String formula = cells[row][col].getFormula();
|
String formula = cells[row][col].getFormula();
|
||||||
String result = "";
|
String result = "";
|
||||||
|
int colon = formula.indexOf(':');
|
||||||
|
String endSubstring = formula.substring((colon + 1), (formula.length()-1));
|
||||||
|
|
||||||
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8)
|
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8)
|
||||||
result = "" + sum(formula.substring(6, 8), formula.substring(9, 11)); // TODO adapt to cells with two digits
|
result = "" + sum(formula.substring(6, colon), endSubstring);
|
||||||
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(formula.substring(8, colon), endSubstring);
|
||||||
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
||||||
result = "TODO"; // TODO
|
result = "" + mit(formula.substring(11, colon), endSubstring);
|
||||||
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 = "" + stabw(formula.substring(6, colon), endSubstring);
|
||||||
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert
|
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> kleinster Wert
|
||||||
result = "TODO"; // TODO
|
result = "" + min(formula.substring(4, colon), endSubstring);
|
||||||
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung
|
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert
|
||||||
result = "TODO"; // TODO
|
result = "" + max(formula.substring(4, colon), endSubstring);
|
||||||
else if (!formula.isEmpty()) {
|
else if (!formula.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
result = "" + calculate(formula);
|
result = "" + calculate(formula);
|
||||||
|
@ -137,6 +175,27 @@ public class Spreadsheet {
|
||||||
cells[row][col].setValue("" + result);
|
cells[row][col].setValue("" + result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for calculating the amount of cells with values inside 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 endCellName The name of the cell in the lower right corner of the rectangle.
|
||||||
|
* @return The counted cells with values calculated.
|
||||||
|
*/
|
||||||
|
private long counterOfCellsWithValue(String startCellName, String endCellName){
|
||||||
|
long counter = 0;
|
||||||
|
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
|
||||||
|
|
||||||
|
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
|
||||||
|
String value = get((row - 1), (col - 'A'));
|
||||||
|
|
||||||
|
if(!value.isEmpty()){
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
@ -144,16 +203,147 @@ public class Spreadsheet {
|
||||||
* @return The sum calculated.
|
* @return The sum calculated.
|
||||||
*/
|
*/
|
||||||
private long sum(String startCellName, String endCellName) {
|
private long sum(String startCellName, String endCellName) {
|
||||||
// TODO implement
|
long result = 0;
|
||||||
|
|
||||||
return 0;
|
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
|
||||||
|
|
||||||
|
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
|
||||||
|
|
||||||
|
String value = get((row - 1), (col - 'A')); //"-1" because we start with 1 instead of 0. "-A" because A in ascii is 65, but we start with 0
|
||||||
|
|
||||||
|
if(!value.isEmpty()){
|
||||||
|
result += Long.parseLong(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for calculating the product 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 endCellName The name of the cell in the lower right corner of the rectangle.
|
||||||
|
* @return The product calculated.
|
||||||
|
*/
|
||||||
|
private long prod(String startCellName, String endCellName) {
|
||||||
|
long result = 0;
|
||||||
|
|
||||||
|
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
|
||||||
|
|
||||||
|
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
|
||||||
|
|
||||||
|
String value = get((row - 1), (col - 'A'));
|
||||||
|
System.out.println(value);
|
||||||
|
|
||||||
|
if(result == 0){
|
||||||
|
result = Long.parseLong(value);
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(!value.isEmpty() && !(value.equals("0"))){
|
||||||
|
result *= Long.parseLong(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for calculating the arithmetic average 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 endCellName The name of the cell in the lower right corner of the rectangle.
|
||||||
|
* @return The arithmetic average calculated.
|
||||||
|
*/
|
||||||
|
private long mit(String startCellName, String endCellName) {
|
||||||
|
|
||||||
|
return sum(startCellName, endCellName)/ counterOfCellsWithValue(startCellName, endCellName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for calculating the standard deviation 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 endCellName The name of the cell in the lower right corner of the rectangle.
|
||||||
|
* @return The standard deviation calculated.
|
||||||
|
*/
|
||||||
|
private long stabw(String startCellName, String endCellName) {
|
||||||
|
long mean = mit(startCellName, endCellName);
|
||||||
|
long counter = counterOfCellsWithValue(startCellName, endCellName);
|
||||||
|
long result = 0;
|
||||||
|
|
||||||
|
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++) {
|
||||||
|
|
||||||
|
for (int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
|
||||||
|
|
||||||
|
String value = get((row - 1), (col - 'A'));
|
||||||
|
|
||||||
|
if(!value.isEmpty()){
|
||||||
|
result += (long) Math.pow(Long.parseLong(value) - mean, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (long) Math.sqrt(result / counter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for finding the maximum value 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 endCellName The name of the cell in the lower right corner of the rectangle.
|
||||||
|
* @return The minimum value of the block.
|
||||||
|
*/
|
||||||
|
private long min(String startCellName, String endCellName) {
|
||||||
|
long result = 0;
|
||||||
|
|
||||||
|
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
|
||||||
|
|
||||||
|
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
|
||||||
|
|
||||||
|
String value = get((row - 1), (col - 'A')); //"-1" because we start with 1 instead of 0. "-A" because A in ascii is 65, but we start with 0
|
||||||
|
|
||||||
|
if((col == startCellName.charAt(0)) && (row == Integer.parseInt(startCellName.substring(1)))) {
|
||||||
|
result = Long.parseLong(value);
|
||||||
|
}
|
||||||
|
else if(!value.isEmpty() && (Long.parseLong(value) < result)){
|
||||||
|
result = Long.parseLong(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for finding the maximum value 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 endCellName The name of the cell in the lower right corner of the rectangle.
|
||||||
|
* @return The maximum value of the block.
|
||||||
|
*/
|
||||||
|
private long max(String startCellName, String endCellName) {
|
||||||
|
long result = 0;
|
||||||
|
|
||||||
|
for(char col = startCellName.charAt(0); col <= endCellName.charAt(0); col++){
|
||||||
|
|
||||||
|
for(int row = Integer.parseInt(startCellName.substring(1)); row <= Integer.parseInt(endCellName.substring(1)); row++) {
|
||||||
|
|
||||||
|
String value = get((row - 1), (col - 'A')); //"-1" because we start with 1 instead of 0. "-A" because A in ascii is 65, but we start with 0
|
||||||
|
|
||||||
|
if((col == startCellName.charAt(0)) && (row == Integer.parseInt(startCellName.substring(1)))) {
|
||||||
|
result = Long.parseLong(value);
|
||||||
|
}
|
||||||
|
else if(!value.isEmpty() && (Long.parseLong(value) > result)){
|
||||||
|
result = Long.parseLong(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
@ -162,17 +352,47 @@ 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;
|
||||||
|
char operator = '+';
|
||||||
|
|
||||||
// TODO implement
|
while (m.find()) {
|
||||||
|
|
||||||
// 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();
|
String s = m.group();
|
||||||
if (!s.isEmpty()) {
|
long value;
|
||||||
System.out.println(s);
|
|
||||||
|
if(s.isEmpty()){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(s.matches("[-\\+\\*/]"))) {
|
||||||
|
if(s.matches("[A-Z][0-9]*")) {
|
||||||
|
int col = this.getCol(s);
|
||||||
|
int row = this.getRow(s);
|
||||||
|
|
||||||
|
value = Long.parseLong(cells[row][col].getValue());
|
||||||
|
|
||||||
|
}else{
|
||||||
|
value = Long.parseLong(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(operator) {
|
||||||
|
case '+':
|
||||||
|
res += value;
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
res -= value;
|
||||||
|
break;
|
||||||
|
case '*':
|
||||||
|
res *= value;
|
||||||
|
break;
|
||||||
|
case '/':
|
||||||
|
if (res != 0) {
|
||||||
|
res /= value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
operator = s.charAt(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/Axel/src" isTestSource="false" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/Test" isTestSource="true" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="module-library" scope="TEST">
|
||||||
|
<library name="JUnit5.8.1">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
</component>
|
||||||
|
</module>
|
|
@ -0,0 +1,138 @@
|
||||||
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class SpreadsheetTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void calculateTheSumOfNumbers(){
|
||||||
|
Spreadsheet eingabe = new Spreadsheet(99, 26);
|
||||||
|
|
||||||
|
eingabe.put("T5", "=13+6");
|
||||||
|
assertEquals("19", eingabe.get("T5"));
|
||||||
|
|
||||||
|
eingabe.put("T6", "=0+0");
|
||||||
|
assertEquals("0", eingabe.get("T6"));
|
||||||
|
|
||||||
|
eingabe.put("T7", "=22+34");
|
||||||
|
assertEquals("56", eingabe.get("T7"));
|
||||||
|
|
||||||
|
eingabe.put("T8", "= 9 + 3");
|
||||||
|
assertEquals("12", eingabe.get("T8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void calculateTheDifOfNumbers(){
|
||||||
|
Spreadsheet eingabe = new Spreadsheet(99, 26);
|
||||||
|
|
||||||
|
eingabe.put("T5", "=13-6");
|
||||||
|
assertEquals("7", eingabe.get("T5"));
|
||||||
|
|
||||||
|
eingabe.put("T6", "=0-0");
|
||||||
|
assertEquals("0", eingabe.get("T6"));
|
||||||
|
|
||||||
|
eingabe.put("T7", "=22-34");
|
||||||
|
assertEquals("-12", eingabe.get("T7"));
|
||||||
|
|
||||||
|
eingabe.put("T8", "= 9 - 3");
|
||||||
|
assertEquals("6", eingabe.get("T8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void calculateTheProdOfNumbers(){
|
||||||
|
Spreadsheet eingabe = new Spreadsheet(99, 26);
|
||||||
|
|
||||||
|
eingabe.put("T5", "=13*6");
|
||||||
|
assertEquals("78", eingabe.get("T5"));
|
||||||
|
|
||||||
|
eingabe.put("T6", "=0*0");
|
||||||
|
assertEquals("0", eingabe.get("T6"));
|
||||||
|
|
||||||
|
eingabe.put("T7", "=-2*34");
|
||||||
|
assertEquals("-68", eingabe.get("T7"));
|
||||||
|
|
||||||
|
eingabe.put("T8", "= 9 * 3");
|
||||||
|
assertEquals("27", eingabe.get("T8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void calculateTheValueOfQuotOfNumbers(){
|
||||||
|
Spreadsheet eingabe = new Spreadsheet(99, 26);
|
||||||
|
|
||||||
|
eingabe.put("T5", "=12/3");
|
||||||
|
assertEquals("4", eingabe.get("T5"));
|
||||||
|
|
||||||
|
eingabe.put("T6", "=5/0");
|
||||||
|
assertEquals("exc.", eingabe.get("T6"));
|
||||||
|
|
||||||
|
eingabe.put("T7", "= 22 / 2");
|
||||||
|
assertEquals("11", eingabe.get("T7"));
|
||||||
|
|
||||||
|
eingabe.put("T8", "= 0 / 5");
|
||||||
|
assertEquals("0", eingabe.get("T8"));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void calculateTheSumOfRectangular(){
|
||||||
|
Spreadsheet eingabe = new Spreadsheet(99, 26);
|
||||||
|
|
||||||
|
eingabe.put("T5", "8");
|
||||||
|
eingabe.put("T6", "2");
|
||||||
|
eingabe.put("T7", "30");
|
||||||
|
eingabe.put("T8", "10");
|
||||||
|
eingabe.put("T9", "=Summe(T5:T8)");
|
||||||
|
assertEquals("50", eingabe.get("T9"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void calculateTheSProdOfRectangular(){
|
||||||
|
Spreadsheet eingabe = new Spreadsheet(99, 26);
|
||||||
|
|
||||||
|
eingabe.put("T5", "2");
|
||||||
|
eingabe.put("T6", "4");
|
||||||
|
eingabe.put("T7", "6");
|
||||||
|
eingabe.put("T8", "10");
|
||||||
|
eingabe.put("T9", "=Produkt(T5:T8)");
|
||||||
|
assertEquals("480", eingabe.get("T9"));
|
||||||
|
}
|
||||||
|
void calculateTheMitOfRectangular(){
|
||||||
|
Spreadsheet eingabe = new Spreadsheet(99, 26);
|
||||||
|
|
||||||
|
eingabe.put("T5", "8");
|
||||||
|
eingabe.put("T6", "2");
|
||||||
|
eingabe.put("T7", "30");
|
||||||
|
eingabe.put("T8", "10");
|
||||||
|
eingabe.put("T9", "=Mittelwert(T5:T8)");
|
||||||
|
assertEquals("12", eingabe.get("T9"));
|
||||||
|
}
|
||||||
|
void calculateTheStabwOfRectangular(){
|
||||||
|
Spreadsheet eingabe = new Spreadsheet(99, 26);
|
||||||
|
|
||||||
|
eingabe.put("T5", "8");
|
||||||
|
eingabe.put("T6", "2");
|
||||||
|
eingabe.put("T7", "30");
|
||||||
|
eingabe.put("T8", "10");
|
||||||
|
eingabe.put("T9", "=Stabw(T5:T8)");
|
||||||
|
assertEquals("12", eingabe.get("T9"));
|
||||||
|
}
|
||||||
|
void calculateTheMinOfRectangular(){
|
||||||
|
Spreadsheet eingabe = new Spreadsheet(99, 26);
|
||||||
|
|
||||||
|
eingabe.put("T5", "8");
|
||||||
|
eingabe.put("T6", "2");
|
||||||
|
eingabe.put("T7", "30");
|
||||||
|
eingabe.put("T8", "10");
|
||||||
|
eingabe.put("T9", "=min(T5:T8)");
|
||||||
|
assertEquals("2", eingabe.get("T9"));
|
||||||
|
}
|
||||||
|
void calculateTheMaxOfRectangular(){
|
||||||
|
Spreadsheet eingabe = new Spreadsheet(99, 26);
|
||||||
|
|
||||||
|
eingabe.put("T5", "8");
|
||||||
|
eingabe.put("T6", "2");
|
||||||
|
eingabe.put("T7", "30");
|
||||||
|
eingabe.put("T8", "10");
|
||||||
|
eingabe.put("T9", "=Summe(T5:T8)");
|
||||||
|
assertEquals("30", eingabe.get("T9"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue