diff options
Autocommit at 28.05.2022 @ 19:59
2 files changed, 14 insertions, 8 deletions
| -rw-r--r-- | src/Models/CellFormula.cpp | 19 | ||||
| -rw-r--r-- | src/Models/Table.cpp | 3 |
diff --git a/src/Models/CellFormula.cpp b/src/Models/CellFormula.cpp index 7ff8c9f..1a340e5 100644 --- a/src/Models/CellFormula.cpp +++ b/src/Models/CellFormula.cpp @@ -2,21 +2,28 @@ #include "CellDouble.h" #include <cstring> -CellFormula::CellFormula(const char* str, const List<List<Cell*>>* tableCells) : referencedCells(), formula(), rawFormula() { +CellFormula::CellFormula(const char* str, const List<List<Cell*>>* tableCells) : localCells(), formula(), rawFormula() { this->tableCells = tableCells; parseAndSetValue(str); } +double pow(double x, unsigned y) { + double res = x; + for (unsigned i = 1; i < y; i++) + res *= res; + return res; +} + double CellFormula::calculate(unsigned index) { double lval = formula[index].left->getNumeralValue(); if (formula[index].right < 0) { switch (-formula[index++].right) { - case '+' : lval += formula[index].left->getNumeralValue(); break; - case '-' : lval -= formula[index].left->getNumeralValue(); break; - case '*' : lval *= formula[index].left->getNumeralValue(); break; - case '/' : lval /= formula[index].left->getNumeralValue(); break; - // case pow : lval += formula[index].left->getNumeralValue(); break; + case '+' : lval += formula[index].left->getNumeralValue(); break; + case '-' : lval -= formula[index].left->getNumeralValue(); break; + case '*' : lval *= formula[index].left->getNumeralValue(); break; + case '/' : lval /= formula[index].left->getNumeralValue(); break; + case '^' : lval = pow(lval, formula[index].left->getNumeralValue()); break; } } diff --git a/src/Models/Table.cpp b/src/Models/Table.cpp index 0719a58..70579b4 100644 --- a/src/Models/Table.cpp +++ b/src/Models/Table.cpp @@ -11,11 +11,10 @@ Table::Table(unsigned rows, unsigned cols) { cells[i] = List<Cell*>(cols); } - bool containsNumber(const char*& str) { while (*str == ' ') str++; return ((*str == '+' || *str == '-') && (*(str + 1) >= '0' && *(str + 1) <= '9')) || - (*str >= '0' && *str <= '9'); + (*str >= '0' && *str <= '9'); // + or -, followed by digit, or it just a digit } void Table::putCell(unsigned row, unsigned col, const char* rawValue) { |
