diff options
Added my work from today's exam
23 files changed, 511 insertions, 0 deletions
diff --git a/Year 1/Object-oriented Programming/Exam 2/README.md b/Year 1/Object-oriented Programming/Exam 2/README.md new file mode 100644 index 0000000..b280594 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/README.md @@ -0,0 +1,2 @@ +**Exam Date**: 25.05.2022 +**Grade**: TBD diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.cpp new file mode 100644 index 0000000..d065206 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.cpp @@ -0,0 +1,15 @@ +#include "AirTravel.h" + +AirTravel::AirTravel() : AirTravel("", 0) {} + +AirTravel::AirTravel(const char* flightNumber, unsigned duration) : flightNumber(flightNumber) { + this->duration = duration; +} + +String AirTravel::getFlightNumber() const { + return flightNumber; +} + +unsigned AirTravel::getDuration() const { + return duration; +} diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.h b/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.h new file mode 100644 index 0000000..c7f24ca --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.h @@ -0,0 +1,18 @@ +#ifndef HEADER_AIRTRAVEL +#define HEADER_AIRTRAVEL + +#include "String.h" + +class AirTravel { + String flightNumber; + unsigned duration; + +public: + AirTravel(); + AirTravel(const char* flightNumber, unsigned duration); + + String getFlightNumber() const; + unsigned getDuration() const; +}; + +#endif diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.cpp new file mode 100644 index 0000000..6632f00 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.cpp @@ -0,0 +1,22 @@ +#include "CarTravel.h" + +CarTravel::CarTravel() : CarTravel(RegNumb(), 0, 0) {} + +CarTravel::CarTravel(RegNumb regNumber, unsigned distance, unsigned duration) { + this->regNumb = regNumber; + this->distance = distance; + this->duration = duration; +} + +RegNumb CarTravel::getRegNumb() const { + return regNumb; +} + +unsigned CarTravel::getDistance() const { + return distance; +} + +unsigned CarTravel::getDuration() const { + return duration; +} + diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.h b/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.h new file mode 100644 index 0000000..b483d66 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.h @@ -0,0 +1,20 @@ +#ifndef HEADER_CARTRAVEL +#define HEADER_CARTRAVEL + +#include "RegistryNumber.h" + +class CarTravel { + RegNumb regNumb; + unsigned distance; + unsigned duration; + +public: + CarTravel(); + CarTravel(RegNumb regNumber, unsigned distance, unsigned duration); + + RegNumb getRegNumb() const; + unsigned getDistance() const; + unsigned getDuration() const; +}; + +#endif diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.cpp new file mode 100644 index 0000000..dbe315b --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.cpp @@ -0,0 +1,66 @@ +#include "CombinedTrip.h" +#include <cstring> +#include <iostream> + +void CombinedTrip::free() { + for (unsigned i = 0; i < destinationsCount; i++) + delete[] destinations[i]; + delete[] destinations; +} + +void CombinedTrip::copyFrom(const CombinedTrip& other) { + destinationsCount = other.destinationsCount; + destinations = new char*[destinationsCount]; + for (unsigned length = 0, i = 0; i < destinationsCount; i++) { + length = strlen(other.destinations[i]); + destinations[i] = new char[length + 1]; + strncpy(destinations[i], other.destinations[i], length + 1); + } +} + +CombinedTrip::CombinedTrip(RegNumb carRegNumb, unsigned carTravelDistance, unsigned carTravelDuration, const char* flightNumber, unsigned flightDuration, const char** destinations, unsigned destinationsCount, unsigned freeTime) : carTravel(carRegNumb, carTravelDistance, carTravelDuration), airTravel(flightNumber, flightDuration) { + if (destinations == nullptr) { + destinations = nullptr; + destinationsCount = freeTime = 0; + return; + } + + this->destinationsCount = destinationsCount; + this->destinations = new char*[destinationsCount]; + for (unsigned length = 0, i = 0; i < destinationsCount; i++) { + length = strlen(destinations[i]); + this->destinations[i] = new char[length + 1]; + strncpy(this->destinations[i], destinations[i], length + 1); + } + this->freeTime = freeTime; +} + +unsigned CombinedTrip::getDuration() const { + return carTravel.getDuration() + freeTime + airTravel.getDuration(); +} + +void CombinedTrip::print() { + std::cout << "Air Travel || Flight number: " << airTravel.getFlightNumber() << " | Duration: " << airTravel.getDuration() << std::endl; + std::cout << "Car Travel || Registry number: " << carTravel.getRegNumb() << carTravel.getRegNumb() << " | Durationt: " << carTravel.getDuration() << " | Distance: " << carTravel.getDistance(); + std::cout << "Free time: " << freeTime << std::endl << "Destinations: "; + for (unsigned i = 0; i < destinationsCount; i++) + std::cout << destinations[i] << std::endl; +} + +CombinedTrip::CombinedTrip() : CombinedTrip(RegNumb(), 0, 0, "", 0, nullptr, 0, 0) {} + +CombinedTrip::CombinedTrip(const CombinedTrip& ct) { + copyFrom(ct); +} + +CombinedTrip& CombinedTrip::operator=(const CombinedTrip& ct) { + if (this != &ct) { + free(); + copyFrom(ct); + } + return *this; +} + +CombinedTrip::~CombinedTrip() { + free(); +} diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.h b/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.h new file mode 100644 index 0000000..77f7090 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.h @@ -0,0 +1,29 @@ +#ifndef HEADER_COMBINEDTRIP +#define HEADER_COMBINEDTRIP + +#include "String.h" +#include "CarTravel.h" +#include "AirTravel.h" + +class CombinedTrip { + CarTravel carTravel; + AirTravel airTravel; + char** destinations; // цитирам "представя се чрез динамичен масив от низове от тип char*" + unsigned destinationsCount; + unsigned freeTime; + + void free(); + void copyFrom(const CombinedTrip& other); +public: + CombinedTrip(RegNumb carRegNumb, unsigned carTravelDistance, unsigned carTravelDuration, const char* flightNumber, unsigned flightDuration, const char** destinations, unsigned destinationsCount, unsigned freeTime); + + unsigned getDuration() const; + void print(); + + CombinedTrip(); + CombinedTrip(const CombinedTrip& ct); + CombinedTrip& operator=(const CombinedTrip& ct); + ~CombinedTrip(); +}; + +#endif diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.cpp new file mode 100644 index 0000000..0d6a3bd --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.cpp @@ -0,0 +1,15 @@ +#include "RegistryNumber.h" +#include <ostream> + +RegNumb::RegNumb() : RegNumb(0, 0, 0, 0) {} + +RegNumb::RegNumb(int regNumber1, int regNumber2, int regNumber3, int regNumber4) { + regNumb[0] = regNumber1; + regNumb[1] = regNumber2; + regNumb[2] = regNumber3; + regNumb[3] = regNumber4; +} + +std::ostream& operator<<(std::ostream& ostr, const RegNumb& regNumb) { + return ostr << regNumb.regNumb[0] << regNumb.regNumb[1] << regNumb.regNumb[2] << regNumb.regNumb[3]; +} diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.h b/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.h new file mode 100644 index 0000000..5101551 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.h @@ -0,0 +1,17 @@ +#ifndef HEADER_REGNUM +#define HEADER_REGNUM + +#include <ostream> + +class RegNumb { + int regNumb[4]; + +public: + RegNumb(); + RegNumb(int regNumber1, int regNumber2, int regNumber3, int regNumber4); + + friend std::ostream& operator<<(std::ostream& ostr, const RegNumb& regNumb); +}; + + +#endif diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp new file mode 100644 index 0000000..693e638 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp @@ -0,0 +1,40 @@ +#include "String.h" +#include <cstring> + +void String::copyFrom(const String& other) { + length = other.length; + str = new char[length + 1]; + strncpy(str, other.str, length + 1); +} + +void String::free() { + delete[] str; +} + +String::String(const char* str) { + length = strlen(str); + this->str = new char[length + 1]; + strncpy(this->str, str, length + 1); +} + +String::String() : String("") {} + +String::String(const String& other) { + copyFrom(other); +} + +String& String::operator=(const String& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +String::~String() { + free(); +} + +std::ostream& operator<<(std::ostream& ostr, const String& str) { + return ostr << str.str; +} diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/String.h b/Year 1/Object-oriented Programming/Exam 2/ex1/String.h new file mode 100644 index 0000000..997093c --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/String.h @@ -0,0 +1,23 @@ +#ifndef HEADER_STRING +#define HEADER_STRING + +#include <ostream> + +class String { + char* str; + unsigned length; + + void copyFrom(const String& other); + void free(); +public: + String(const char* str); + + String(); + String(const String& other); + String& operator=(const String& other); + ~String(); + + friend std::ostream& operator<<(std::ostream& ostr, const String& str); +}; + +#endif diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/main.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/main.cpp new file mode 100644 index 0000000..0ea5920 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/main.cpp @@ -0,0 +1,20 @@ +#include "CombinedTrip.h" +#include <cstring> + +int main() { + while (1) { + char** dest = new char*[3]; + for (unsigned i = 0; i < 3; i++) + dest[i] = new char[2]; + strcpy(dest[0], "A"); + strcpy(dest[1],"B"); + strcpy(dest[2], "C"); + + CombinedTrip ct(RegNumb(1, 2, 3, 4), 1, 5, "ASKDJHS", 35, (const char**)dest, 3, 57); + ct.print(); + + for (unsigned i = 0; i < 3; i++) + delete[] dest[i]; + delete[] dest; + } +} diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/Event.cpp b/Year 1/Object-oriented Programming/Exam 2/ex2/Event.cpp new file mode 100644 index 0000000..90c3740 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/Event.cpp @@ -0,0 +1,18 @@ +#include "Event.h" + +void Event::setStartTime(unsigned startTime) { + this->startTime = startTime; +} + +void Event::setEndTime(unsigned endTime) { + this->endTime = endTime; +} + +Event::Event(unsigned startTime, unsigned endTime) { + setStartTime(startTime); + setEndTime(endTime); +} + +bool Event::isHappeningAt(unsigned time) const { + return time >= startTime && time <= endTime; +} diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/Event.h b/Year 1/Object-oriented Programming/Exam 2/ex2/Event.h new file mode 100644 index 0000000..a02f60c --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/Event.h @@ -0,0 +1,20 @@ +#ifndef HEADER_EVENT +#define HEADER_EVENT + +class Event { + unsigned startTime; + unsigned endTime; + +protected: + void setStartTime(unsigned startTime); + void setEndTime(unsigned endTime); + +public: + Event(unsigned startTime, unsigned endTime); + virtual bool isHappeningAt(unsigned time) const; + + virtual Event* clone() const = 0; + virtual ~Event() = default; +}; + +#endif diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/EventManager.cpp b/Year 1/Object-oriented Programming/Exam 2/ex2/EventManager.cpp new file mode 100644 index 0000000..cf96756 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/EventManager.cpp @@ -0,0 +1,54 @@ +#include "EventManager.h" + +void EventManager::copyFrom(const EventManager& other) { + events = new Event*[other.maxCount]; + for (unsigned i = 0; i < other.count; i++) + events[i] = other.events[i]->clone(); + count = other.count; + maxCount = other.maxCount; +} + +void EventManager::free() { + for (unsigned i = 0; i < count; i++) + delete events[i]; + delete[] events; +} + +EventManager::EventManager(unsigned maxCount) { + events = new Event*[maxCount]; + this->maxCount = maxCount; + count = 0; +} + +bool EventManager::addEvent(const Event* event) { + if (maxCount == count) + return false; + + events[count++] = event->clone(); + return true; +} + +unsigned EventManager::ongoingEvents(unsigned time) { + unsigned ongoing = 0; + for (unsigned i = 0; i < count; i++) + ongoing += events[i]->isHappeningAt(time); + return ongoing; +} + +EventManager::EventManager() : EventManager(1) {} + +EventManager::EventManager(const EventManager& other) { + copyFrom(other); +} + +EventManager& EventManager::operator=(const EventManager& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +EventManager::~EventManager() { + free(); +} diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/EventManager.h b/Year 1/Object-oriented Programming/Exam 2/ex2/EventManager.h new file mode 100644 index 0000000..f6721b7 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/EventManager.h @@ -0,0 +1,20 @@ +#include "Event.h" + +class EventManager { + Event** events; + unsigned count; + unsigned maxCount; + + void copyFrom(const EventManager& other); + void free(); +public: + EventManager(unsigned maxCount); + + bool addEvent(const Event* event); + unsigned ongoingEvents(unsigned time); + + EventManager(); + EventManager(const EventManager& other); + EventManager& operator=(const EventManager& other); + ~EventManager(); +}; diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithFixedIntermission.cpp b/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithFixedIntermission.cpp new file mode 100644 index 0000000..17078d9 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithFixedIntermission.cpp @@ -0,0 +1,20 @@ +#include "EventWithFixedIntermission.h" +#include <iostream> + +EventWithFixedIntermission::EventWithFixedIntermission(unsigned startTime, unsigned endTime, unsigned intermissionStart) : Event(0, 0) { + this->intermissionStart = intermissionStart; + if (endTime - startTime <= 240) { + Event::setStartTime(startTime); + Event::setEndTime(endTime); + } + else + std::cout << "Event cannot last for more than 4 hours (240 minutes)!" << std::endl; +} + +bool EventWithFixedIntermission::isHappeningAt(unsigned time) const { + return (time < intermissionStart || time > intermissionStart + 20) && Event::isHappeningAt(time); +} + +EventWithFixedIntermission* EventWithFixedIntermission::clone() const { + return new EventWithFixedIntermission(*this); +} diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithFixedIntermission.h b/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithFixedIntermission.h new file mode 100644 index 0000000..4db3c1a --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithFixedIntermission.h @@ -0,0 +1,11 @@ +#include "Event.h" + +class EventWithFixedIntermission : public Event { + unsigned intermissionStart; + +public: + EventWithFixedIntermission(unsigned startTime, unsigned endTime, unsigned intermissionStart); + virtual bool isHappeningAt(unsigned time) const override; + + EventWithFixedIntermission* clone() const override; +}; diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithIntermission.cpp b/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithIntermission.cpp new file mode 100644 index 0000000..2681b71 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithIntermission.cpp @@ -0,0 +1,28 @@ +#include "EventWithIntermission.h" +#include <iostream> + +EventWithIntermission::EventWithIntermission(unsigned startTime, unsigned endTime, unsigned intermissionStart, unsigned intermissionEnd) : Event(0, 0) { + this->intermissionStart = this->intermissionEnd = 0; + + if (endTime - startTime <= 360) { + Event::setStartTime(startTime); + Event::setEndTime(endTime); + } + else + std::cout << "Event cannot last for more than 6 hours (360 minutes)!" << std::endl; + + if (intermissionEnd - intermissionStart >= 30 && intermissionEnd - intermissionStart <= 60) { + this->intermissionStart = intermissionStart; + this->intermissionEnd = intermissionEnd; + } + else + std::cout << "Event intermission must last between 30 and 60 minutes!" << std::endl; +} + +bool EventWithIntermission::isHappeningAt(unsigned time) const { + return (time < intermissionStart || time > intermissionEnd) && Event::isHappeningAt(time); +} + +EventWithIntermission* EventWithIntermission::clone() const { + return new EventWithIntermission(*this); +} diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithIntermission.h b/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithIntermission.h new file mode 100644 index 0000000..dbc6b4c --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/EventWithIntermission.h @@ -0,0 +1,12 @@ +#include "Event.h" + +class EventWithIntermission : public Event { + unsigned intermissionStart; + unsigned intermissionEnd; + +public: + EventWithIntermission(unsigned startTime, unsigned endTime, unsigned intermissionStart, unsigned intermissionEnd); + virtual bool isHappeningAt(unsigned time) const override; + + EventWithIntermission* clone() const override; +}; diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/SimpleEvent.cpp b/Year 1/Object-oriented Programming/Exam 2/ex2/SimpleEvent.cpp new file mode 100644 index 0000000..bca2bdf --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/SimpleEvent.cpp @@ -0,0 +1,15 @@ +#include "SimpleEvent.h" +#include <iostream> + +SimpleEvent::SimpleEvent(unsigned startTime, unsigned endTime) : Event(0, 0) { + if (endTime - startTime <= 120) { + Event::setStartTime(startTime); + Event::setEndTime(endTime); + } + else + std::cout << "Event cannot last for more than 2 hours (120 minutes)!" << std::endl; +} + +SimpleEvent* SimpleEvent::clone() const { + return new SimpleEvent(*this); +} diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/SimpleEvent.h b/Year 1/Object-oriented Programming/Exam 2/ex2/SimpleEvent.h new file mode 100644 index 0000000..b98256d --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/SimpleEvent.h @@ -0,0 +1,8 @@ +#include "Event.h" + +class SimpleEvent : public Event { +public: + SimpleEvent(unsigned startTime, unsigned endTime); + + SimpleEvent* clone() const override; +}; diff --git a/Year 1/Object-oriented Programming/Exam 2/ex2/main.cpp b/Year 1/Object-oriented Programming/Exam 2/ex2/main.cpp new file mode 100644 index 0000000..1790a48 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex2/main.cpp @@ -0,0 +1,18 @@ +#include "EventManager.h" +#include "EventWithFixedIntermission.h" +#include "EventWithIntermission.h" +#include "SimpleEvent.h" +#include <iostream> + +int main() { + EventManager es(3); + SimpleEvent e1(10, 20); + EventWithFixedIntermission e2(30, 50, 35); + EventWithIntermission e3(90, 200, 95, 126); + + es.addEvent(&e1); + es.addEvent(&e2); + es.addEvent(&e3); + + std::cout << es.ongoingEvents(91) << std::endl; +} |
