22 using Vartable = std::map<std::string, double>;
28 const std::string fname = dirname +
"/qstn1.txt";
30 std::ifstream ifs{ fname };
32 throw std::ios_base::failure(
"error open:" + fname);
38 while (getline(ifs, line)) {
39 lines.push_back(line);
51 std::stringstream sstream;
52 sstream << std::scientific << std::setprecision(2) << x;
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;
68 values[
"$3"] = radius;
69 values[
"$4"] = energy;
79 for (
auto &line : txt) {
81 for (
const auto &item : values) {
83 const auto key = item.first;
85 const auto val =
convert(item.second);
88 while ((pos = line.find(key)) != std::string::npos) {
90 line.replace(pos, key.length(), val);
101 const std::string fname = dirname +
"/answr1.txt";
102 std::ofstream ofs{ fname };
105 throw std::ios_base::failure(
"error open:" + fname);
108 for (
const auto &line : doc) {
109 ofs << line << std::endl;
119 Vartable vtable{ {
"$1", 10 }, {
"$2", 300._km } };
129 std::string txt =
"eins,zwei,drei,vier,fünf,sechs," 130 "sieben,acht,neun und zehn";
133 std::stringstream sstream{ txt };
137 while (std::getline(sstream, token,
',')) {
140 std::cout << token <<
"-";
142 std::cout << std::endl;
150 std::string txt =
"eins,zwei,drei,vier,fünf,sechs," 151 "sieben,acht,neun und zehn";
153 std::string delim =
" ,";
158 while ((beg = txt.find_first_not_of(delim, pos)) != std::string::npos) {
160 pos = txt.find_first_of(delim, beg + 1);
162 std::cout << beg <<
"\t" 163 << (pos != std::string::npos ? pos : 1000)
164 <<
"\t" << txt.substr(beg, pos - beg) <<
"\n";
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;
179 const std::string w1 =
"Objekts";
181 const std::string w2 =
"Teilchens";
186 std::cout <<
"Methode 1:" << std::endl;
188 size_t pos = txt1.find(w1);
190 if (pos != std::string::npos) {
191 txt1.erase(pos, w1.length());
192 std::cout << txt1 << std::endl;
194 std::cout <<
"not found:" << std::endl;
197 if (pos != std::string::npos) {
198 txt1.insert(pos, w2);
199 std::cout << txt1 << std::endl;
201 std::cout <<
"not found:" << std::endl;
207 std::cout <<
"Methode 2:" << std::endl;
211 if (pos != std::string::npos) {
212 txt2.replace(pos, w1.length(), w2);
213 std::cout << txt2 << std::endl;
215 std::cout <<
"not found:" << std::endl;
void ex4()
ex4 Zeichenketten für ein gegebenes Trennzeichen aufteilen
auto replace(Document &txt, const Vartable &values)
calc_replace
void ex3()
ex3 Zeichenketten aufteilen mit mehreren Trennzeichen
auto read_text()
read_text Einlesen des Texts
std::map< std::string, double > Vartable
std::vector< std::string > Document
auto convert(double x)
convert Konvertierung der Zahl in eine Zeichenkette
const std::string dirname
static const std::string data_directory
void save_answer(const Document &doc)
save Dokument wird gespeichert
void calc(Vartable &values)
calc Berechnung der fehlenden Größen
void ex1()
ex1 Bearbeitung eines Texts und Berechnung der unbekannten physikalischen Größen
void ex2()
ex2 Suchen und Ersetzen von Zeichenketten innerhalb eines Texts