diff options
| author | Syndamia <kamen@syndamia.com> | 2024-03-17 18:41:14 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-03-17 18:41:14 +0200 |
| commit | 5d6eb350a7843f8cbe87068b2240f10a27e68f56 (patch) | |
| tree | 079fde5f8914ed22f25e6378bc2e370630ff8522 /week02 | |
| parent | 9f6607ad802a11bb04937d0cc5a254d63f127102 (diff) | |
| download | oop-2023-solutions-5d6eb350a7843f8cbe87068b2240f10a27e68f56.tar oop-2023-solutions-5d6eb350a7843f8cbe87068b2240f10a27e68f56.tar.gz oop-2023-solutions-5d6eb350a7843f8cbe87068b2240f10a27e68f56.zip | |
[w2/ex2] Added solution
Diffstat (limited to 'week02')
| -rw-r--r-- | week02/Exercise2.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/week02/Exercise2.cpp b/week02/Exercise2.cpp new file mode 100644 index 0000000..c114b81 --- /dev/null +++ b/week02/Exercise2.cpp @@ -0,0 +1,112 @@ +#include <iostream> + +struct Cell { +private: + int value; + char op; + Cell* left; + Cell* right; + +public: + Cell() { + SetValue(0); + } + Cell(int value) { + SetValue(value); + } + Cell(char op, Cell* left, Cell* right) { + SetFormula(op, left, right); + } + + int Evaluate() { + switch (op) { + case '+': value = left->GetValue() + right->GetValue(); break; + case '-': value = left->GetValue() - right->GetValue(); break; + case '*': value = left->GetValue() * right->GetValue(); break; + case '/': value = left->GetValue() / right->GetValue(); break; + } + return 0; + } + + int GetValue() { + return value; + } + void SetValue(int value) { + op = '\0'; + left = right = nullptr; + this->value = value; + } + void SetFormula(char op, Cell* left, Cell* right) { + this-> op = op; + this->left = left; + this->right = right; + Evaluate(); + } +}; + +struct Table { +private: + Cell** cells; + int rows; + int cols; + + void evaluateAllCells() { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + cells[i][j].Evaluate(); + } + } + } + +public: + Table(int rows, int cols) { + this->rows = rows; + this->cols = cols; + cells = new Cell*[rows]; + for (int i = 0; i < rows; i++) { + cells[i] = new Cell[cols]; + } + } + ~Table() { + for (int i = 0; i < rows; i++) { + delete[] cells[i]; + } + delete[] cells; + } + + Cell* GetCell(int row, int col) { + return &cells[row][col]; + } + void SetCell(int row, int col, Cell cell) { + cells[row][col] = cell; + } + + void UpdateCell(int row, int col, int value) { + cells[row][col].SetValue(value); + evaluateAllCells(); + } + void UpdateCell(int row, int col, char op, Cell* left, Cell* right) { + cells[row][col].SetFormula(op, left, right); + evaluateAllCells(); + } + + void Print() { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + std::cout << "|" << cells[i][j].GetValue(); + } + std::cout << "|" << std::endl; + } + } +}; + +int main() { + Table mytable(2, 2); + mytable.SetCell(0, 0, Cell(5)); + mytable.SetCell(0, 1, Cell(7)); + mytable.SetCell(1, 0, Cell('*', mytable.GetCell(0, 0), mytable.GetCell(0, 1))); + + mytable.Print(); + + return 0; +} |
