From deba2fdcf08ebe3d1af04f11b03b4def75c705e4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 19 Nov 2019 00:19:04 +0700 Subject: [PATCH] c3 --- 1sem/adaptation courses/09/28.cpp | 7 ++-- 1sem/adaptation courses/11/35.cpp | 16 ++++++--- 1sem/programming basics/z11/31.cpp | 41 +++++++++++++++++++++++ 1sem/programming basics/z11/32.cpp | 51 ++++++++++++++++++++++++++++ 1sem/programming basics/z11/33.cpp | 53 ++++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+), 8 deletions(-) create mode 100644 1sem/programming basics/z11/31.cpp create mode 100644 1sem/programming basics/z11/32.cpp create mode 100644 1sem/programming basics/z11/33.cpp diff --git a/1sem/adaptation courses/09/28.cpp b/1sem/adaptation courses/09/28.cpp index f211b82..f77b36b 100644 --- a/1sem/adaptation courses/09/28.cpp +++ b/1sem/adaptation courses/09/28.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using namespace std; @@ -30,9 +31,9 @@ int main() { return 1; } vector v = read_vec(inf); - size_t index = -1; - float smallest = 1e38f; - for (size_t i = 0; i < v.size(); i++) { + size_t index = 0; + float smallest = v[index]; + for (size_t i = 1; i < v.size(); i++) { if (nearest_int_dist(v[i]) < smallest) { smallest = nearest_int_dist(v[i]); index = i; diff --git a/1sem/adaptation courses/11/35.cpp b/1sem/adaptation courses/11/35.cpp index 96cfc74..d696fc4 100644 --- a/1sem/adaptation courses/11/35.cpp +++ b/1sem/adaptation courses/11/35.cpp @@ -3,15 +3,20 @@ #include #include #include -#define MAX_STRING 5000 using namespace std; string get_short_name(const string &full_name) { - char s[MAX_STRING], n[MAX_STRING], f[MAX_STRING], out[MAX_STRING]; - sscanf(full_name.c_str(), "%s %s %s", s, n, f); - sprintf(out, "%s %c.%c.", s, n[0], f[0]); - return string(out); + string out[3]; + int i = 0; + for (char c : full_name) { + if (c == ' ') { + i++; + continue; + } + out[i] += c; + } + return out[0] + " " + out[1][0] + "." + out[2][0] + "."; } void read_string(istream &reader, string &where, char delimiter) { @@ -31,6 +36,7 @@ int main() { while (!inf.eof()) { string s; read_string(inf, s, '\n'); + if (s.size() < 2) continue; cout << get_short_name(s) << endl; } return 0; diff --git a/1sem/programming basics/z11/31.cpp b/1sem/programming basics/z11/31.cpp new file mode 100644 index 0000000..0866641 --- /dev/null +++ b/1sem/programming basics/z11/31.cpp @@ -0,0 +1,41 @@ +// pb_z11_31.cpp +// Горбацевич Андрей +#include + +using namespace std; + +int **read_matrix(size_t rows, size_t cols); +void print_matrix(int **matrix, size_t rows, size_t cols); +int **make2d(size_t nrows, size_t ncols, int val); + +int main() { + size_t rows, cols; + int filler; + cout << "rows, cols, filling_val >>>"; + cin >> rows >> cols >> filler; + int **matrix = make2d(rows, cols, filler); + print_matrix(matrix, rows, cols); + return 0; +} + +int **make2d(size_t nrows, size_t ncols, int val) { + int **out = new int*[nrows]; + for(size_t i = 0; i < nrows; i++) { + int *row = new int[ncols]; + for(size_t j = 0; j < ncols; j++) { + *(row + j) = val; + } + *(out + i) = row; + } + return out; +} + +void print_matrix(int **matrix, size_t rows, size_t cols) { + for(size_t i = 0; i < rows; i++) { + for(size_t j = 0; j < cols; j++) { + cout << *(*(matrix + i) + j) << " "; + } + cout << endl; + } +} + diff --git a/1sem/programming basics/z11/32.cpp b/1sem/programming basics/z11/32.cpp new file mode 100644 index 0000000..c35e4f2 --- /dev/null +++ b/1sem/programming basics/z11/32.cpp @@ -0,0 +1,51 @@ +// pb_z11_32.cpp +// Горбацевич Андрей +#include + +using namespace std; + +int **read_matrix(size_t nrows, size_t ncols); +void print_matrix(int **matrix, size_t nrows, size_t ncols); +void fliplr(int **matrix, size_t nrows, size_t ncols); + +int main() { + size_t rows, cols; + cout << "rows, cols >>>"; + cin >> rows >> cols; + int **matrix = read_matrix(rows, cols); + cout << "Before:" << endl; + print_matrix(matrix, rows, cols); + fliplr(matrix, rows, cols); + cout << "After:" << endl; + print_matrix(matrix, rows, cols); + return 0; +} + +int **read_matrix(size_t nrows, size_t ncols) { + int **out = new int*[nrows]; + for(size_t i = 0; i < nrows; i++) { + int *row = new int[ncols]; + for(size_t j = 0; j < ncols; j++) { + cin >> *(row + j); + } + *(out + i) = row; + } + return out; +} + +void print_matrix(int **matrix, size_t nrows, size_t ncols) { + for(size_t i = 0; i < nrows; i++) { + for(size_t j = 0; j < ncols; j++) { + cout << *(*(matrix + i) + j) << " "; + } + cout << endl; + } +} + +void fliplr(int **matrix, size_t nrows, size_t ncols) { + for(size_t i = 0; i < nrows; i++) { + for(size_t j = 0; j < ncols/2; j++) { + swap(*(*(matrix + i) + j), *(*(matrix + i) + ncols - j - 1)); + } + } +} diff --git a/1sem/programming basics/z11/33.cpp b/1sem/programming basics/z11/33.cpp new file mode 100644 index 0000000..b450f2c --- /dev/null +++ b/1sem/programming basics/z11/33.cpp @@ -0,0 +1,53 @@ +// pb_z11_33.cpp +// Горбацевич Андрей +#include + +using namespace std; + +int **read_matrix(size_t nrows, size_t ncols); +void print_matrix(int **matrix, size_t nrows, size_t ncols); +void transpose(int **matrix, size_t size); + +int main() { + size_t size; + cout << "size >>>"; + cin >> size; + int **matrix = read_matrix(size, size); + cout << "Before:" << endl; + print_matrix(matrix, size, size); + transpose(matrix, size); + cout << "After:" << endl; + print_matrix(matrix, size, size); + return 0; +} + +int **read_matrix(size_t nrows, size_t ncols) { + int **out = new int*[nrows]; + for(size_t i = 0; i < nrows; i++) { + int *row = new int[ncols]; + for(size_t j = 0; j < ncols; j++) { + cin >> *(row + j); + } + *(out + i) = row; + } + return out; +} + +void print_matrix(int **matrix, size_t nrows, size_t ncols) { + for(size_t i = 0; i < nrows; i++) { + for(size_t j = 0; j < ncols; j++) { + cout << *(*(matrix + i) + j) << " "; + } + cout << endl; + } +} + +void transpose(int **matrix, size_t size) { + for(size_t i = 0; i < size; i++) { + for(size_t j = i; j < size; j++) { + int ri = *(*(matrix + i) + j); + int ci = *(*(matrix + j) + i); + swap(*(*(matrix + i) + j), *(*(matrix + j) + i)); + } + } +}