Website Summary Files Commits Branches @commit @diff @stats
path: root / Week08 / Exercise01 / Rocket.cpp
diff options
context:
space:
mode:
Commit7d98b664c9e459a08f2e6e6fa5b636686dfed554 (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-04-08 10:42:51 +0300
Committer
 Syndamia <kamen@syndamia.com>
2026-04-08 10:42:51 +0300
Filesb2b6eddb0c6f582999ebb32c8dea2a01b477d747 /Week08/Exercise01/Rocket.cpp
oop-2025-solutions-7d98b664c9e459a08f2e6e6fa5b636686dfed554.tar
oop-2025-solutions-7d98b664c9e459a08f2e6e6fa5b636686dfed554.tar.gz
oop-2025-solutions-7d98b664c9e459a08f2e6e6fa5b636686dfed554.zip
Parent945f7e67d6ca8047c961457786fac5e2d9a13d82 (diff)
feat(Week08): Add solutions
1 files changed, 154 insertions, 0 deletions
-rw-r--r--Week08/Exercise01/Rocket.cpp154
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]));
+}