x004.h
Go to the documentation of this file.
1 #pragma once
2 #include "xunits.h"
3 #include <cmath>
4 #include <iomanip>
5 #include <iostream>
6 #include <stdexcept>
7 #include <vector>
8 
9 namespace nmx::apps::x004 {
10 
16 inline double read_input(const char *c) {
17  double x;
18  std::cout << c << "=?\t";
19  std::cin >> x;
20  // x ist double, std::cin erwartet double
21  if (std::cin.fail()) {
22  throw std::invalid_argument(c);
23  }
24  // die Seite eines Dreiecks muss immer eine positive Zahl sein
25  if (x <= 0) {
26  throw std::domain_error(c);
27  }
28  return x;
29 };
30 
35 inline void ex1() {
36  constexpr auto maxstreamsize = std::numeric_limits<std::streamsize>::max();
37  char cmd = 'y';
38 
39  // rechne bis der Benutzer per Befehl das Programm beendet
40  while (cmd == 'y') {
41  try {
42  //Benutzereingabe: zwei senkrechte Seiten
43  double x = read_input("x");
44  double y = read_input("y");
45  //Ausgabe
46  std::cout << "hypotenuse:" << std::hypot(x, y) << std::endl;
47  } catch (std::invalid_argument &invarg) {
48  //Buchstabe statt einer Zahl wurde eingegeben
49  std::cin.clear();
50  //ignoriere alle restlichen Eingaben
51  std::cin.ignore(maxstreamsize, '\n');
52  std::cout << "invalid argument for:" //
53  << invarg.what() << std::endl;
54  } catch (std::domain_error &domerr) {
55  //negative Zahl wurde eingegeben
56  std::cin.clear();
57  //ignoriere alle restlichen Eingaben
58  std::cin.ignore(maxstreamsize, '\n');
59  std::cout << "input parameter must be >0:" //
60  << domerr.what() << std::endl;
61  }
62 
63  //Programm beenden?
64  do {
65  std::cout << "new claculation? y[yes] / n[no]" << std::endl;
66  std::cin >> cmd;
67  } while (cmd != 'y' && cmd != 'n');
68  }
69 }
70 
79 inline double brake_assist(double v0, double d, double tR) {
80  double x0 = v0 * tR;
81 
82  if (x0 > d) {
83  throw std::range_error(std::to_string(0));
84  }
85 
86  double a = 0.5 * std::pow(v0, 2) / (d - x0);
87  if (a > 6) {
88  throw std::range_error(std::to_string(a));
89  }
90  return a;
91 };
92 
99 void break_assist_ui(double v0, double d, double rtime) {
100  std::cout << "d=" << d << " m,";
101  std::cout << "v0=" << v0 << " m/s,";
102  std::cout << "tR=" << rtime << " m/s" << std::endl;
103  std::cout << std::setprecision(2);
104  try {
105  const double a = brake_assist(v0, d, rtime);
106  double x0 = v0 * rtime;
107  double x = x0 + 0.5 * std::pow(v0, 2) / a;
108  std::cout << "need a=" << a << " m/s^2" << std::endl;
109  std::cout << "use a=" << std::ceil(a) << " m/s^2" << std::endl;
110  std::cout << "distance=" << x << std::endl;
111  } catch (std::range_error err) {
112  std::cout << "need : " << err.what() << " m/s^2" << std::endl;
113  std::cout << "car will crash" << std::endl;
114  }
115 }
116 
120 void ex2() {
121  const double d = 30.0;
122  const double rtime = 0.7;
123  const double v0 = 14; //m/s;
124  break_assist_ui(v0, d, rtime);
125  //zweiter Versuch mit Geschwindigkeit 2*v0
126  std::cout << "-------------------" << std::endl;
127  break_assist_ui(2 * v0, d, rtime);
128 }
129 
130 } // namespace nmx::apps::x004
double read_input(const char *c)
read_input lese eine double-Zahl von der Standardeingabe
Definition: x004.h:16
double brake_assist(double v0, double d, double tR)
brake_assist Berechnung der Beschleunigung damit das Auto noch vor dem Hindernis zum Stehen kommt ...
Definition: x004.h:79
void break_assist_ui(double v0, double d, double rtime)
break_assist_ui Benutzerschnittstelle
Definition: x004.h:99
void ex2()
ex2 Auto bremst, um einem Hindernis ausweichen
Definition: x004.h:120
void ex1()
ex1 Berechnung der Hypotenuse eines rechtwinkligen Dreiecks, Version 3.0 (Benutzerschnittstelle) ...
Definition: x004.h:35