diff options
| author | Syndamia <kamen@syndamia.com> | 2024-03-07 18:00:02 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-03-07 18:00:02 +0200 |
| commit | a2c7c3ac1fb6e3c877f58f2bab1754f4d93e522f (patch) | |
| tree | b44282a01417c35f47f543ca61ccc2de2e8dbb40 /week03 | |
| parent | c4efdf0a5b9af3d70d75f4ab642305d44e27ba1f (diff) | |
| download | oop-2023-solutions-a2c7c3ac1fb6e3c877f58f2bab1754f4d93e522f.tar oop-2023-solutions-a2c7c3ac1fb6e3c877f58f2bab1754f4d93e522f.tar.gz oop-2023-solutions-a2c7c3ac1fb6e3c877f58f2bab1754f4d93e522f.zip | |
[w3] Solved exercises 5-7
Diffstat (limited to 'week03')
| -rw-r--r-- | week03/Exercise5.cpp | 96 | ||||
| -rw-r--r-- | week03/Exercise6.cpp | 88 | ||||
| -rw-r--r-- | week03/Exercise7.cpp | 58 |
3 files changed, 242 insertions, 0 deletions
diff --git a/week03/Exercise5.cpp b/week03/Exercise5.cpp new file mode 100644 index 0000000..28721a4 --- /dev/null +++ b/week03/Exercise5.cpp @@ -0,0 +1,96 @@ +#include <cstring> + +struct Car { +private: + char* model; + unsigned int doors; + unsigned int seats; + +public: + Car() { + model = nullptr; + doors = 0; + seats = 0; + } + Car(const char* model, unsigned int doors, unsigned int seats) { + this->model = nullptr; + setModel(model); + setDoors(doors); + setSeats(seats); + } + ~Car() { + delete[] model; + } + + const char* getModel() { + return model; + } + void setModel(const char* newModel) { + delete[] model; + model = new char[strlen(newModel) + 1]; + strcpy(model, newModel); + } + + unsigned int getDoors() { + return doors; + } + void setDoors(unsigned int newDoorsCount) { + doors = newDoorsCount; + } + + unsigned int getSeats() { + return seats; + } + void setSeats(unsigned int newSeatsCount) { + seats = newSeatsCount; + } + + void CopyDataTo(Car& destination) { + destination.setModel(model); + destination.setDoors(doors); + destination.setSeats(seats); + } +}; + +struct TrafficJam { +private: + Car* cars; + int size; + int lastIndex; + + void resize() { + Car* newCars = new Car[size * 2]; + for (int i = 0; i < size; i++) { + cars[i].CopyDataTo(newCars[i]); + } + delete[] cars; + cars = newCars; + size *= 2; + } + +public: + TrafficJam() { + size = 3; + cars = new Car[size]; + lastIndex = 0; + } + ~TrafficJam() { + delete[] cars; + } + + void AddCar(Car& car) { + if (lastIndex == size) + resize(); + car.CopyDataTo(cars[lastIndex++]); + } +}; + +int main() { + TrafficJam tj; + + Car c1("Toyota", 4, 5); + Car c2("Ferarri", 2, 2); + + tj.AddCar(c1); + tj.AddCar(c2); +} diff --git a/week03/Exercise6.cpp b/week03/Exercise6.cpp new file mode 100644 index 0000000..3d3c0c0 --- /dev/null +++ b/week03/Exercise6.cpp @@ -0,0 +1,88 @@ +#include <iostream> + +const int WHITE = 0; +const int BLACK = 1; + +struct ChessPiece { + int color; + char figure; + + ChessPiece(int color, int figure) { + this->color = color; + this->figure = figure; + } +}; + +struct ChessBoard { +private: + ChessPiece*** board; + int N; + + void setColumnPieces(int column, char pieceChar) { + board[column][0] = new ChessPiece(WHITE, pieceChar); + board[column][1] = new ChessPiece(WHITE, 'P'); + for (int i = 2; i < N-2; i++) { + board[column][i] = nullptr; + } + board[column][N-2] = new ChessPiece(BLACK, 'P'); + board[column][N-1] = new ChessPiece(BLACK, pieceChar); + } + + void fillBoardWithPieces() { + for (int column = 0; column < N; column++) { + switch (column % 8) { + case 0: case 7: + setColumnPieces(column, 'R'); break; + case 1: case 6: + setColumnPieces(column, 'K'); break; + case 2: case 5: + setColumnPieces(column, 'B'); break; + case 3: + setColumnPieces(column, 'I'); break; + case 4: + setColumnPieces(column, 'Q'); break; + } + } + } + +public: + ChessBoard(int N) { + if (N < 5) { + throw "Board size cannot be less than 5!"; + } + + board = new ChessPiece**[N]; + for (int i = 0; i < N; i++) { + board[i] = new ChessPiece*[N]; + } + this->N = N; + + fillBoardWithPieces(); + } + ~ChessBoard() { + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + delete board[i][j]; + } + delete[] board[i]; + } + delete[] board; + } + + void Print() { + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (board[j][i] == nullptr) + std::cout << ' '; + else + std::cout << board[j][i]->figure; + } + std::cout << std::endl; + } + } +}; + +int main() { + ChessBoard cb(10); + cb.Print(); +} diff --git a/week03/Exercise7.cpp b/week03/Exercise7.cpp new file mode 100644 index 0000000..4c7975b --- /dev/null +++ b/week03/Exercise7.cpp @@ -0,0 +1,58 @@ +#include <iostream> + +int max(int a, int b) { + return (a > b) ? a : b; +} + +struct Interval { +private: + int left; + int right; +public: + Interval(int left, int right) { + if (left > right) throw "Left cannot be greater than right!"; + + this->left = left; + this->right = right; + } + + int getLeft() { + return left; + } + int getRight() { + return right; + } + + bool TryMergeWith(Interval*& other) { + if (this == nullptr || other == nullptr || other == this || right < other->left || other->right < left) + return false; + + left = max(left, other->left); + right = max(right, other->right); + + delete other; + other = nullptr; + + return true; + } +}; + +int main() { + Interval* ivs[] = { new Interval(0, 3), new Interval(3, 5), new Interval(3, 8), + new Interval(10, 15), + new Interval(30, 80), }; + int size = 5; + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + ivs[i]->TryMergeWith(ivs[j]); + } + } + + for (int i = 0; i < size; i++) { + if (ivs[i] != nullptr) { + std::cout << "[" << ivs[i]->getLeft() << ", " << ivs[i]->getRight() << "]"; + delete ivs[i]; + } + } + std::cout << std::endl; +} |
