diff options
| Commit | bc8cc2c75177151a8238ff13485127ac300d52e5 (patch) | |
|---|---|---|
| Author | 2022-05-29 15:00:17 +0300 | |
| Committer | 2022-05-29 15:00:17 +0300 | |
| Files | 607b683976b55cd4522cfcf20cdc318972cf21cc /src | |
| FMI-OOP-P2_Tables-bc8cc2c75177151a8238ff13485127ac300d52e5.tar FMI-OOP-P2_Tables-bc8cc2c75177151a8238ff13485127ac300d52e5.tar.gz FMI-OOP-P2_Tables-bc8cc2c75177151a8238ff13485127ac300d52e5.zip | ||
| Parent | 039d0d6757925f368158bf3e5026d4dd4106130b (diff) | |
Autocommit at 29.05.2022 @ 15:00
2 files changed, 11 insertions, 5 deletions
| -rw-r--r-- | src/Models/CellFormula.cpp | 14 | ||||
| -rw-r--r-- | src/main.cpp | 2 |
diff --git a/src/Models/CellFormula.cpp b/src/Models/CellFormula.cpp index 20871b8..fcc71a9 100644 --- a/src/Models/CellFormula.cpp +++ b/src/Models/CellFormula.cpp @@ -25,10 +25,13 @@ double CellFormula::calculate(unsigned index) { } } + double temp; switch(formula[index].right) { case '+' : return lval + calculate(++index); case '*' : return lval * calculate(++index); - case '/' : return lval / calculate(++index); + case '/' : temp = calculate(++index); + if (temp == 0) throw new std::logic_error(""); + return lval / temp; case '^' : return pow(lval, calculate(++index)); case '\0' : default : return lval; @@ -52,9 +55,12 @@ double CellFormula::getNumeralValue() { } String CellFormula::getValueForPrint() { - for (unsigned i = 0; i < formula.get_count(); i++) - std::cout << "[" << formula[i].left->getNumeralValue() << " " << ((formula[i].right < 0) ? (int)formula[i].right : (char)formula[i].right) << std::endl; - return String() += calculate(); + try { + return String() += calculate(); + } + catch (std::logic_error) { + return "Error"; + } } void CellFormula::parseAndSetValue(const char* str) { diff --git a/src/main.cpp b/src/main.cpp index fb12425..f9ee337 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 + R0C2 ^ R0C2"); + table.putCell(0, 3, "= 3 * R0C1 + R0C0 ^ R0C1 * 27 / R0C1 - 2 * 2.5 * 2 * 2 + R0C2 ^ R0C2"); // table.putCell(0, 3, "= 1 + 2 * 3 + 4 + 5"); UserInterface ui(&table); ui.run(); |
