2020-09-19 13:16:55 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class Hop {
|
|
|
|
private:
|
2020-09-19 14:26:49 +00:00
|
|
|
double maxheight, startTime;
|
2020-09-19 13:16:55 +00:00
|
|
|
bool isHalfed;
|
|
|
|
const double loss = 0.7;
|
2020-09-19 14:26:49 +00:00
|
|
|
const double g = 9.8;
|
2020-09-19 13:16:55 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Hop(double initHeight, bool starter);
|
|
|
|
~Hop();
|
|
|
|
double hopTime();
|
2020-09-19 14:26:49 +00:00
|
|
|
double getMaxVelocity();
|
|
|
|
double getMaxHeight();
|
2020-09-19 13:16:55 +00:00
|
|
|
double getPosition(double time);
|
|
|
|
double getVelocity(double time);
|
2020-09-19 14:26:49 +00:00
|
|
|
double getEndTime();
|
|
|
|
void makeHop();
|
2020-09-19 13:16:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Hop::Hop(double initHeight, bool starter)
|
|
|
|
{
|
|
|
|
maxheight = initHeight;
|
|
|
|
isHalfed = starter;
|
|
|
|
startTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Hop::hopTime()
|
|
|
|
{
|
|
|
|
if (isHalfed)
|
|
|
|
return sqrt((2 * maxheight) / g);
|
|
|
|
return 2 * sqrt((2 * maxheight) / g);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Hop::getPosition(double time)
|
|
|
|
{
|
2020-09-19 14:26:49 +00:00
|
|
|
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;
|
2020-09-19 13:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Hop::~Hop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-09-19 14:26:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-19 13:16:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|