x002.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xconfig.h"
4 #include <array>
5 #include <cmath>
6 #include <fstream>
7 #include <functional>
8 #include <iomanip>
9 #include <iostream>
10 #include <string>
11 
12 namespace nmx::apps::x002 {
13 
17 inline void ex1() {
18  //Konfigurationsdaten nutzen
19  using namespace nmx::settings;
20  //Pfad zu einem Ausgabeverzeichnis benennen
21  Config::set_current_dir("x002");
22  //Dateiname zu Ausgabe der Werte
23  std::string fname = Config::get_current_file(__func__, "dat");
24 
25  try {
26  std::ofstream fout{ fname };
27  // wurde die Datei zum Schreiben geöffnet?
28  if (!fout) {
29  throw std::ios_base::failure{ fname };
30  }
31  //Formatierung
32  fout << std::scientific << std::setprecision(2);
33  //Ausgabe
34  for (double x = -2; x <= 2; x += 1) {
35  fout << std::setw(9) << x << "," << std::pow(x, 2) //
36  << "," << std::setw(9) << std::pow(x, 3) //
37  << "," << std::setw(9) << std::pow(x, 4) //
38  << "," << std::setw(9) << std::pow(x, 5) << std::endl;
39  }
40 
41  } catch (std::ios_base::failure ferror) {
42  std::cout << "could not open file:" //
43  << ferror.what() << std::endl;
44  }
45 }
46 
51 auto open_file(const std::string &fname) {
52  std::ofstream fout{ fname };
53  // wurde die Datei zum Schreiben geöffnet?
54  if (!fout) {
55  throw std::ios_base::failure{ fname };
56  }
57  return fout;
58 }
59 
63 using RealFN = std::function<double(double)>;
64 
71 inline void fn_data(std::ofstream &fout,
72  std::initializer_list<RealFN> fnlst,
73  std::array<double, 3> params) {
74  //structured bindings jedes Element des Feldes wird
75  //einer Variablen zugeordnet
76  auto [xmin, xmax, dx] = params;
77  for (double x = xmin; x <= xmax; x += dx) {
78  // für jedes x
79  fout << x;
80  for (auto &fn : fnlst) {
81  //werden die dazugehörigen Funktionswerte ausgegeben
82  fout << "," << fn(x);
83  }
84  fout << std::endl;
85  }
86 }
90 inline void ex2() {
91  using namespace nmx::settings;
92  Config::set_current_dir("x002");
93  std::string fname = Config::get_current_file(std::string(__func__) + ".dat");
94 
95  auto fn2 = [](double x) { return std::pow(x, 2); };
96  auto fn3 = [](double x) { return std::pow(x, 3); };
97  auto fn4 = [](double x) { return std::pow(x, 4); };
98  auto fn5 = [](double x) { return std::pow(x, 5); };
99 
100  try {
101  //öffne Ausgabestrom
102  auto fout = open_file(fname);
103  fout << std::scientific << std::setprecision(2);
104 
105  //Berechne und schreibe Funktionsdaten mit xmin = -2,
106  // xmax=2 und dx = 0.5
107  fn_data(fout, { fn2, fn3, fn4, fn5 }, { -2, 2, 0.5 });
108 
109  } catch (std::ios_base::failure ferror) {
110  std::cout << "could not open file:" //
111  << ferror.what() << std::endl;
112  }
113 }
114 
115 } // namespace nmx::apps::x002
std::function< double(double)> RealFN
Definition: x002.h:63
void fn_data(std::ofstream &fout, std::initializer_list< RealFN > fnlst, std::array< double, 3 > params)
fn_data Ausgabe von Funktionswerten in eine Datei
Definition: x002.h:71
auto open_file(const std::string &fname)
open_file öffne Ausgabestrom für eine Datei
Definition: x002.h:51
void ex2()
ex2 schreibe Funktionsdaten für x^2,x^3,x^4,x^5
Definition: x002.h:90
void ex1()
ex1 Ausgabe von Funktionswerten in eine Datei
Definition: x002.h:17