hw1 task3 done

This commit is contained in:
Inex Code 2020-09-19 14:26:49 +00:00
parent c00ec138f3
commit 89f3e69df5

View file

@ -4,20 +4,23 @@
using namespace std; using namespace std;
// TODO: finish implementation
class Hop { class Hop {
private: private:
double maxheight, startVelocity, startTime; double maxheight, startTime;
bool isHalfed; bool isHalfed;
const double loss = 0.7; const double loss = 0.7;
const double g = 10; const double g = 9.8;
public: public:
Hop(double initHeight, bool starter); Hop(double initHeight, bool starter);
~Hop(); ~Hop();
double hopTime(); double hopTime();
double getMaxVelocity();
double getMaxHeight();
double getPosition(double time); double getPosition(double time);
double getVelocity(double time); double getVelocity(double time);
double getEndTime();
void makeHop();
}; };
Hop::Hop(double initHeight, bool starter) Hop::Hop(double initHeight, bool starter)
@ -25,7 +28,6 @@ Hop::Hop(double initHeight, bool starter)
maxheight = initHeight; maxheight = initHeight;
isHalfed = starter; isHalfed = starter;
startTime = 0; startTime = 0;
startVelocity = 0;
} }
double Hop::hopTime() double Hop::hopTime()
@ -37,7 +39,38 @@ double Hop::hopTime()
double Hop::getPosition(double time) 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() Hop::~Hop()
@ -46,5 +79,33 @@ Hop::~Hop()
int main() 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; return 0;
} }