From 5d6eb350a7843f8cbe87068b2240f10a27e68f56 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 17 Mar 2024 18:41:14 +0200 Subject: [w2/ex2] Added solution --- week02/Exercise2.cpp | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 week02/Exercise2.cpp (limited to 'week02') 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 + +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; +} -- cgit v1.2.3