Advertisement
kirya_shkolnik

ee

Feb 2nd, 2023 (edited)
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int myInput();
  5. void myInputPointer(int* a);
  6. int compareString(char* a, char* b);
  7. int isCharInt(char ch);
  8. int isMyInputCorrect(int res);
  9.  
  10. int** resmat(int m, int n);
  11. int** input(int m, int n, int* matrixCorrect);
  12. void output(int** matrix, int m, int n);
  13.  
  14. /*
  15.     1 6 7
  16.     2 5 8
  17.     3 4 9
  18. */
  19. void sort_vertical(int **matrix, int n, int m, int **result_matrix);
  20.  
  21. /*
  22.     1 2 3
  23.     6 5 4
  24.     7 8 9
  25. */
  26. void sort_horizontal(int **matrix, int n, int m, int **result_matrix);
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. int main(){
  35.     int **matrix, **result;
  36.     int n,m;
  37.     m = myInput();
  38.     n = myInput();
  39.  
  40.     int matrixCorrect = 1;
  41.     if (isMyInputCorrect(m) && isMyInputCorrect(n) && m > 0 && n > 0) {
  42.  
  43.                 matrix = input(m,n,&matrixCorrect);
  44.                 if(matrixCorrect){
  45.                     result = resmat(m,n);
  46.                     sort_vertical(matrix, m, n, result);
  47.                     output(result, m, n);
  48.                    
  49.                     printf("\n");
  50.                     sort_horizontal(matrix, m, n, result);
  51.                     // output(result);
  52.                     output(result, m, n);
  53.                    
  54.                 }
  55.         }
  56.         else{
  57.             matrixCorrect = 0;
  58.         }
  59.        
  60.         if(matrixCorrect == 0){
  61.             printf("n/a");
  62.         }
  63. }
  64.  
  65. void sort(int *a, int n) {
  66.     int i, j;
  67.  
  68.     for (i = 0; i < n - 1; i++) {
  69.         for (j = 0; j < n - i - 1; j++) {
  70.             if (a[j] > a[j + 1]) {
  71.                 int temp = a[j];
  72.                 a[j] = a[j + 1];
  73.                 a[j + 1] = temp;
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79.  
  80. void sort_vertical(int **matrix, int m, int n, int **result_matrix){
  81.    int *a = malloc(m * n * sizeof(int));
  82.    int counter = 0;
  83.    
  84.    for (int i = 0; i < m; i++)
  85.      for (int j = 0; j < n; j++) {
  86.        a[counter] = matrix[i][j];
  87.        counter++;
  88.      }
  89.    
  90.    sort(a, counter);
  91.    
  92.    int counter1 = 0;
  93.    for (int i = 0; i < n; i++)
  94.      for (int j = 0; j < m; j++) {
  95.        result_matrix[j][i] = a[counter1];
  96.        counter1++;
  97.      }
  98.    free(a);
  99. }
  100.  
  101. void sort_horizontal(int **matrix, int m, int n, int **result_matrix){
  102.        int *a = malloc(m * n * sizeof(int));
  103.    int counter = 0;
  104.    
  105.    for (int i = 0; i < m; i++)
  106.      for (int j = 0; j < n; j++) {
  107.        a[counter] = matrix[i][j];
  108.        counter++;
  109.      }
  110.    
  111.    sort(a, counter);
  112.    
  113.    int counter1 = 0;
  114.    for (int i = 0; i < m; i++)
  115.      for (int j = 0; j < n; j++) {
  116.        result_matrix[i][j] = a[counter1];
  117.        counter1++;
  118.      }
  119.    free(a);
  120. }
  121. int **resmat(int m, int n) {  // m -rows n -cols
  122.     int **mat = (int **)malloc(m * n * sizeof(int) + m * sizeof(int *));
  123.     int *ptr = (int *)(mat + m);
  124.     //     int flag = 1;
  125.     //     int temp;
  126.     for (int i = 0; i < m; i++) mat[i] = ptr + n * i;
  127.     return mat;
  128. }
  129.  
  130. int** input(int m, int n, int* matrixCorrect) {  // m -rows n -cols
  131.     int** mat = (int**)malloc(m * n * sizeof(int) + m * sizeof(int*));
  132.     int* ptr = (int*)(mat + m);
  133.     int flag = 1;
  134.     int temp;
  135.     for (int i = 0; i < m; i++) mat[i] = ptr + n * i;
  136.     for (int i = 0; i < m; i++)
  137.         for (int j = 0; j < n; j++) {
  138.             if (flag) {
  139.                 temp = myInput();
  140.                 if (!isMyInputCorrect(temp)) {
  141.                     flag = 0;
  142.                     *matrixCorrect = 0;
  143.                 } else {
  144.                     mat[i][j] = temp;
  145.                 }
  146.             }
  147.         }
  148.     return mat;
  149. }
  150.  
  151.  
  152. void output(int** matrix, int m, int n) {
  153.     for (int i = 0; i < m; i++) {
  154.         for (int j = 0; j < n; j++) {
  155.             if (j == 0)
  156.                 printf("%d", matrix[i][j]);
  157.             else
  158.                 printf(" %d", matrix[i][j]);
  159.         }
  160.         if (i != m - 1) printf("\n");
  161.     }
  162. }
  163.  
  164. /**************** Проверка на ввод инта **********
  165.  * Концепция:
  166.  *
  167.  * Попытка проверить на инты без сторонних библиотек
  168.  *
  169.  * Так, моя идея создать функцию myInput() и там проверять через строки
  170.  * Так как злые дяди и тети запретили мне использовать сторонние библиотеки
  171.  * и я не могу использовать strcmp() я создал свой strcmp()
  172.  * Более детальное объяснение снизу. Мы русские с нами бог...
  173.  *************************************************/
  174.  
  175. /*************************************************
  176.  * Функция myInput():
  177.  * Какую же гадость я написал...
  178.  *
  179.  * У нас есть две строки checker
  180.  * Изначально из консоли мы записываем в checker1
  181.  * Переносим это все в int a
  182.  * Потом int a преобразуем в строку и записываем в checker2
  183.  * И по сути если мы ввели не int то здесь строки будут разные
  184.  *
  185.  * Проверка на одинаковость строк через другую непонятную функцию
  186.  *************************************************/
  187. int myInput() {
  188.     int a;
  189.     char checker1[256], checker2[256];
  190.     if (1 != scanf("%s", checker1)) {
  191.         a = -2147483647;
  192.     } else {
  193.         sscanf(checker1, "%d", &a);
  194.         sprintf(checker2, "%d", a);
  195.         if (compareString(checker1, checker2) != 0) {
  196.             a = -2147483647;
  197.         }
  198.     }
  199.     return a;
  200. }
  201.  
  202. /*************************************************
  203.  * Функция compareString():
  204.  * Какую же гадость я написал x2...
  205.  *
  206.  * Концепция что мы проверяем посимвольно и если вдруг какой-то
  207.  * символ другой возвращаем разницу этих символов по таблице ASCI
  208.  *************************************************/
  209. int compareString(char* a, char* b) {
  210.     while ((*a == *b) && (*a != 0)) {
  211.         // printf("%s. %d | ", a, *a);
  212.         // printf("%s. %d\n", b, *b);
  213.         a++;
  214.         b++;
  215.     }
  216.  
  217.     return *a - *b;
  218. }
  219.  
  220. /*************************************************
  221.  * Функция myInputPointer():
  222.  * Жизнь меня заставляет использовать указатель...
  223.  *
  224.  * Концепция один в один как myInput() только вместо return используем указатель
  225.  *************************************************/
  226.  
  227. void myInputPointer(int* a) {
  228.     char checker1[256], checker2[256];
  229.     if (1 != scanf("%s", checker1)) {
  230.         *a = -2147483647;
  231.     } else {
  232.         sscanf(checker1, "%d", a);
  233.         sprintf(checker2, "%d", *a);
  234.         if (compareString(checker1, checker2) != 0) {
  235.             *a = -2147483647;
  236.         }
  237.     }
  238. }
  239.  
  240. /*************************************************
  241.  * Функция isCharInt():
  242.  * Жизнь странная штука. Теперь я добавляю еще одну странную функцию...
  243.  *
  244.  * Смысл украден с myInput()
  245.  * Мы должны проверить является ли char числом [0-9]
  246.  * Получаем char ch, значение заносим в char[] checker1, потом переносим
  247.  * значение char[] checker1 в int a и потом в char[] checker2
  248.  * По итогу сравниваем checker1 и checker2
  249.  *
  250.  * Возвращаемые значения: целая положительная цифра [0-9] or -10 if error
  251.  *************************************************/
  252.  
  253. int isCharInt(char ch) {
  254.     int a, res = 1;
  255.     char checker1[256], checker2[256];
  256.     sprintf(checker1, "%c", ch);
  257.     sscanf(checker1, "%d", &a);
  258.     sprintf(checker2, "%d", a);
  259.     if (compareString(checker1, checker2) != 0) {
  260.         res = -10;
  261.     } else {
  262.         res = a;
  263.     }
  264.     return res;
  265. }
  266.  
  267. /*************************************************
  268.  * Я устал делать проверки поетому пусть он за меня делает
  269.  **************************************************/
  270. int isMyInputCorrect(int res) {
  271.     if (res != -2147483647) {
  272.         return 1;
  273.     } else {
  274.         return 0;
  275.     }
  276. }
  277.  
  278.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement