Advertisement
IzhanVarsky

Untitled

Mar 26th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. ifstream cin("input.txt");
  9. ofstream cout("output.txt");
  10.  
  11. char *getline();
  12.  
  13. int **create_array2d(size_t a, size_t b);
  14.  
  15. void free_array2d(int **arr);
  16.  
  17. char *resize(const char *string1, size_t size, size_t new_size);
  18.  
  19. void print_array2d(int **arr, size_t a, size_t b);
  20.  
  21. void swap_min(int **arr, size_t a, size_t b);
  22.  
  23. void task1();
  24.  
  25. void task2();
  26.  
  27. int main() {
  28.     task1();
  29.     task2();
  30.     return 0;
  31. }
  32.  
  33. void task1() {
  34.     cout << endl << " <<<<<<<  TASK 1:  >>>>>>> " << endl << endl;
  35.     size_t a = 10;
  36.     size_t b = 10;
  37.     int **arr = create_array2d(a, b);
  38.     print_array2d(arr, a, b); // with trash
  39.     swap_min(arr, a, b);
  40.     print_array2d(arr, a, b);
  41.     free_array2d(arr);
  42. }
  43.  
  44. void task2() {
  45.     cout << endl << " <<<<<<<  TASK 2:  >>>>>>> " << endl << endl;
  46.     char *str = getline();
  47.     while (str) {
  48.         cout << str << "::END::" << endl;
  49.         str = getline();
  50.     }
  51. }
  52.  
  53. void swap_min(int **arr, size_t a, size_t b) {
  54.     int min = INT32_MAX;
  55.     size_t str = 1;
  56.     for (size_t i = 1; i != a; ++i) {
  57.         for (size_t j = 1; j != b; ++j) {
  58.             if (arr[i][j] < min) {
  59.                 min = arr[i][j];
  60.                 str = i;
  61.             }
  62.         }
  63.     }
  64.     cout << endl << " MIN: " << min << "  STR: " << str << endl << endl;
  65.     if (str == 0) return;
  66.     int *new_line = arr[1];
  67.     arr[1] = arr[str];
  68.     arr[str] = new_line;
  69. }
  70.  
  71. int **create_array2d(size_t a, size_t b) {
  72.     int **arr = new int *[a];
  73.     arr[0] = new int[a * b];
  74.     for (size_t i = 1; i != a; ++i)
  75.         arr[i] = arr[i - 1] + b;
  76.     return arr;
  77. }
  78.  
  79. void free_array2d(int **arr) {
  80.     delete[] arr[0];
  81.     delete[] arr;
  82. }
  83.  
  84. void print_array2d(int **arr, size_t a, size_t b) {
  85.     for (size_t i = 1; i != a; ++i) {
  86.         for (size_t j = 1; j != b; ++j) {
  87.             cout << arr[i][j] << " ";
  88.         }
  89.         cout << endl;
  90.     }
  91. }
  92.  
  93. char *resize(const char *str, size_t size, size_t new_size) {
  94.     char *newStr = new char[new_size];
  95.     for (int i = 0; i < min(size, new_size); ++i) {
  96.         newStr[i] = str[i];
  97.     }
  98.     delete[] str;
  99.     return newStr;
  100. }
  101.  
  102. char *getline() {
  103.     size_t size = 10;
  104.     char *str = new char[size];
  105.     size_t curLen = 0;
  106.     char c;
  107.     cin.get(c);
  108.     while (true) {
  109.         if (c == '\0') {
  110.             if (curLen != 0) {
  111.                 break;
  112.             }
  113.             return nullptr; // не пашет если в конце непробельный или не \n и застревает
  114.         }
  115.         if (c == '\n') {
  116.             break;
  117.         }
  118.         if (curLen == size - 1) {
  119.             str = resize(str, size, size * 2);
  120.             size *= 2;
  121.         }
  122.         str[curLen] = c;
  123.         curLen++;
  124.         cin.get(c);
  125.     }
  126.     str[curLen] = '\0';
  127.     return str;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement