diff options
| Commit | 039d0d6757925f368158bf3e5026d4dd4106130b (patch) | |
|---|---|---|
| Author | 2022-05-29 14:55:16 +0300 | |
| Committer | 2022-05-29 14:55:16 +0300 | |
| Files | 60e61a8a236f308ab86c71177a85e9de5ac7aedf /src | |
| FMI-OOP-P2_Tables-039d0d6757925f368158bf3e5026d4dd4106130b.tar FMI-OOP-P2_Tables-039d0d6757925f368158bf3e5026d4dd4106130b.tar.gz FMI-OOP-P2_Tables-039d0d6757925f368158bf3e5026d4dd4106130b.zip | ||
| Parent | cf3abe4c719d08564d06f37933ca984ce72ddc18 (diff) | |
Autocommit at 29.05.2022 @ 14:55
2 files changed, 10 insertions, 3 deletions
| -rw-r--r-- | src/Models/CellFormula.cpp | 11 | ||||
| -rw-r--r-- | src/main.cpp | 2 |
diff --git a/src/Models/CellFormula.cpp b/src/Models/CellFormula.cpp index 82806eb..20871b8 100644 --- a/src/Models/CellFormula.cpp +++ b/src/Models/CellFormula.cpp @@ -2,6 +2,7 @@ #include "CellDouble.h" #include <cstring> #include <iostream> +#include <stdexcept> double pow(double x, unsigned y) { if (y == 0) return 1; @@ -18,7 +19,8 @@ double CellFormula::calculate(unsigned index) { 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 '/' : if (formula[index].left->getNumeralValue() == 0) throw new std::logic_error(""); + lval /= formula[index].left->getNumeralValue(); break; case '^' : lval = pow(lval, formula[index].left->getNumeralValue()); break; } } @@ -41,7 +43,12 @@ CellFormula::CellFormula(const char* str, const List<List<Cell*>>* tableCells) : #include <iostream> double CellFormula::getNumeralValue() { - return calculate(); + try { + return calculate(); + } + catch (std::logic_error) { + return 0; + } } String CellFormula::getValueForPrint() { diff --git a/src/main.cpp b/src/main.cpp index 640bc3a..fb12425 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,7 @@ int main() { table.putCell(0, 0, "8.6"); table.putCell(0, 1, "3"); table.putCell(0, 2, "String"); - table.putCell(0, 3, "= 3 * R0C1 + R0C0 ^ R0C1 * 9 - 2 * 2.5 * 2 * 2 + 1"); + table.putCell(0, 3, "= 3 * R0C1 + R0C0 ^ R0C1 * 9 - 2 * 2.5 * 2 * 2 + R0C2 ^ R0C2"); // table.putCell(0, 3, "= 1 + 2 * 3 + 4 + 5"); UserInterface ui(&table); ui.run(); |
