aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--week05/Exercise1.cpp32
-rw-r--r--week05/Exercise2.cpp46
-rw-r--r--week05/Exercise3.cpp45
-rw-r--r--week05/Exercise4.cpp67
-rw-r--r--week05/Exercise5.cpp103
-rw-r--r--week05/Exercise6.cpp74
-rw-r--r--week05/Exercise7.cpp94
7 files changed, 461 insertions, 0 deletions
diff --git a/week05/Exercise1.cpp b/week05/Exercise1.cpp
new file mode 100644
index 0000000..28fc290
--- /dev/null
+++ b/week05/Exercise1.cpp
@@ -0,0 +1,32 @@
+struct Patient {
+ unsigned id;
+ char name[1024];
+};
+
+struct Hospital {
+private:
+ Patient* patients;
+ unsigned maxPatients;
+ unsigned lastIndex;
+
+public:
+ Hospital(unsigned maxPatients) {
+ this->maxPatients = maxPatients;
+ patients = new Patient[maxPatients];
+ lastIndex = 0;
+ }
+ ~Hospital() {
+ delete[] patients;
+ }
+
+ void AddPatient(const Patient& newPatient) {
+ if (lastIndex == maxPatients) return;
+ patients[lastIndex++] = newPatient;
+ }
+
+ // По принцип логиката трябва да е по-сложна, да се маха пациент по индекс и
+ // след това да се изместват останалите, но за целите на задачата това не е нужно
+ void RemovePatient() {
+ lastIndex--;
+ }
+};
diff --git a/week05/Exercise2.cpp b/week05/Exercise2.cpp
new file mode 100644
index 0000000..0648931
--- /dev/null
+++ b/week05/Exercise2.cpp
@@ -0,0 +1,46 @@
+struct FloatArray {
+private:
+ float* arr;
+ unsigned size;
+
+ void free() {
+ delete[] arr;
+ }
+ void copyFrom(const FloatArray& other) {
+ this->size = other.size;
+ this->arr = new float[size];
+ for (int i = 0; i < size; i++) {
+ this->arr[i] = other.arr[i];
+ }
+ }
+
+public:
+ FloatArray(unsigned size) {
+ this->size = size;
+ arr = new float[size];
+ }
+ ~FloatArray() {
+ free();
+ }
+ FloatArray(const FloatArray& other) {
+ copyFrom(other);
+ }
+ FloatArray& operator=(const FloatArray& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ float GetElem(unsigned index) {
+ if (index >= size) return 0.0;
+
+ return arr[index];
+ }
+ void SetElem(unsigned index, float value) {
+ if (index >= size) return;
+
+ arr[index] = value;
+ }
+};
diff --git a/week05/Exercise3.cpp b/week05/Exercise3.cpp
new file mode 100644
index 0000000..5373540
--- /dev/null
+++ b/week05/Exercise3.cpp
@@ -0,0 +1,45 @@
+#include <cstring>
+
+struct Email {
+private:
+ char address[128];
+ char* contents;
+
+ void free() {
+ delete[] contents;
+ }
+ void copyFrom(const Email& other) {
+ strcpy(this->address, other.address);
+
+ int contSize = strlen(other.contents);
+ this->contents = new char[contSize+1];
+ strcpy(this->contents, other.contents);
+ }
+
+public:
+ Email(char address[128], const char* contents) {
+ strcpy(this->address, address);
+
+ int contSize = strlen(contents);
+ this->contents = new char[contSize + 1];
+ strcpy(this->contents, contents);
+ }
+
+ Email() {
+ address[0] = '\0';
+ contents = nullptr;
+ }
+ ~Email() {
+ free();
+ }
+ Email(const Email& other) {
+ copyFrom(other);
+ }
+ Email& operator=(const Email& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+};
diff --git a/week05/Exercise4.cpp b/week05/Exercise4.cpp
new file mode 100644
index 0000000..6fbb5f6
--- /dev/null
+++ b/week05/Exercise4.cpp
@@ -0,0 +1,67 @@
+struct Receipt {
+private:
+ float* prices;
+ unsigned allocated;
+ unsigned size;
+
+ void resize() {
+ allocated *= 2;
+ float* morePrices = new float[allocated];
+ for (int i = 0; i < size; i++) {
+ morePrices[i] = prices[i];
+ }
+ delete[] prices;
+ prices = morePrices;
+ }
+
+ void free() {
+ delete[] prices;
+ }
+ void copyFrom(const Receipt& other) {
+ this->allocated = other.allocated;
+ this->size = other.size;
+
+ this->prices = new float[allocated];
+ for (int i = 0; i < size; i++) {
+ this->prices[i] = other.prices[i];
+ }
+ }
+
+public:
+ Receipt() {
+ allocated = 2;
+ size = 0;
+ prices = new float[allocated];
+ }
+ ~Receipt() {
+ free();
+ }
+ Receipt(const Receipt& other) {
+ copyFrom(other);
+ }
+ Receipt& operator=(const Receipt& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ void AddItemPrice(float price) {
+ if (price < 0.0) return;
+
+ if (this->allocated == this->size) {
+ resize();
+ }
+ this->prices[size++] = price;
+ }
+
+ void RemoveItemPrice(unsigned index) {
+ if (index >= size) return;
+
+ size--;
+ for (int i = index; i < size; i++) {
+ this->prices[i] = this->prices[i+1];
+ }
+ }
+};
diff --git a/week05/Exercise5.cpp b/week05/Exercise5.cpp
new file mode 100644
index 0000000..8117503
--- /dev/null
+++ b/week05/Exercise5.cpp
@@ -0,0 +1,103 @@
+#include <cstring>
+
+struct Checklist {
+private:
+ bool checked;
+ char* contents;
+
+ Checklist* subchecklists;
+ unsigned allocated;
+ unsigned size;
+
+ void resize() {
+ allocated *= 2;
+ Checklist* biggerSubs = new Checklist[allocated];
+ for (int i = 0; i < size; i++) {
+ biggerSubs[i] = subchecklists[i];
+ }
+ delete[] subchecklists;
+ subchecklists = biggerSubs;
+ }
+
+ void free() {
+ delete[] contents;
+ delete[] subchecklists;
+ }
+ void copyFrom(const Checklist& other) {
+ this->checked = other.checked;
+
+ unsigned contSize = strlen(other.contents);
+ this->contents = new char[contSize+1];
+ strcpy(this->contents, other.contents);
+
+ this->allocated = other.allocated;
+ this->size = other.size;
+ this->subchecklists = new Checklist[allocated];
+ for (int i = 0; i < size; i++) {
+ this->subchecklists[i] = other.subchecklists[i];
+ }
+ }
+
+public:
+ Checklist(const char* contents) {
+ checked = false;
+
+ unsigned contSize = strlen(contents);
+ this->contents = new char[contSize+1];
+ strcpy(this->contents, contents);
+
+ this->allocated = 2;
+ this->size = 0;
+ this->subchecklists = new Checklist[allocated];
+ }
+
+ Checklist() {
+ checked = false;
+ contents = nullptr;
+ subchecklists = nullptr;
+ allocated = size = 0;
+ }
+ ~Checklist() {
+ free();
+ }
+ Checklist(const Checklist& other) {
+ copyFrom(other);
+ }
+ Checklist& operator=(const Checklist& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ void AddChecklist(const Checklist& newChecklist) {
+ if (allocated == size) {
+ resize();
+ }
+ subchecklists[size++] = newChecklist;
+ }
+ void RemoveChecklist(unsigned index) {
+ if (index >= size) return;
+
+ size--;
+ for (int i = index; i < size; i++) {
+ subchecklists[i] = subchecklists[i+1];
+ }
+ }
+
+ void SwapRight(unsigned index) {
+ if (index >= size-1) return;
+
+ Checklist temp = subchecklists[index];
+ subchecklists[index] = subchecklists[index+1];
+ subchecklists[index+1] = temp;
+ }
+ void SwapLeft(unsigned index) {
+ if (index == 0 || index >= size) return;
+
+ Checklist temp = subchecklists[index];
+ subchecklists[index] = subchecklists[index-1];
+ subchecklists[index-1] = temp;
+ }
+};
diff --git a/week05/Exercise6.cpp b/week05/Exercise6.cpp
new file mode 100644
index 0000000..9f8183e
--- /dev/null
+++ b/week05/Exercise6.cpp
@@ -0,0 +1,74 @@
+#include <cstring>
+
+struct PhoneBook {
+private:
+ char** entries; // Масив от указатели char*
+ unsigned allocated;
+ unsigned size;
+
+ void resize() {
+ allocated *= 2;
+ char** moreEntries = new char*[allocated];
+ for (int i = 0; i < size; i++) {
+ moreEntries[i] = entries[i];
+ }
+ delete[] entries;
+ entries = moreEntries;
+ }
+
+ void free() {
+ for (int i = 0; i < size; i++) {
+ delete[] entries[i];
+ }
+ delete[] entries;
+ }
+ void copyFrom(const PhoneBook& other) {
+ this->allocated = other.allocated;
+ this->size = other.size;
+ this->entries = new char*[allocated];
+ for (int i = 0; i < size; i++) {
+ this->entries[i] = new char[strlen(other.entries[i])+1];
+ strcpy(this->entries[i], other.entries[i]);
+ }
+ }
+
+public:
+ PhoneBook() {
+ entries = nullptr;
+ allocated = size = 0;
+ }
+ ~PhoneBook() {
+ free();
+ }
+ PhoneBook(const PhoneBook& other) {
+ copyFrom(other);
+ }
+ PhoneBook& operator=(const PhoneBook& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ void InsertPerson(const char* entry) {
+ if (allocated == size) {
+ resize();
+ }
+
+ int index = 0;
+ while (index < size) {
+ if (strcmp(entry, entries[index]) < 0) {
+ break;
+ }
+ index++;
+ }
+
+ size++;
+ for (int i = size; i > index; i--) {
+ entries[i] = entries[i-1];
+ }
+ entries[index] = new char[strlen(entry)+1];
+ strcpy(entries[index], entry);
+ }
+};
diff --git a/week05/Exercise7.cpp b/week05/Exercise7.cpp
new file mode 100644
index 0000000..38da2e0
--- /dev/null
+++ b/week05/Exercise7.cpp
@@ -0,0 +1,94 @@
+#include <iostream>
+#include <random>
+
+int rand(int min, int max) {
+ return rand() % max + min;
+}
+
+struct WaterMeter {
+private:
+ double usedWater;
+
+public:
+ WaterMeter() {
+ usedWater = 0.0;
+ }
+
+ void AddWater(double amount) {
+ usedWater += amount;
+ }
+ void Print() {
+ std::cout << usedWater;
+ }
+};
+
+struct Building {
+private:
+ WaterMeter* meters;
+ unsigned size;
+
+ void free() {
+ delete[] meters;
+ }
+ void copyFrom(const Building& other) {
+ this->size = other.size;
+ this->meters = new WaterMeter[size];
+ for (int i = 0; i < size; i++) {
+ this->meters[i] = other.meters[i];
+ }
+ }
+
+public:
+ Building(unsigned size) {
+ this->size = size;
+ meters = new WaterMeter[size];
+ }
+
+ Building() {
+ meters = nullptr;
+ size = 0;
+ }
+ ~Building() {
+ free();
+ }
+ Building(const Building& other) {
+ copyFrom(other);
+ }
+ Building& operator=(const Building& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+
+ void SimulateDay() {
+ for (int i = 0; i < size; i++) {
+ meters[i].AddWater(rand(0, 999) / 100.0);
+ }
+ }
+ void SimulateMonth() {
+ WaterMeter** reports = new WaterMeter*[5];
+ for (int i = 0; i < 5; i++) {
+ reports[i] = new WaterMeter[size];
+ }
+
+ for (int day = 0; day <= 30; day++) {
+ SimulateDay();
+ if (day % 5 == 0 && day > 0) {
+ unsigned reportIndex = day / 5 - 1;
+ for (int i = 0; i < size; i++) {
+ reports[reportIndex][i] = meters[i];
+ }
+ }
+ }
+
+ for (int meterI = 0; meterI < size; meterI++) {
+ for (int reportI = 0; reportI < 5; reportI++) {
+ reports[reportI][meterI].Print();
+ std::cout << " ";
+ }
+ std::cout << std::endl;
+ }
+ }
+};