diff options
feat(Week08): Add solutions
12 files changed, 887 insertions, 0 deletions
| -rw-r--r-- | Week08/Exercise01/Hangar.cpp | 165 | ||||
| -rw-r--r-- | Week08/Exercise01/Hangar.h | 41 | ||||
| -rw-r--r-- | Week08/Exercise01/LaunchSite.cpp | 26 | ||||
| -rw-r--r-- | Week08/Exercise01/LaunchSite.h | 20 | ||||
| -rw-r--r-- | Week08/Exercise01/Rocket.cpp | 154 | ||||
| -rw-r--r-- | Week08/Exercise01/Rocket.h | 39 | ||||
| -rw-r--r-- | Week08/Exercise01/RocketStage.cpp | 55 | ||||
| -rw-r--r-- | Week08/Exercise01/RocketStage.h | 33 | ||||
| -rw-r--r-- | Week08/Exercise01/SpaceAgency.cpp | 170 | ||||
| -rw-r--r-- | Week08/Exercise01/SpaceAgency.h | 46 | ||||
| -rw-r--r-- | Week08/Exercise01/main.cpp | 135 | ||||
| -rw-r--r-- | Week08/README.md | 3 |
diff --git a/Week08/Exercise01/Hangar.cpp b/Week08/Exercise01/Hangar.cpp new file mode 100644 index 0000000..6a67ab4 --- /dev/null +++ b/Week08/Exercise01/Hangar.cpp @@ -0,0 +1,165 @@ +#include "Hangar.h" +#include "RocketStage.h" + +void Hangar::free() { + delete[] rockets; + delete[] stages; +} + +void Hangar::copyFrom(const Hangar& other) { + rocketsCount = other.rocketsCount; + rocketsAlloc = other.rocketsAlloc; + + rockets = new Rocket[rocketsAlloc]; + for (unsigned i = 0; i < rocketsCount; ++i) + rockets[i] = other.rockets[i]; +} + +void Hangar::resizeRockets() { + rocketsAlloc = rocketsAlloc == 0 ? 8 : rocketsAlloc * 2; + Rocket* rocketsResized = new Rocket[rocketsAlloc]; + + for (unsigned i = 0; i < rocketsCount; ++i) + rocketsResized[i] = rockets[i]; + + delete[] rockets; + rockets = rocketsResized; +} + +void Hangar::resizeStages() { + stagesAlloc = stagesAlloc == 0 ? 8 : stagesAlloc * 2; + RocketStage* stagesResized = new RocketStage[stagesAlloc]; + + for (unsigned i = 0; i < stagesCount; ++i) +stagesResized[i] = stages[i]; + + delete[] stages; + stages = stagesResized; +} + +Hangar::Hangar() { + rockets = nullptr; + stages = nullptr; + rocketsCount = rocketsAlloc = stagesCount = stagesAlloc = 0; +} + +Hangar::~Hangar() { + free(); +} + +Hangar::Hangar(const Hangar& other) { + copyFrom(other); +} + +Hangar& Hangar::operator=(const Hangar& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Hangar::Hangar(Hangar&& other) { + rockets = other.rockets; + rocketsCount = other.rocketsCount; + rocketsAlloc = other.rocketsAlloc; + + stages = other.stages; + stagesCount = other.stagesCount; + stagesAlloc = other.stagesAlloc; + + other.rockets = nullptr; + other.rocketsCount = other.rocketsAlloc = 0; + other.stages = nullptr; + other.stagesCount = other.stagesAlloc = 0; +} + +Hangar& Hangar::operator=(Hangar&& other) { + if (this != &other) { + free(); + + rockets = other.rockets; + rocketsCount = other.rocketsCount; + rocketsAlloc = other.rocketsAlloc; + + stages = other.stages; + stagesCount = other.stagesCount; + stagesAlloc = other.stagesAlloc; + + other.rockets = nullptr; + other.rocketsCount = other.rocketsAlloc = 0; + other.stages = nullptr; + other.stagesCount = other.stagesAlloc = 0; + } + return *this; +} + +unsigned Hangar::AddStage(const RocketStage& stage) { + if (stagesCount >= stagesAlloc) + resizeStages(); + stages[stagesCount] = stage; + return stagesCount++; +} + +RocketStage Hangar::RemoveStage(unsigned stageIndex) { + if (stageIndex >= stagesCount) + return RocketStage(); + + RocketStage rs = stages[stageIndex]; + + stagesCount--; + for (unsigned i = stageIndex; i < stagesCount; ++i) + stages[i] = stages[i + 1]; + + return rs; +} + +unsigned Hangar::AddRocket(const Rocket& rocket) { + if (rocketsCount >= rocketsAlloc) + resizeRockets(); + rockets[rocketsCount] = rocket; + return rocketsCount++; +} + +const Rocket* Hangar::GetRocket(unsigned rocketIndex) const { + if (rocketIndex >= rocketsCount) + return nullptr; + return &rockets[rocketIndex]; +} + +void Hangar::MoveStageToRocket(unsigned rocketIndex, unsigned stageIndex) { + if (rocketIndex >= rocketsCount || stageIndex >= stagesCount) + return; + + rockets[rocketIndex].AddStage(RemoveStage(stageIndex)); +} + +void Hangar::Export(std::ofstream& outFile) const { + outFile.write((const char*)&rocketsCount, sizeof(rocketsCount)); + outFile.write((const char*)&rocketsAlloc, sizeof(rocketsAlloc)); + for (unsigned i = 0; i < rocketsCount; ++i) + rockets[i].Export(outFile); + + outFile.write((const char*)&stagesCount, sizeof(stagesCount)); + outFile.write((const char*)&stagesAlloc, sizeof(stagesAlloc)); + for (unsigned i = 0; i < stagesCount; ++i) + outFile.write((const char*)&stages[i], sizeof(stages[i])); +} + +void Hangar::Import(std::ifstream& inFile) { + free(); + + inFile.read((char*)&rocketsCount, sizeof(rocketsCount)); + inFile.read((char*)&rocketsAlloc, sizeof(rocketsAlloc)); + + rockets = new Rocket[rocketsAlloc]; + for (unsigned i = 0; i < rocketsCount; ++i) + inFile.read((char*)&rockets[i], sizeof(rockets[i])); + + inFile.read((char*)&stagesCount, sizeof(stagesCount)); + inFile.read((char*)&stagesAlloc, sizeof(stagesAlloc)); + + stages = new RocketStage[stagesAlloc]; + for (unsigned i = 0; i < stagesCount; ++i) + inFile.read((char*)&stages[i], sizeof(stages[i])); +} diff --git a/Week08/Exercise01/Hangar.h b/Week08/Exercise01/Hangar.h new file mode 100644 index 0000000..29bd5da --- /dev/null +++ b/Week08/Exercise01/Hangar.h @@ -0,0 +1,41 @@ +#ifndef _HANGAR +#define _HANGAR + +#include "RocketStage.h" +#include "Rocket.h" + +class Hangar { + Rocket* rockets; + unsigned rocketsCount; + unsigned rocketsAlloc; + + RocketStage* stages; + unsigned stagesCount; + unsigned stagesAlloc; + + void free(); + void copyFrom(const Hangar& other); + + void resizeRockets(); + void resizeStages(); + +public: + Hangar(); + ~Hangar(); + Hangar(const Hangar& other); + Hangar& operator=(const Hangar& other); + Hangar(Hangar&& other); + Hangar& operator=(Hangar&& other); + + unsigned AddStage(const RocketStage& stage); + RocketStage RemoveStage(unsigned stageIndex); + + unsigned AddRocket(const Rocket& rocket); + const Rocket* GetRocket(unsigned rocketIndex) const; + void MoveStageToRocket(unsigned rocketIndex, unsigned stageIndex); + + void Export(std::ofstream& outFile) const; + void Import(std::ifstream& inFile); +}; + +#endif /* _HANGAR */ diff --git a/Week08/Exercise01/LaunchSite.cpp b/Week08/Exercise01/LaunchSite.cpp new file mode 100644 index 0000000..e326003 --- /dev/null +++ b/Week08/Exercise01/LaunchSite.cpp @@ -0,0 +1,26 @@ +#include "LaunchSite.h" +#include <cstring> + +const char* LaunchSite::GetName() const { + return name; +} + +void LaunchSite::SetName(const char* name) { + strncpy(this->name, name, 250); + this->name[250] = '\0'; +} + +double LaunchSite::GetMaxWeight() const { + return maxWeight; +} +void LaunchSite::SetMaxWeight(double weight) { + this->maxWeight = weight; +} + +unsigned LaunchSite::GetMaxHeight() const { + return maxHeight; +} + +void LaunchSite::SetMaxHeight(unsigned height) { + this->maxHeight = height; +} diff --git a/Week08/Exercise01/LaunchSite.h b/Week08/Exercise01/LaunchSite.h new file mode 100644 index 0000000..13c3cb3 --- /dev/null +++ b/Week08/Exercise01/LaunchSite.h @@ -0,0 +1,20 @@ +#ifndef _LAUNCH_SITE +#define _LAUNCH_SITE + +class LaunchSite { + char name[251]; + double maxWeight; + unsigned maxHeight; + +public: + const char* GetName() const; + void SetName(const char* name); + + double GetMaxWeight() const; + void SetMaxWeight(double weight); + + unsigned GetMaxHeight() const; + void SetMaxHeight(unsigned height); +}; + +#endif /* _LAUNCH_SITE */ diff --git a/Week08/Exercise01/Rocket.cpp b/Week08/Exercise01/Rocket.cpp new file mode 100644 index 0000000..5f92ad8 --- /dev/null +++ b/Week08/Exercise01/Rocket.cpp @@ -0,0 +1,154 @@ +#include "Rocket.h" +#include <cstring> + +void Rocket::free() { + delete[] stages; +} + +void Rocket::copyFrom(const Rocket& other) { + strncpy(name, other.name, sizeof(name) / sizeof(*name)); + + stagesAlloc = other.stagesAlloc; + stagesCount = other.stagesCount; + + stages = new RocketStage[stagesAlloc]; + for (unsigned i = 0; i < stagesCount; ++i) + stages[i] = other.stages[i]; +} + +void Rocket::resize() { + stagesAlloc = stagesAlloc == 0 ? 8 : stagesAlloc * 2; + RocketStage* stagesResized = new RocketStage[stagesAlloc]; + + for (unsigned i = 0; i < stagesCount; ++i) + stagesResized[i] = stages[i]; + + delete[] stages; + stages = stagesResized; +} + +Rocket::Rocket() { + name[0] = '\0'; + stages = nullptr; + stagesCount = stagesAlloc = 0; +} + +Rocket::~Rocket() { + free(); +} + +Rocket::Rocket(const Rocket& other) { + copyFrom(other); +} + +Rocket& Rocket::operator=(const Rocket& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Rocket::Rocket(Rocket&& other) { + strncpy(name, other.name, sizeof(other.name) / sizeof(*other.name)); + stages = other.stages; + stagesCount = other.stagesCount; + stagesAlloc = other.stagesAlloc; + + other.name[0] = '\0'; + other.stages = nullptr; + other.stagesCount = other.stagesAlloc = 0; +} + +Rocket& Rocket::operator=(Rocket&& other) { + if (this != &other) { + free(); + + strncpy(name, other.name, sizeof(other.name) / sizeof(*other.name)); + stages = other.stages; + stagesCount = other.stagesCount; + stagesAlloc = other.stagesAlloc; + + other.name[0] = '\0'; + other.stages = nullptr; + other.stagesCount = other.stagesAlloc = 0; + } + return *this; +} + +void Rocket::SetName(const char* name) { + strncpy(this->name, name, sizeof(this->name) / sizeof(*this->name)); +} + +void Rocket::AddStage(const RocketStage& stage) { + if (stagesCount >= stagesAlloc) + resize(); + stages[stagesCount++] = stage; +} + +double Rocket::TotalWeight() const { + double weight = 0.0; + for (unsigned i = 0; i < stagesCount; ++i) + weight += stages[i].GetWeight(); + return weight; +} + +unsigned Rocket::TotalHeight() const { + unsigned height = 0; + for (unsigned i = 0; i < stagesCount; ++i) + height += stages[i].GetHeight(); + return height; +} + +double Rocket::FinalStageSpeed() const { + double speed = 0.0; + unsigned weight = TotalWeight(); + + for (unsigned i = 0; i < stagesCount; ++i) { + weight -= stages[i].GetWeight(); + speed += stages[i].Speed(weight); + } + + return speed; +} + +double Rocket::TotalDistanceTravel() const { + unsigned weight = TotalWeight(); + double x = 0.0; + double v0 = 0.0; + + for (unsigned i = 0; i < stagesCount; ++i) { + weight -= stages[i].GetWeight(); + x += v0 * stages[i].GetBurnTime() + stages[i].DistanceTravel(weight); + v0 = stages[i].Speed(weight); + } + + return x; +} + +unsigned Rocket::TotalBurnTime() const { + unsigned burnTime = 0; + for (unsigned i = 0; i < stagesCount; ++i) + burnTime += stages[i].GetBurnTime(); + return burnTime; +} + +void Rocket::Export(std::ofstream& outFile) const { + outFile.write(name, sizeof(name) / sizeof(*name)); + outFile.write((const char*)&stagesCount, sizeof(stagesCount)); + outFile.write((const char*)&stagesAlloc, sizeof(stagesAlloc)); + for (unsigned i = 0; i < stagesCount; ++i) + outFile.write((const char*)&stages[i], sizeof(stages[i])); +} + +void Rocket::Import(std::ifstream& inFile) { + free(); + + inFile.read(name, sizeof(name) / sizeof(*name)); + inFile.read((char*)&stagesCount, sizeof(stagesCount)); + inFile.read((char*)&stagesAlloc, sizeof(stagesAlloc)); + + stages = new RocketStage[stagesAlloc]; + for (unsigned i = 0; i < stagesCount; ++i) + inFile.read((char*)&stages[i], sizeof(stages[i])); +} diff --git a/Week08/Exercise01/Rocket.h b/Week08/Exercise01/Rocket.h new file mode 100644 index 0000000..90ab2c7 --- /dev/null +++ b/Week08/Exercise01/Rocket.h @@ -0,0 +1,39 @@ +#ifndef _ROCKET +#define _ROCKET + +#include "RocketStage.h" +#include <fstream> + +class Rocket { + char name[512]; + RocketStage* stages; + unsigned stagesCount; + unsigned stagesAlloc; + + void free(); + void copyFrom(const Rocket& other); + + void resize(); + +public: + Rocket(); + ~Rocket(); + Rocket(const Rocket& other); + Rocket& operator=(const Rocket& other); + Rocket(Rocket&& other); + Rocket& operator=(Rocket&& other); + + void SetName(const char* name); + void AddStage(const RocketStage& stage); + + double TotalWeight() const; + unsigned TotalHeight() const; + double FinalStageSpeed() const; + double TotalDistanceTravel() const; + unsigned TotalBurnTime() const; + + void Export(std::ofstream& outFile) const; + void Import(std::ifstream& inFile); +}; + +#endif /* _ROCKET */ diff --git a/Week08/Exercise01/RocketStage.cpp b/Week08/Exercise01/RocketStage.cpp new file mode 100644 index 0000000..bdc6a79 --- /dev/null +++ b/Week08/Exercise01/RocketStage.cpp @@ -0,0 +1,55 @@ +#include "RocketStage.h" +#include <cstring> + +const char* RocketStage::GetName() const { + return name; +} + +void RocketStage::SetName(const char* name) { + strncpy(this->name, name, 100); + this->name[100] = '\0'; +} + +unsigned RocketStage::GetHeight() const { + return height; +} + +void RocketStage::SetHeight(unsigned height) { + this->height = height; +} + +double RocketStage::GetWeight() const { + return weight; +} + +void RocketStage::SetWeight(double weight) { + this->weight = weight; +} + +double RocketStage::GetLiftForce() const { + return liftForce; +} + +void RocketStage::SetLiftForce(double liftForce) { + this->liftForce = liftForce; +} + +unsigned RocketStage::GetBurnTime() const { + return burnTime; +} + +void RocketStage::SetBurnTime(unsigned burnTime) { + this->burnTime = burnTime; +} + +double RocketStage::acceleration(double payloadWeight) const { + return liftForce / (weight + payloadWeight); +} + +double RocketStage::Speed(double payloadWeight) const { + return burnTime * acceleration(payloadWeight); +} + +double RocketStage::DistanceTravel(double payloadWeight) const { + return (acceleration(payloadWeight) * burnTime * burnTime) / 2.0; +} diff --git a/Week08/Exercise01/RocketStage.h b/Week08/Exercise01/RocketStage.h new file mode 100644 index 0000000..8109e70 --- /dev/null +++ b/Week08/Exercise01/RocketStage.h @@ -0,0 +1,33 @@ +#ifndef _ROCKET_STAGE +#define _ROCKET_STAGE + +class RocketStage { + char name[101]; + unsigned height; // Метри + double weight; // Килограми + double liftForce; // Нютони + unsigned burnTime; // Секунди + + double acceleration(double payloadWeight) const; + +public: + const char* GetName() const; + void SetName(const char* name); + + unsigned GetHeight() const; + void SetHeight(unsigned height); + + double GetWeight() const; + void SetWeight(double weight); + + double GetLiftForce() const; + void SetLiftForce(double liftForce); + + unsigned GetBurnTime() const; + void SetBurnTime(unsigned burnTime); + + double Speed(double payloadWeight) const; + double DistanceTravel(double payloadWeight) const; +}; + +#endif /* _ROCKET_STAGE */ diff --git a/Week08/Exercise01/SpaceAgency.cpp b/Week08/Exercise01/SpaceAgency.cpp new file mode 100644 index 0000000..2c4e96e --- /dev/null +++ b/Week08/Exercise01/SpaceAgency.cpp @@ -0,0 +1,170 @@ +#include "SpaceAgency.h" + +void SpaceAgency::free() { + delete[] hangars; + delete[] sites; +} + +void SpaceAgency::copyFrom(const SpaceAgency& other) { + hangarsCount = other.hangarsCount; + hangarsAlloc = other.hangarsAlloc; + + hangars = new Hangar[hangarsAlloc]; + for (unsigned i = 0; i < hangarsCount; ++i) + hangars[i] = other.hangars[i]; + + sitesCount = other.sitesCount; + sitesAlloc = other.sitesAlloc; + + sites = new LaunchSite[sitesAlloc]; + for (unsigned i = 0; i < sitesCount; ++i) + sites[i] = other.sites[i]; +} + +void SpaceAgency::resizeHangars() { + hangarsAlloc = hangarsAlloc == 0 ? 8 : hangarsAlloc * 2; + Hangar* hangarsResized = new Hangar[hangarsAlloc]; + + for (unsigned i = 0; i < hangarsCount; ++i) + hangarsResized[i] = hangars[i]; + + delete[] hangars; + hangars = hangarsResized; +} + +void SpaceAgency::resizeSites() { + sitesAlloc = sitesAlloc == 0 ? 8 : sitesAlloc * 2; + LaunchSite* sitesResized = new LaunchSite[sitesAlloc]; + + for (unsigned i = 0; i < sitesCount; ++i) + sitesResized[i] = sites[i]; + + delete[] sites; + sites = sitesResized; +} + +SpaceAgency::SpaceAgency() { + hangars = nullptr; + sites = nullptr; + hangarsCount = hangarsAlloc = sitesCount = sitesAlloc = 0; +} + +SpaceAgency::~SpaceAgency() { + free(); +} + +SpaceAgency::SpaceAgency(const SpaceAgency& other) { + copyFrom(other); +} + +SpaceAgency& SpaceAgency::operator=(const SpaceAgency& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +SpaceAgency::SpaceAgency(SpaceAgency&& other) { + hangars = other.hangars; + hangarsAlloc = other.hangarsAlloc; + hangarsCount = other.hangarsCount; + + sites = other.sites; + sitesAlloc = other.sitesAlloc; + sitesCount = other.sitesCount; + + other.hangars = nullptr; + other.hangarsAlloc = other.hangarsCount = 0; + other.sites = nullptr; + other.sitesAlloc = other.sitesCount = 0; +} + +SpaceAgency& SpaceAgency::operator=(SpaceAgency&& other) { + if (this != &other) { + free(); + + hangars = other.hangars; + hangarsAlloc = other.hangarsAlloc; + hangarsCount = other.hangarsCount; + + sites = other.sites; + sitesAlloc = other.sitesAlloc; + sitesCount = other.sitesCount; + + other.hangars = nullptr; + other.hangarsAlloc = other.hangarsCount = 0; + other.sites = nullptr; + other.sitesAlloc = other.sitesCount = 0; + } + return *this; +} + +void SpaceAgency::AddHangar(const Hangar& hangar) { + if (hangarsCount >= hangarsAlloc) + resizeHangars(); + hangars[hangarsCount++] = hangar; +} + +Hangar* SpaceAgency::GetHangar(unsigned hangarIndex) { + if (hangarIndex >= hangarsCount) + return nullptr; + return &hangars[hangarIndex]; +} + +const Hangar* SpaceAgency::GetHangar(unsigned hangarIndex) const { + if (hangarIndex >= hangarsCount) + return nullptr; + return &hangars[hangarIndex]; +} + +void SpaceAgency::AddLaunchSite(const LaunchSite& site) { + if (sitesCount >= sitesAlloc) + resizeSites(); + sites[sitesCount++] = site; +} + +LaunchSite* SpaceAgency::GetLaunchSite(unsigned launchSiteIndex) { + if (launchSiteIndex >= sitesCount) + return nullptr; + return &sites[launchSiteIndex]; +} +const LaunchSite* SpaceAgency::GetLaunchSite(unsigned launchSiteIndex) const { + if (launchSiteIndex >= sitesCount) + return nullptr; + return &sites[launchSiteIndex]; +} + +void SpaceAgency::AddRocketStage(const RocketStage& stage, unsigned hangarIndex) { + if (hangarIndex >= hangarsCount) + return; + hangars[hangarIndex].AddStage(stage); +} + +unsigned SpaceAgency::AddRocket(const Rocket& rocket, unsigned hangarIndex) { + if (hangarIndex >= hangarsCount) + return -1; // Заради 2s complement това е само единици битове и резултата е най-голямото unsigned число + return hangars[hangarIndex].AddRocket(rocket); +} + +void SpaceAgency::MoveRocketStageToRocket(unsigned stageHangar, unsigned stageIndex, unsigned rocketHangar, unsigned rocketIndex) { + if (stageHangar >= hangarsCount || rocketHangar >= hangarsCount) + return; + + if (stageHangar != rocketHangar) { + stageIndex = hangars[rocketIndex].AddStage(hangars[stageIndex].RemoveStage(stageIndex)); + } + + hangars[rocketIndex].MoveStageToRocket(rocketIndex, stageIndex); +} + +bool SpaceAgency::CanLaunch(unsigned siteIndex, unsigned hangarIndex, unsigned rocketIndex) const { + if (siteIndex >= sitesCount || hangarIndex >= hangarsCount) + return false; + + const Rocket* rock = hangars[hangarIndex].GetRocket(rocketIndex); + if (rock == nullptr) + return false; + + return rock->TotalHeight() <= sites[siteIndex].GetMaxHeight() && rock->TotalWeight() <= sites[siteIndex].GetMaxWeight(); +} diff --git a/Week08/Exercise01/SpaceAgency.h b/Week08/Exercise01/SpaceAgency.h new file mode 100644 index 0000000..38f3e5a --- /dev/null +++ b/Week08/Exercise01/SpaceAgency.h @@ -0,0 +1,46 @@ +#ifndef _SPACE_AGENCY +#define _SPACE_AGENCY + +#include "Hangar.h" +#include "LaunchSite.h" + +class SpaceAgency { + Hangar* hangars; + unsigned hangarsCount; + unsigned hangarsAlloc; + + LaunchSite* sites; + unsigned sitesCount; + unsigned sitesAlloc; + + void free(); + void copyFrom(const SpaceAgency& other); + + void resizeHangars(); + void resizeSites(); + +public: + SpaceAgency(); + ~SpaceAgency(); + SpaceAgency(const SpaceAgency& other); + SpaceAgency& operator=(const SpaceAgency& other); + SpaceAgency(SpaceAgency&& other); + SpaceAgency& operator=(SpaceAgency&& other); + + void AddHangar(const Hangar& hangar); + Hangar* GetHangar(unsigned hangarIndex); + const Hangar* GetHangar(unsigned hangarIndex) const; + + void AddLaunchSite(const LaunchSite& site); + LaunchSite* GetLaunchSite(unsigned launchSiteIndex); + const LaunchSite* GetLaunchSite(unsigned launchSiteIndex) const; + + void AddRocketStage(const RocketStage& stage, unsigned hangarIndex); + + unsigned AddRocket(const Rocket& rocket, unsigned hangarIndex); + void MoveRocketStageToRocket(unsigned stageHangar, unsigned stageIndex, unsigned rocketHangar, unsigned rocketIndex); + + bool CanLaunch(unsigned siteIndex, unsigned hangarIndex, unsigned rocketIndex) const; +}; + +#endif /* _SPACE_AGENCY */ diff --git a/Week08/Exercise01/main.cpp b/Week08/Exercise01/main.cpp new file mode 100644 index 0000000..8ba91e8 --- /dev/null +++ b/Week08/Exercise01/main.cpp @@ -0,0 +1,135 @@ +#include <cstring> +#include <iostream> + +#include "SpaceAgency.h" + +int main() { + char command[1024]; + SpaceAgency sa; + + while (true) { + std::cin.getline(command, sizeof(command) / sizeof(*command)); + if (strcmp(command, "exit") == 0) { + break; + } + else if (strcmp(command, "new_hangar") == 0) { + sa.AddHangar(Hangar()); + } + else if (strcmp(command, "new_launch_site") == 0) { + char nameBuffer[251]; + std::cout << "Name: "; + std::cin.getline(nameBuffer, sizeof(nameBuffer) / sizeof(*nameBuffer)); + + double maxWeight; + std::cout << "Max weight: "; + std::cin >> maxWeight; + + unsigned maxHeight; + std::cout << "Max height: "; + std::cin >> maxHeight; + + std::cin.ignore(); + + LaunchSite ls; + ls.SetName(nameBuffer); + ls.SetMaxWeight(maxWeight); + ls.SetMaxHeight(maxHeight); + + sa.AddLaunchSite(ls); + } + else if (strcmp(command, "new_rocket_stage") == 0) { + char name[101]; + std::cout << "Name: "; + std::cin.getline(name, sizeof(name) / sizeof(*name)); + + unsigned height; + std::cout << "Height: "; + std::cin >> height; + + double weight; + std::cout << "Weight: "; + std::cin >> weight; + + double liftForce; + std::cout << "Lift force: "; + std::cin >> liftForce; + + unsigned burnTime; + std::cout << "Burn time: "; + std::cin >> burnTime; + + RocketStage rs; + rs.SetName(name); + rs.SetHeight(height); + rs.SetWeight(weight); + rs.SetLiftForce(liftForce); + rs.SetBurnTime(burnTime); + + unsigned hangarIndex; + std::cout << "Hangar index: "; + std::cin >> hangarIndex; + + std::cin.ignore(); + + sa.AddRocketStage(rs, hangarIndex); + } + else if (strcmp(command, "new_rocket") == 0) { + unsigned rocketHangarIndex; + std::cout << "Rocket hangar index: "; + std::cin >> rocketHangarIndex; + + std::cin.ignore(); + + char name[512]; + std::cout << "Name: "; + std::cin.getline(name, sizeof(name) / sizeof(*name)); + + Rocket r; + r.SetName(name); + unsigned rocketIndex = sa.AddRocket(r, rocketHangarIndex); + + unsigned stages; + std::cout << "Stages: "; + std::cin >> stages; + + for (unsigned i = 0; i < stages; ++i) { + unsigned stageHangar, stageIndex; + + std::cout << "Stage hangar index: "; + std::cin >> stageHangar; + std::cout << "Stage index: "; + std::cin >> stageIndex; + + sa.MoveRocketStageToRocket(stageHangar, stageIndex, rocketHangarIndex, rocketIndex); + } + std::cin.ignore(); + } + else if (strcmp(command, "can_launch") == 0) { + unsigned siteIndex; + std::cout << "Site index: "; + std::cin >> siteIndex; + + unsigned hangarIndex; + std::cout << "Rocket hangar index: "; + std::cin >> hangarIndex; + + unsigned rocketIndex; + std::cout << "Rocket index: "; + std::cin >> rocketIndex; + + std::cin.ignore(); + + if (sa.CanLaunch(siteIndex, hangarIndex, rocketIndex)) { + std::cout << "Can be launched!" << std::endl; + } + else { + std::cout << "Cannot be launched!" << std::endl; + } + } + else { + std::cout << "Invalid command!" << std::endl; + } + } + + return 0; +} diff --git a/Week08/README.md b/Week08/README.md new file mode 100644 index 0000000..47152af --- /dev/null +++ b/Week08/README.md @@ -0,0 +1,3 @@ +# Задачи + +<https://syndamia.com/teaching/oop-2025/week08/> |
