x007.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xmath.h"
4 #include "xphysics.h"
5 #include <algorithm>
6 #include <cmath>
7 #include <iomanip>
8 #include <iostream>
9 #include <iterator>
10 #include <vector>
11 
12 namespace nmx::apps::x007 {
13 
17 inline void ex1() {
18  std::cout << std::scientific << std::setprecision(3);
19 
20  //Name der Spalten (erste Reihe)
21  std::cout << "phi [deg]" //
22  << std::setw(15) << "phi [rad]" //
23  << std::setw(15) << "cos(phi)" //
24  << std::setw(15) << "sin(phi)" //
25  << std::setw(15) << "tan(phi)" << std::endl;
26 
27  for (double phi = 0; phi <= 90; phi += 30) {
28  double phiRad = Math::to_radians(phi);
29  std::cout << phi //
30  << std::setw(15) << phiRad //
31  << std::setw(15) << cos(phiRad) //
32  << std::setw(15) << sin(phiRad);
33  if (Math::cmp(phiRad, 0.5 * Math::PI, 1e-9) == 0) {
34  std::cout << std::setw(15) << "nan" << std::endl;
35  } else {
36  std::cout << std::setw(15) << tan(phiRad) << std::endl;
37  }
38  }
39 }
40 
44 inline void ex2() {
45  using namespace gravitation;
46  const double m = 720;
47  std::cout << std::scientific << std::setprecision(3);
48  //Namen der Spalten (erste Reihe)
49  std::cout << "alpha" << std::setw(12) << "r" << std::setw(12) //
50  << "force" << std::setw(12) << "g1" << std::setw(12) //
51  << "g1/g0" << std::endl;
52  //berechne Daten
53  for (double alpha : { 1.55, 1.75, 1.81, 1.92 }) {
54  const double c = 1 + alpha;
55  const double r = alpha * Earth::radius;
56  const double force = Astronomy::G * Earth::mass * m / pow(r, 2);
57  const double g1 = Earth::g / pow(c, 2);
58  std::cout << alpha << std::setw(12) << r << std::setw(12) //
59  << force << std::setw(12) << g1 << std::setw(12) //
60  << g1 / Earth::g << std::endl;
61  }
62 }
63 
67 inline void ex3() {
68  //Synonym für den Typ des Wertepaares
69  using Item = std::pair<double, double>;
70  //Container zur Speicherung der Werte
71  std::vector<Item> values;
72  //der Container wird gefüllt
73  for (double a = 2; a < 50; a += 1) {
74  values.push_back({ a, std::pow(a, 1.5) });
75  }
76  //Filter
77  auto filter = []() {
78  static size_t d = 1;
79  //wenn nicht 10 erhöhe Wert um 1
80  d = (d == 10) ? 1 : d + 1;
81  return d == 1;
82  };
83  //Schreibe jedes n-te Element heraus
84  for (const auto &item : values) {
85  if (filter()) {
86  std::cout << "(" << item.first << "," //
87  << item.second //
88  << ")" << std::endl;
89  }
90  }
91 }
92 
96 inline void ex4() {
97  //lambda zur Generierung von Zahlen
98  auto fn = []() {
99  static double val = 0;
100  return val += 2; //erste Zahl ist die 2
101  };
102  //C-Feld erzeuge Speicher für 9 Elemente
103  const size_t n = 9;
104  int values[9];
105  std::generate_n(values, n, fn);
106  //Ausgabe auf dem Bildschirm
107  for (auto x : values) {
108  std::cout << x << ",";
109  }
110  std::cout << std::endl;
111 }
112 } // namespace nmx::apps::x007
void ex3()
ex3 Umlaufzeit eines Planeten innerhalb des Sonnensystems
Definition: x007.h:67
void ex2()
ex2 Satellit auf kreisförmiger Bahn um die Erde
Definition: x007.h:44
void ex4()
ex4 Speicherung und Ausgabe von n geraden Zahlen
Definition: x007.h:96
void ex1()
ex1 Umrechnung Bogen- und Gradmaß
Definition: x007.h:17
static double to_radians(double x)
Definition: xmath.h:15
static constexpr double PI
Definition: xmath.h:11
static int cmp(double x, double y, double epsilon)
Definition: xmath.h:24