// mondlandung.cc - compilieren mit g++ -o mondlandung mondlandung.cc
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;

#define A 1.62      /*   m/qsec Fallbeschleunigung Mond   */
#define G 9.81      /*   m/qsec Fallbeschleunigung Erde   */

class state {
  double v, h, f, a;
         // Geschwindigkeit, Hoehe, Treibstoff-Vorrat, Beschleunigung
public:

  // Einzige Konstruktor-Funktion:
  state (double vv, double hh, double ff) {
    v = vv; h = hh; f = (ff >= 0) ? ff : -ff; a = 0.0;
  }

  // Einige Member-Funktionen:
  double height() { return h; }
  double fuel()   { return f; }

  state decel(double ff) {
    ff = (ff >= 0) ? ff : -ff ; // negativer Treibstoff-Verbrauch??
    if ( ff >= f ) ff = f;      // Vorrat verbraucht!
    f = f - ff;  a = ff - A;  h = h + v + a/2;  v = v + a;
    return *this;
  }

  void print() {
    if ( h > 0.0 )
      printf("%8.2f %8.2f %8.2f %8.2fg   ::  ", v, h, f, a/G);
    else {
      double v0 = -sqrt(v*v - 2*a*h); // Geschwindigkeit beim Aufprall
      if ( v0 <= -2.0 )
        printf("Crash:\n");
      else
        printf("Landung:\n");
      printf("%8.2f %8.2f %8.2f %8.2fg   ::  \n", v0, 0.0, f, a/G);
      printf("Aufprall entspricht einem Sturz aus %2.2f m \
Hoehe auf die Erde.\n", v0*v0/(2*G) );
    }
  }
};

int main() {
  state s = state(-106.0, 533.0, 140.0);
  double ff;
  printf("    v        h        f          a     :: Treibstoff ?\n");
  while ( s.height() > 0.0 ) {
    ff = 0.0;
    if ( s.fuel() > 0.0 ) {
      s.print();
      cin >> ff;
    }
    s.decel(ff);
  }
  s.print();
}
