From 89f3e69df5984eb8a12979932261cf121dabd370 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Sat, 19 Sep 2020 14:26:49 +0000 Subject: [PATCH] hw1 task3 done --- Hw1/task3.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/Hw1/task3.cpp b/Hw1/task3.cpp index 60d2f53..56c651f 100644 --- a/Hw1/task3.cpp +++ b/Hw1/task3.cpp @@ -4,20 +4,23 @@ using namespace std; -// TODO: finish implementation class Hop { private: - double maxheight, startVelocity, startTime; + double maxheight, startTime; bool isHalfed; const double loss = 0.7; - const double g = 10; + const double g = 9.8; public: Hop(double initHeight, bool starter); ~Hop(); double hopTime(); + double getMaxVelocity(); + double getMaxHeight(); double getPosition(double time); double getVelocity(double time); + double getEndTime(); + void makeHop(); }; Hop::Hop(double initHeight, bool starter) @@ -25,7 +28,6 @@ Hop::Hop(double initHeight, bool starter) maxheight = initHeight; isHalfed = starter; startTime = 0; - startVelocity = 0; } double Hop::hopTime() @@ -37,7 +39,38 @@ double Hop::hopTime() double Hop::getPosition(double time) { - return time * startVelocity - g * time * time / 2; + if (isHalfed) + return maxheight - g * (time - startTime) * (time - startTime) / 2; + return (time - startTime) * getMaxVelocity() - g * (time - startTime) * (time - startTime) / 2; +} + +double Hop::getVelocity(double time) +{ + if (isHalfed) + return 0 - g * (time - startTime); + return getMaxVelocity() - g * (time - startTime); +} + +double Hop::getMaxVelocity() +{ + return sqrt(2 * g * maxheight); +} + +void Hop::makeHop() +{ + startTime += hopTime(); + maxheight *= 0.7; + isHalfed = false; +} + +double Hop::getEndTime() +{ + return startTime + hopTime(); +} + +double Hop::getMaxHeight() +{ + return maxheight; } Hop::~Hop() @@ -46,5 +79,33 @@ Hop::~Hop() int main() { + double init_height, time; + int i = 0; + cout << "Enter initial height in meters: "; + cin >> init_height; + cout << "Enter desired time in seconds: "; + cin >> time; + + if (init_height > 0.0 && time > 0.0) { + cout << "Modelling...\n"; + Hop a(init_height, true); + while (a.getEndTime() < time && i < 100) { + a.makeHop(); + i++; + } + if (a.getEndTime() < time && i == 100) { + cerr << "Too many iterations. Aborting." << endl; + return 2; + } + cout << "Hop №" << i << endl; + cout << "Height is " << a.getPosition(time) << " m\n"; + cout << "Velocity is " << a.getVelocity(time) << " m/s\n"; + + return 0; + } else { + cout << "Illegal height or time.\n"; + return 1; + } + return 0; } \ No newline at end of file