HLPL2/Hw1/task3.cpp
2020-09-19 14:26:49 +00:00

111 lines
2.2 KiB
C++

#include <cmath>
#include <iostream>
#include <string>
using namespace std;
class Hop {
private:
double maxheight, startTime;
bool isHalfed;
const double loss = 0.7;
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)
{
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)
{
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()
{
}
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;
}