#include <iostream> #include <cmath> int const width = 80; class screen { int cursor; /* for escape-sequence, now not used. */ double max; public: screen(double max) : cursor(0), max(max) { std::cout << std::endl; } ~screen() { std::cout << std::endl; } void out(double x) { int cx = (int)(x / max * (width / 2.0) + width / 2.0); for (int i = 0; i < cx; i++) std::cout << ' '; std::cout << 'x' << std::endl; } }; double const k = 0.05; double const u = 0.18; double const epsilon = 0.1; class mass { double x, v; public: mass(double x, double v) : x(x), v(v) {} void delta() { double dv; dv = -k * x + ((v < 0.0) ? +1.0 : -1.0 ) * u; if (fabs(dv) < epsilon && fabs(v) < epsilon) v = 0.0; else v += dv; x += v; } double position() { return x; } }; int const Tmax = 200; double const A = 100.0; double const v0 = 0.0; int main() { int t; screen screen(A); mass m(A, v0); for (t = 0; t < Tmax; t++) { m.delta(); screen.out(m.position()); } return 0; } /* end */
Standard input is empty
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x