fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. int const width = 80;
  5.  
  6. class screen {
  7. int cursor; /* for escape-sequence, now not used. */
  8. double max;
  9. public:
  10. screen(double max) : cursor(0), max(max) {
  11. std::cout << std::endl;
  12. }
  13. ~screen() { std::cout << std::endl; }
  14. void out(double x) {
  15. int cx = (int)(x / max * (width / 2.0) + width / 2.0);
  16. for (int i = 0; i < cx; i++)
  17. std::cout << ' ';
  18. std::cout << 'x' << std::endl;
  19. }
  20. };
  21.  
  22. double const k = 0.05;
  23. double const u = 0.18;
  24. double const epsilon = 0.1;
  25.  
  26. class mass {
  27. double x, v;
  28. public:
  29. mass(double x, double v) : x(x), v(v) {}
  30. void delta() {
  31. double dv;
  32. dv = -k * x + ((v < 0.0) ? +1.0 : -1.0 ) * u;
  33. if (fabs(dv) < epsilon && fabs(v) < epsilon) v = 0.0; else v += dv;
  34. x += v;
  35. }
  36. double position() { return x; }
  37. };
  38.  
  39. int const Tmax = 200;
  40. double const A = 100.0;
  41. double const v0 = 0.0;
  42. int main() {
  43. int t;
  44. screen screen(A);
  45. mass m(A, v0);
  46. for (t = 0; t < Tmax; t++) {
  47. m.delta();
  48. screen.out(m.position());
  49. }
  50. return 0;
  51. }
  52. /* end */
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
                                                                             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