x000.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <cmath>
5 #include <iomanip>
6 #include <iostream>
7 #include <numeric>
8 
9 namespace nmx::apps::x000 {
10 
11 inline void ex1() {
12  std::array v1 = { 1., 2., 3. };
13  std::array v2 = { -3., -9., 1. };
14 
15  auto normalize = [](auto v) {
16  auto vcopy = v;
17  double vnorm = std::inner_product(vcopy.begin(), vcopy.end(), vcopy.begin(), 0.0);
18  std::transform(vcopy.begin(), vcopy.end(), vcopy.begin(), [vnorm](double x) { //
19  return x / vnorm;
20  });
21  return vcopy;
22  };
23 
24  auto print = [](auto v) {
25  for (auto x : v) {
26  std::cout << x << "\t";
27  }
28  std::cout << std::endl;
29  };
30 
31  auto v1n = normalize(v1);
32  auto v2n = normalize(v2);
33 
34  std::cout << std::fixed << std::setprecision(2);
35  print(v1n);
36  print(v2n);
37 }
38 
43 inline void ex2() {
44  double x0, v0, a, t1; // input
45  double x1, v1, s, vbar; //output
46  char cmd = 'y'; // die Schleife soll wiederholt werden
47  std::cout << "------------input-------------" << std::endl;
48  while (cmd == 'y') {
49  //1. Eingabe : Anfangsort
50  std::cout << "x0=?\t";
51  std::cin >> x0;
52  //2. Eingabe : Anfangsgeschwindigkeit
53  std::cout << "v0=?\t";
54  std::cin >> v0;
55  //3. Eingabe : Beschleunigung
56  std::cout << "a =?\t";
57  std::cin >> a;
58  //4. Eingabe : Zeitpunkt
59  std::cout << "t1=?\t";
60  std::cin >> t1;
61  // Die Formeln für:
62  x1 = x0 + v0 * t1 + 0.5 * a * pow(t1, 2); //Ort
63  v1 = v0 + a * t1; //Geschwindigkeit
64  s = x1 - x0; //Strecke
65  vbar = s / t1; //Durchschnittsgeschwindigkeit
66  //Ausgabe der Ergebnisse
67  std::cout << std::fixed << std::setprecision(2);
68  std::cout << "------------output-------" << std::endl;
69  std::cout << "t1 = " << t1 << "\t"; // std::endl;
70  std::cout << "x1 = " << x1 << "\t"; // std::endl;
71  std::cout << "v1 = " << v1 << "\t"; // std::endl;
72  std::cout << "s = " << s << "\t"; // std::endl;
73  std::cout << "vbar = " << vbar << std::endl;
74  //Programm beenden?
75  do {
76  std::cout << "continue? y[yes] / n[no]" << std::endl;
77  std::cin >> cmd;
78  } while (cmd != 'y' && cmd != 'n');
79  }
80 }
81 
82 } // namespace nmx::apps::x000
83 
88 namespace nmx::apps::x000 {
89 
90 // lese Benutzereingabe
91 double read(const char *label) {
92  double val;
93  std::cout << label << "=?\t";
94  std::cin >> val;
95  //Fehler wenn val nicht vom Typ double ist
96  if (std::cin.fail()) {
97  throw std::invalid_argument(label);
98  }
99  return val;
100 }
101 
102 //
103 using ValuePair = std::pair<std::string, double>;
104 
105 // schreibe alle Ergebnisse
106 void write(std::initializer_list<ValuePair> lst) {
107  for (auto &[label, val] : lst) {
108  std::cout << label << std::setw(6 - static_cast<int>(label.size())) << "=" << val
109  << std::endl;
110  }
111 }
112 
113 // Eingabe->Berechnung->Ausgabe
114 void corefn() {
115  double x0, v0, a, t1; // input
116  double x1, v1, s, vbar; //output
117  //1. Eingabe : Anfangsort
118  x0 = read("x0");
119  //2. Eingabe : Anfangsgeschwindigkeit
120  v0 = read("v0");
121  //3. Eingabe : Beschleunigung
122  a = read("a");
123  //4. Eingabe : Zeitpunkt
124  t1 = read("t1");
125  if (t1 <= 0) {
126  throw std::domain_error("time must be >0");
127  }
128  // Die Formeln für:
129  x1 = x0 + v0 * t1 + 0.5 * a * pow(t1, 2); //Ort
130  v1 = v0 + a * t1; //Geschwindigkeit
131  s = x1 - x0; //Strecke
132  vbar = s / t1; //Durchschnittsgeschwindigkeit
133  //Ausgabe der Ergebnisse
134  std::cout << std::fixed << std::setprecision(2);
135  std::cout << "------------output-------" << std::endl;
136  write({ { "t1", t1 }, //
137  { "x1", x1 },
138  { "v1", v1 },
139  { "s", s },
140  { "vbar", vbar } });
141 }
142 
143 // Benutzerschnittstelle
144 inline void calcui() {
145  char cmd = 'y'; // die Schleife soll wiederholt werden
146  constexpr auto maxstreamsize = std::numeric_limits<std::streamsize>::max();
147  std::cout << "------------input-------------" << std::endl;
148  while (cmd == 'y') {
149  try {
150  corefn();
151  } catch (std::domain_error ex1) {
152  std::cin.clear();
153  //ignoriere all restlichen Eingaben
154  std::cin.ignore(maxstreamsize, '\n');
155  std::cerr << "input error:" << ex1.what() << std::endl;
156  } catch (std::invalid_argument ex2) {
157  std::cin.clear();
158  //ignoriere all restlichen Eingaben
159  std::cin.ignore(maxstreamsize, '\n');
160  std::cerr << "input error for:" << ex2.what() << std::endl;
161  }
162  //Programm beenden?
163  do {
164  std::cout << "new claculation? y[yes] / n[no]" << std::endl;
165  std::cin >> cmd;
166  } while (cmd != 'y' && cmd != 'n');
167  }
168 }
169 
170 } // namespace nmx::apps::x000
void corefn()
Definition: x000.h:114
void ex2()
ex2 ex3 Geradlinige Bewegung mit konstanter Beschleunigung, Version 1.0
Definition: x000.h:43
void ex1()
Definition: x000.h:11
std::pair< std::string, double > ValuePair
Definition: x000.h:103
void calcui()
Definition: x000.h:144
void write(std::initializer_list< ValuePair > lst)
Definition: x000.h:106
double read(const char *label)
Definition: x000.h:91