Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Models
diff options
context:
space:
mode:
Commit8cd7e44980107a3e205f67834eaea20927c6a0fe (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-05-30 15:52:23 +0300
Committer
 Syndamia <kamen@syndamia.com>
2022-05-30 15:52:23 +0300
Files012f3d4fdd2aaae7575579405f61efe7f85a7dc7 /src/Models
FMI-OOP-P2_Tables-8cd7e44980107a3e205f67834eaea20927c6a0fe.tar
FMI-OOP-P2_Tables-8cd7e44980107a3e205f67834eaea20927c6a0fe.tar.gz
FMI-OOP-P2_Tables-8cd7e44980107a3e205f67834eaea20927c6a0fe.zip
Parent7571a449b3f90e3f723488f167c25a3d095e0093 (diff)
Started work on making edit work with cells, referenced by formulas
4 files changed, 17 insertions, 12 deletions
-rw-r--r--src/Models/Cell.h4
-rw-r--r--src/Models/CellDouble.h4
-rw-r--r--src/Models/CellFormula.cpp15
-rw-r--r--src/Models/CellFormula.h6
diff --git a/src/Models/Cell.h b/src/Models/Cell.h
index 73a6071..4bed4de 100644
--- a/src/Models/Cell.h
+++ b/src/Models/Cell.h
@@ -6,8 +6,8 @@
class Cell {
public:
- virtual double getNumeralValue() = 0;
- virtual String getValueForPrint() = 0;
+ virtual double getNumeralValue() const = 0;
+ virtual String getValueForPrint() const = 0;
virtual void parseAndSetValue(const char* str) = 0;
virtual void readFromFile(std::ifstream& file) = 0;
virtual void writeToFile(std::ofstream& file) = 0;
diff --git a/src/Models/CellDouble.h b/src/Models/CellDouble.h
index 6396089..e81c8b2 100644
--- a/src/Models/CellDouble.h
+++ b/src/Models/CellDouble.h
@@ -10,8 +10,8 @@ public:
CellDouble() = default;
CellDouble(double value);
- double getNumeralValue() override;
- String getValueForPrint() override;
+ double getNumeralValue() const override;
+ String getValueForPrint() const override;
void parseAndSetValue(const char* str) override;
void readFromFile(std::ifstream& file) override;
void writeToFile(std::ofstream& file) override;
diff --git a/src/Models/CellFormula.cpp b/src/Models/CellFormula.cpp
index 1ebc6d6..b3203bc 100644
--- a/src/Models/CellFormula.cpp
+++ b/src/Models/CellFormula.cpp
@@ -13,7 +13,8 @@ double pow(double x, unsigned y) {
}
double CellFormula::calculate(unsigned index) {
- double lval = formula[index].left->getNumeralValue();
+ CellLoc& c = formula[index].left;
+ double lval = (c.firstInd == -1) ? localCells[c.secondInd]->getNumeralValue() : tableCells[c.firstInd][c.secondInd].getNumeralValue();
while (formula[index].right < 0) {
switch (-formula[index++].right) {
@@ -66,21 +67,21 @@ String CellFormula::getValueForPrint() {
void CellFormula::parseAndSetValue(const char* str) {
rawFormula = String(str);
- Cell* cellToAdd;
+ CellLoc cellToAdd;
char currOp;
while (*str != '\0') {
str++;
while (*str == ' ') str++;
if (*str == 'R') {
- unsigned row = atoi(++str);
+ int row = atoi(++str);
while (*str != 'C') str++;
- unsigned col = atoi(++str);
+ int col = atoi(++str);
while (*str != ' ' && *str != '\0') str++;
while (*str == ' ') str++;
currOp = *str;
- cellToAdd = (*tableCells)[row][col];
+ cellToAdd = {row, col};
}
else {
double val = atof(str);
@@ -88,7 +89,7 @@ void CellFormula::parseAndSetValue(const char* str) {
currOp = *str;
localCells.add(CellDouble(val));
- cellToAdd = &localCells[localCells.get_count() - 1];
+ cellToAdd = {-1, (int)(localCells.get_count() - 1)};
}
if (currOp == '^') currOp *= -1;
@@ -100,7 +101,7 @@ void CellFormula::parseAndSetValue(const char* str) {
if (currOp == '-') {
formula.add({cellToAdd, '+'});
localCells.add(CellDouble(-1.0));
- formula.add({&localCells[localCells.get_count() - 1], '*' * -1});
+ formula.add({{-1, (int)(localCells.get_count() - 1)}, '*' * -1});
}
else formula.add({cellToAdd, currOp});
}
diff --git a/src/Models/CellFormula.h b/src/Models/CellFormula.h
index 2aaba0f..3387c4a 100644
--- a/src/Models/CellFormula.h
+++ b/src/Models/CellFormula.h
@@ -7,10 +7,14 @@
#include "../Generic/Pair/Pair.hpp"
class CellFormula : public Cell {
+ struct CellLoc {
+ int firstInd;
+ int secondInd;
+ };
const List<List<Cell*>>* tableCells;
List<CellDouble> localCells;
- List<Pair<Cell*, char>> formula;
+ List<Pair<CellLoc, char>> formula;
String rawFormula;
double calculate(unsigned index = 0);