From 4d82d1d64f82a2318704ed07409b68fc58852420 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 23 Nov 2019 02:39:51 +0700 Subject: [PATCH] pb/z13 --- 1sem/programming basics/z13/37.cpp | 36 +++++++++++++++++++++++++++++ 1sem/programming basics/z13/38.cpp | 31 +++++++++++++++++++++++++ 1sem/programming basics/z13/39.cpp | 37 ++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 1sem/programming basics/z13/37.cpp create mode 100644 1sem/programming basics/z13/38.cpp create mode 100644 1sem/programming basics/z13/39.cpp diff --git a/1sem/programming basics/z13/37.cpp b/1sem/programming basics/z13/37.cpp new file mode 100644 index 0000000..faafcaf --- /dev/null +++ b/1sem/programming basics/z13/37.cpp @@ -0,0 +1,36 @@ +// pb_z13_37.cpp +// Горбацевич Андрей +#include +#include +#define MAX_STRING 500 + +using namespace std; + +void join(const char *sep, istream &ist, ostream &ost); + +int main() { + ifstream inf("in.txt"); + ofstream fout("out.txt", ios::trunc); + if (!inf.is_open() || !fout.is_open()) + { + cerr << "Unable to open file" << endl; + return 1; + } + + string sep; + cout << "Enter separator >>>"; + getline(cin, sep, '\n'); + join(sep.c_str(), inf, fout); + return 0; +} + +void join(const char *sep, istream &ist, ostream &ost) { + while (!ist.eof() && ist.good()) { + char buf[MAX_STRING]; + ist >> buf; + ost << buf; + if (!ist.eof()) { + ost << sep; + } + } +} diff --git a/1sem/programming basics/z13/38.cpp b/1sem/programming basics/z13/38.cpp new file mode 100644 index 0000000..bf04810 --- /dev/null +++ b/1sem/programming basics/z13/38.cpp @@ -0,0 +1,31 @@ +// pb_z13_38.cpp +// Горбацевич Андрей +#include +#include + +using namespace std; + +void rev_lines(istream &ist, ostream &ost); + +int main() { + ifstream inf("in.txt"); + ofstream fout("out.txt", ios::trunc); + if (!inf.is_open() || !fout.is_open()) + { + cerr << "Unable to open file" << endl; + return 1; + } + + rev_lines(inf, fout); + return 0; +} + +void rev_lines(istream &ist, ostream &ost) { + string out; + while (!ist.eof() && ist.good()) { + string buf; + getline(ist, buf); + out.insert(0, buf + "\n"); + } + ost << out.substr(0, out.size()-1); +} diff --git a/1sem/programming basics/z13/39.cpp b/1sem/programming basics/z13/39.cpp new file mode 100644 index 0000000..72efc9b --- /dev/null +++ b/1sem/programming basics/z13/39.cpp @@ -0,0 +1,37 @@ +// pb_z13_39.cpp +// Горбацевич Андрей +#include +#include + +using namespace std; + +void wrap_text(istream &ist, ostream &ost, int max_line_len); + +int main() { + ifstream inf("in.txt"); + ofstream fout("out.txt", ios::trunc); + if (!inf.is_open() || !fout.is_open()) + { + cerr << "Unable to open file" << endl; + return 1; + } + + int max_line_len = 0; + cout << "Maximal line length >>>"; + cin >> max_line_len; + wrap_text(inf, fout, max_line_len); + return 0; +} + +void wrap_text(istream &ist, ostream &ost, int max_line_len) { + int cl = 0; + char c; + while ((c = ist.get()) && !ist.eof() && ist.good()) { + if (cl >= max_line_len) { + cl = 0; + ost << endl; + } + ost << c; + cl++; + } +}