x006.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xconfig.h"
4 #include "xphysics.h"
5 #include "xunits.h"
6 #include <cstring>
7 #include <fstream>
8 #include <iomanip>
9 #include <iostream>
10 #include <map>
11 #include <sstream>
12 #include <string>
13 #include <vector>
14 
15 namespace nmx::apps::x006 {
16 
17 //**
18 //Verzeichnisname und Synonyme
19 const std::string dirname = settings::Config::data_directory + "/x006";
20 
21 using Document = std::vector<std::string>;
22 using Vartable = std::map<std::string, double>;
23 
27 inline auto read_text() {
28  const std::string fname = dirname + "/qstn1.txt";
29  //öffne Datei
30  std::ifstream ifs{ fname };
31  if (!ifs) {
32  throw std::ios_base::failure("error open:" + fname);
33  }
34 
35  std::string line;
36  Document lines;
37  // lese Datei zeilenweise und speichere in std::vector ab
38  while (getline(ifs, line)) {
39  lines.push_back(line);
40  }
41 
42  return lines;
43 }
44 
50 inline auto convert(double x) {
51  std::stringstream sstream;
52  sstream << std::scientific << std::setprecision(2) << x;
53  return sstream.str();
54 }
55 
61 inline void calc(Vartable &values) {
62  using namespace gravitation;
63  double mass = values.at("$1");
64  double height = values.at("$2");
65  double radius = Earth::radius + height;
66  double energy = -0.5 * Astronomy::G * Earth::mass * mass / radius;
67  //setze berechnete Werte
68  values["$3"] = radius;
69  values["$4"] = energy;
70 }
71 
77 inline auto replace(Document &txt, const Vartable &values) {
78  // Iteration über alle Zeilen des Dokuments
79  for (auto &line : txt) {
80  //Iteration über alle Platzhalter
81  for (const auto &item : values) {
82  //Platzhalter
83  const auto key = item.first;
84  //Wert als Zeichenkette
85  const auto val = convert(item.second);
86  size_t pos;
87  //suche
88  while ((pos = line.find(key)) != std::string::npos) {
89  //ersetze
90  line.replace(pos, key.length(), val);
91  }
92  }
93  }
94 }
95 
100 inline void save_answer(const Document &doc) {
101  const std::string fname = dirname + "/answr1.txt";
102  std::ofstream ofs{ fname };
103 
104  if (!ofs) {
105  throw std::ios_base::failure("error open:" + fname);
106  }
107 
108  for (const auto &line : doc) {
109  ofs << line << std::endl;
110  }
111 }
112 
117 inline void ex1() {
118  auto doc = read_text();
119  Vartable vtable{ { "$1", 10 }, { "$2", 300._km } };
120  calc(vtable);
121  replace(doc, vtable);
122  save_answer(doc);
123 }
124 
128 inline void ex4() {
129  std::string txt = "eins,zwei,drei,vier,fünf,sechs,"
130  "sieben,acht,neun und zehn";
131 
132  // Zeichenkette wird einem stringstream übergeben
133  std::stringstream sstream{ txt };
134  std::string token;
135 
136  //übetrage Inhalt der Zeichenkette bis Trennzeichen vorkommt
137  while (std::getline(sstream, token, ',')) {
138  //schreibe den extrahierten Teil der Zeichenkette mit
139  //anderem Trennzeichen
140  std::cout << token << "-";
141  }
142  std::cout << std::endl;
143 }
144 
148 inline void ex3() {
149  //Textvorgabe
150  std::string txt = "eins,zwei,drei,vier,fünf,sechs,"
151  "sieben,acht,neun und zehn";
152  //Trennzeichen: Leerzeichen und Komma
153  std::string delim = " ,";
154  //Anfangsposition
155  size_t beg, pos = 0;
156  //finde die erste Stelle nach "pos" in der KEIN Trennzeichen
157  //gefunden wurde
158  while ((beg = txt.find_first_not_of(delim, pos)) != std::string::npos) {
159  //finde die erste Stelle in der EIN Trennzeichen gefunden wurde
160  pos = txt.find_first_of(delim, beg + 1);
161  //schreibe die gefunden Teil-Zeichenkette
162  std::cout << beg << "\t" //
163  << (pos != std::string::npos ? pos : 1000) //
164  << "\t" << txt.substr(beg, pos - beg) << "\n";
165  }
166 }
167 
172 inline void ex2() {
173  //Textvorlage
174  std::string txt = "die Bewegung eines Objekts in einer"
175  " Flüssigkeit beschreiben...";
176  std::cout << "Text:" << std::endl;
177  std::cout << txt << std::endl;
178  //Wort im Text
179  const std::string w1 = "Objekts";
180  //wird ersetzt durch
181  const std::string w2 = "Teilchens";
182 
183  //**------------------
184  //Methode 1
185  //--------------------
186  std::cout << "Methode 1:" << std::endl;
187  auto txt1 = txt;
188  size_t pos = txt1.find(w1);
189  // finde Position von w1 und schneide es heraus
190  if (pos != std::string::npos) {
191  txt1.erase(pos, w1.length());
192  std::cout << txt1 << std::endl;
193  } else {
194  std::cout << "not found:" << std::endl;
195  }
196  //füge an der Position eine neue Zeichenkette ein
197  if (pos != std::string::npos) {
198  txt1.insert(pos, w2);
199  std::cout << txt1 << std::endl;
200  } else {
201  std::cout << "not found:" << std::endl;
202  }
203 
204  //**----------------------
205  //Methode 2
206  //------------------------
207  std::cout << "Methode 2:" << std::endl;
208  auto txt2 = txt;
209  pos = txt2.find(w1);
210  //ausschneiden und ersetzen mit einem Befehl
211  if (pos != std::string::npos) {
212  txt2.replace(pos, w1.length(), w2);
213  std::cout << txt2 << std::endl;
214  } else {
215  std::cout << "not found:" << std::endl;
216  }
217 }
218 
219 } // namespace nmx::apps::x006
void ex4()
ex4 Zeichenketten für ein gegebenes Trennzeichen aufteilen
Definition: x006.h:128
auto replace(Document &txt, const Vartable &values)
calc_replace
Definition: x006.h:77
void ex3()
ex3 Zeichenketten aufteilen mit mehreren Trennzeichen
Definition: x006.h:148
auto read_text()
read_text Einlesen des Texts
Definition: x006.h:27
std::map< std::string, double > Vartable
Definition: x006.h:22
std::vector< std::string > Document
Definition: x006.h:21
auto convert(double x)
convert Konvertierung der Zahl in eine Zeichenkette
Definition: x006.h:50
const std::string dirname
Definition: x006.h:19
static const std::string data_directory
Definition: xconfig.h:23
void save_answer(const Document &doc)
save Dokument wird gespeichert
Definition: x006.h:100
void calc(Vartable &values)
calc Berechnung der fehlenden Größen
Definition: x006.h:61
void ex1()
ex1 Bearbeitung eines Texts und Berechnung der unbekannten physikalischen Größen
Definition: x006.h:117
void ex2()
ex2 Suchen und Ersetzen von Zeichenketten innerhalb eines Texts
Definition: x006.h:172